Add a reference “Microsoft.Office.Interop.Excel” into project
![]()
Reading and Writing Data to Excel using Microsoft.Office.Interop.Excel
using Excel=Microsoft.Office.Interop.Excel;
using System.Reflection;
Excel.ApplicationClass excelApp = new Excel.ApplicationClass();
Excel.Workbook workbook = (Excel.Workbook)excelApp.Workbooks.Add(Missing.Value);
Excel.Worksheet worksheet;
// Opening excel file
workbook = excelApp.Workbooks.Open("D:\\test.xlsx", 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
// Get first Worksheet
worksheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
// Setting cell values
((Excel.Range)worksheet.Cells["1", "A"]).Value2 = "5";
((Excel.Range)worksheet.Cells["2", "A"]).Value2 = "7";
workbook.Save();
workbook.Close(0, 0, 0);
excelApp.Quit();
Related posts:
No Comments