Create New XLS with C#

Posted on 2008-06-12
Last Modified: 2013-12-17
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
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;
            // 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 );
               //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

Question by:soapygus
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

shekharsom earned 500 total points
ID: 21770466
try using objBook.SaveAs

Author Comment

ID: 21770785
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);


Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question