• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5982
  • Last Modified:

Create New XLS with C#

Hello Experts,

I have what I think is a pretty basic question -- I have modified the attached snippet to fill a spreadsheet from a dataview (via an array).  When the procedure completes the spreadsheet is visible and called "Book1."

I have been trying to modify the code so that I can create an xls with a specficied filename and then save and close the the XLS.

So how can I create an XLS called "c:\\mySpreadsheet.xls"?

Thanks for your help.

The attached snippet (pulled from http://support.microsoft.com/kb/302096/EN-US/)
Excel.Application objApp;
      Excel._Workbook objBook;
 
      private void button1_Click(object sender, System.EventArgs e)
      {
         Excel.Workbooks objBooks;
         Excel.Sheets objSheets;
         Excel._Worksheet objSheet;
         Excel.Range range;
 
         try
         {
            // Instantiate Excel and start a new workbook.
            objApp = new Excel.Application();
            objBooks = objApp.Workbooks;
            objBook = objBooks.Add( Missing.Value );
            objSheets = objBook.Worksheets;
            objSheet = (Excel._Worksheet)objSheets.get_Item(1);
 
            //Get the range where the starting cell has the address
            //m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
            range = objSheet.get_Range("A1", Missing.Value);
            range = range.get_Resize(5, 5);
 
            if (this.FillWithStrings.Checked == false)
            {
               //Create an array.
               double[,] saRet = new double[5, 5];
 
               //Fill the array.
               for (long iRow = 0; iRow < 5; iRow++)
               {
                  for (long iCol = 0; iCol < 5; iCol++)
                  {
                     //Put a counter in the cell.
                     saRet[iRow, iCol] = iRow * iCol;
                  }
               }
 
               //Set the range value to the array.
               range.set_Value(Missing.Value, saRet );
            }
 
            else
            {
               //Create an array.
               string[,] saRet = new string[5, 5];
 
               //Fill the array.
               for (long iRow = 0; iRow < 5; iRow++)
               {
                  for (long iCol = 0; iCol < 5; iCol++)
                  {
                     //Put the row and column address in the cell.
                     saRet[iRow, iCol] = iRow.ToString() + "|" + iCol.ToString();
                  }
               }
 
               //Set the range value to the array.
               range.set_Value(Missing.Value, saRet );
            }
 
            //Return control of Excel to the user.
            objApp.Visible = true;
            objApp.UserControl = true;
         }
         catch( Exception theException ) 
         {
            String errorMessage;
            errorMessage = "Error: ";
            errorMessage = String.Concat( errorMessage, theException.Message );
            errorMessage = String.Concat( errorMessage, " Line: " );
            errorMessage = String.Concat( errorMessage, theException.Source );
 
            MessageBox.Show( errorMessage, "Error" );
         }
      }

Open in new window

0
soapygus
Asked:
soapygus
1 Solution
 
shekharsomCommented:
try using objBook.SaveAs
0
 
soapygusAuthor Commented:
I got SaveAs to work, but I found using the below line was better for my purposes.  Where oName is the FileName that I assigned.

 objBook.Close(true, oName, oMissing);

Thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now