1 | initial version |
The Microsoft.Office.Interop.Excel can be utilized with c# in multiple ways. Below are the steps that can be followed:
Firstly, install the Microsoft.Office.Interop.Excel package from the NuGet Package Manager in Visual Studio.
In the C# code, add the following namespace: using Microsoft.Office.Interop.Excel;
Create an instance of the Excel application using the following code: Excel.Application xlApp = new Excel.Application();
Next, create a new workbook and worksheet using the following code: Excel.Workbook xlWorkbook = xlApp.Workbooks.Add(); Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1);
You can then modify the worksheet by adding data, formatting cells, etc. For example: xlWorksheet.Cells[1, 1] = "Name"; xlWorksheet.Cells[1, 2] = "Age"; xlWorksheet.Cells[2, 1] = "John"; xlWorksheet.Cells[2, 2] = "30";
Finally, save the workbook and close the Excel application using the following code: xlWorkbook.SaveAs("C:\MyWorkbook.xlsx"); xlWorkbook.Close(); xlApp.Quit();
These are just some basic examples of how the Microsoft.Office.Interop.Excel can be utilized with c#. There are many more possibilities, such as reading and writing data from existing Excel files.