Home » C# scripts » Scripts to write excel file using c#

Scripts to write excel file using c#

Namespace:
using Microsoft.Office.Interop.Excel

Workbook eMWBook;
Sheets eMWSheet;
Worksheet eMWSheet1; 
rangeUsed ranges;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible=true;
xlApp.DisplayAlerts=false;
eMWBook =xlApp.Workbooks.Open("c:\\test.xls",0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
eMWSheet = eMWBook.Worksheets;
eMWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)eMWSheet.get_Item(<sheetName>);
ranges= eMWSheet1.Usedrange;
rownum=range.Rows.Count+1; 
eMWSheet1.Cells[rownum,1]="India";
eMWSheet1.Cells[rownum,2]="Delhi";
eMWBook.SaveAs("c:\\test.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,Missing.Value, Missing.Value, Missing.Value, Missing.Value,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value);
eMWBook.Close(true, null, null);
eMWSheet1 = null;
eMWBook = null;
xlApp.Quit();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

 


5 Comments

Leave a comment

Your email address will not be published. Required fields are marked *