Create Excel sheet in C#.net using interop service
Here I am going to explain how to create excel sheet dynamically in c#.net using .net's interop service. Sometimes we need to create excel sheet from provided database's data and we need to save that excel file into a drive. With this interop service we can access each and every functions of Microsoft Excel.
Here I am going to explain how to create one excel file in C#.net using microsoft office interop service.
To Create Excel file follow the below steps :Step 1 :
Add the Microsoft Excel 12.0 Object Library to your project.
--Create a new website and add a Command Button to the page.
--To add Excel reference library in to your project see below picture.
Step 2 :
Select Add Reference dialogue from Website MenuStep 3 :
Select Microsoft.Excel 12.0 Object Library and click OK buttonStep 4 :
Now add Interop.Excel.dll to your projects bin folder and then add the reference to Interop.Excel.dll
To make reference to Interop.Excel.dll follow the same steps as above but instead of choose COM tab select Browse tab and then you can navigate to your bin folder and then click on Interop.Excel.dll and then click on OK
Download Interop.Excel.dll file.
Then Use the below code to create a simple excel file and save it to c drive :Code :
protected void Button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "Sample Data";
xlWorkBook.SaveAs("c:\\csharp-Excel.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);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
}
finally
{
GC.Collect();
}
}
Reference: http://dotnetsquare.com/resources/41-create-excel-sheet-in-C-Sharp-using-interop-service
sir/mam
create the excel sheet but it asked for replace the previous excel sheet...
but i want save in any where in system