Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone!

I have a task to dump the dataset data into Excel WorkBook.

Suppose if I have more than one table then I have to create more WorkSheets dynamically.

For that I did a small application but I was stuck with creating numbers of WorkSheets.

Could anyone help me?

Below is the Code:

private void btn_export2excel_Click(object sender, EventArgs e)
      {
          DataSet ds = new DataSet();
          ds.Tables.Add("Categories");
          ds.Tables.Add("Employee");
          try
          {
              Excel.Application xlApp;
              Excel.Workbook xlWorkBook;
              Excel.Worksheet[] xlWorkSheet = new Excel.Worksheet[2];
              object misValue = System.Reflection.Missing.Value;
              xlApp = new Excel.Application();
              xlWorkBook = xlApp.Workbooks.Add(misValue);
              xlWorkSheet[0] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
              xlWorkSheet[1] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

              xlWorkSheet[0].Cells[1, 1] = "www.google.com";
              xlWorkSheet[1].Cells[1, 1] = "www.yahoo.com";
              xlWorkBook.SaveAs("Sample.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
              xlWorkBook.Close(true, misValue, misValue);
              xlApp.Quit();
              releaseObject(xlWorkSheet[0]);
              releaseObject(xlWorkSheet[1]);
              releaseObject(xlWorkBook);
              releaseObject(xlApp);

          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message, "Message from form");
          }
          finally
          {
              GC.Collect();
          }
      }
      private void releaseObject(object obj)
      {
          try
          {
              System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
              obj = null;
          }
          catch (Exception ex)
          {
              obj = null;
              MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
          }
          finally
          {
              GC.Collect();
          }
      }
Posted
Updated 8-Sep-10 3:44am
v3
Comments
Dalek Dave 8-Sep-10 9:44am    
Edited for Readability and Grammar.

1 solution

The quickest way would be to set the Application.SheetsInNewWorkbook Property
http://msdn.microsoft.com/en-us/library/bb221669%28v=office.12%29.aspx[^]

MIDL
xlApp = new Excel.Application();
xlApp.SheetsInNewWorkbook = 10 //or whatever you need
xlWorkBook = xlApp.Workbooks.Add(misValue);


It changes the default number of sheets created in new workbooks (default is 3).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900