views:

128

answers:

1

Hi, I am working on Windows Forms application , c# I have three datatables. I want to save the first table in Sheet1 of an excel , second table in Sheet2 of the same excel and the third in the Sheet3.

The names of these three sheets will remain the same, as Summary,Details and Retailers.

Can anyone please let me know how do I do this.

Regards

Hema

A: 

The only way I am aware of is to use the PIA (Primary Interop Assemblies). Here is some sample code using the PIA to create an excel file, and several worksheets:

ApplicationClass exApp;
Workbook book;
Worksheet sheet;

exApp = new ApplicationClass();
book = exApp.Workbooks.Add(missing);
for (int sheetnum=1; sheetnum++; sheetnum<5)
{
 sheet = (Worksheet)book.Worksheets.Add(missing, missing, missing, XlSheetType.xlWorksheet);
 sheet.Name = "Sheet Number: " + sheetnum.ToString();
 sheet.Cells[1,1] = "Hello, I am here";
}
book.Close(true, "TestSheet.xls", null);
exApp.Quit();

There are some catches - Excel must be installed on the PC running the code, and Microsoft does not advise using the PIA's on a server for obvious reasons.

thorkia