- Microsoft.Office.Interop.Excel
- You have the to have Excel installed.
- ADD a reference to your project to the Excel interop DLL. To does this on the. NET tab Select Microsoft.Office.Interop.Excel. There could is multiple assemblies with the this name. Select the appropriate for your Visual Studio and Excel version.
- Here's a code sample to create a new Workbook and fill a column with the items from your list
If you want to make Excel visible Excapp. Visible = true; Create a blank workbook var workbook = Excapp. Workbooks.Add (NsExcel.XlWBATemplate.xlWBATWorksheet); or open one-this is no pleasant, but Yue ' re probably interested in the first parameter string Workbookpath = "C:\te St.xls "; var workbook = Excapp. Workbooks.Open (Workbookpath, 0, False, 5, "", "", False, Excel.XlPlatform.xlWindows, "", True, False, 0, Tru E, False, false); Not done yet. You should have a specific sheet-note the cast//you and no sheets at all. Then you had to add one with NSEXCEL.WORKSHEET.ADD () var sheet = (nsexcel.worksheet) workbook. SHEETS[1]; Indexing starts from 1//do something usefull:you Select now an individual cell var range = Sheet.get_range ("A1", "A1"); Range. Value2 = "Test"; Value2 is a typo//now the list string cellname; int counter = 1; foreach (var item in list) {cellname = "A" + couNter. ToString (); var range = Sheet.get_range (Cellname, cellname); Range. Value2 = Item. ToString (); ++counter; }//you ' ve probably got the point is now, so a detailed explanation about workbook. SaveAs and Workbook. Close is not necessary//important:if do not make Excel visible terminating your application would terminate Excel As Well-i tested it//but if you do it-to be honest-i don ' t know what to close the main Excel Window-maybee so Mewhere around Excapp. Windows or Excapp. ActiveWindow}
Using the CSV idea
Using system.io;using (StreamWriter SW = File.createtext ("List.csv")) {for (int i = 0; i < L.count; i++) { s W.writeline (L[i]);} }
Using Closedxml Library (there is no need to install MS Excel
Https://github.com/closedxml/closedxml/wiki
https://www.c-sharpcorner.com/UploadFile/deveshomar/exporting-generic-listt-to-excel-in-C-Sharp-using-interop/
C # note38:export data into Excel