Hi I am having a list container which contains the list of values. I wish to export the list values directly to Excel. Is there any way to do it directly?
On the Lists app for Windows, you'll now see an export dropdown menu with options to export data as a CSV file or as an Excel workbook.
You can write any data (lists, strings, numbers etc) to Excel, by first converting it into a Pandas DataFrame and then writing the DataFrame to Excel. To export a Pandas DataFrame as an Excel file (extension: . xlsx, . xls), use the to_excel() method.
OK, here is a step-by-step guide if you want to use COM.
using NsExcel = Microsoft.Office.Interop.Excel; public void ListToExcel(List<string> list) { //start excel NsExcel.ApplicationClass excapp = new Microsoft.Office.Interop.Excel.ApplicationClass(); //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:\test.xls"; var workbook = excapp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); //Not done yet. You have to work on a specific sheet - note the cast //You may not have any sheets at all. Then you have 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 not 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 by now, so a detailed explanation about workbook.SaveAs and workbook.Close is not necessary //important: if you did not make excel visible terminating your application will terminate excel as well - I tested it //but if you did it - to be honest - I don't know how to close the main excel window - maybee somewhere around excapp.Windows or excapp.ActiveWindow }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With