I need export it to a excel file
How to export a C# list 'T' to a excel file
I have a c# List
I need export it to a excel file
I need export it to a excel file
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Jul 19, 2011, 10:31 PM EDT
Try this,
using NsExcel = Microsoft.Office.Interop.Excel;
public void ListToExcel(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 = 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
}
Kyle Story24 reputationPosted Nov 24, 2014, 12:21 AM EST
Hi, in case anyone has a generic list (not a string list) like list of entities from Entity Framework or any other list of objects that contain the required information in their properties, then here is what you can do:
public static void ExportGenericListToExcel(List list, string excelFilePath) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); ExcelFile file = new ExcelFile(); ExcelWorksheet sheet = file.Worksheets.Add("Exported List"); for (int i = 0; i for (int i = 0; i for (int j = 0; j < properties.Count; j++) sheet.Cells[i + 1, j].Value = properties[j].GetValue(list[i]); file.Save(excelFilePath); }And here is an example how you can use it:
// My custom type called 'Student'. var students = new ListStudent>(); // ... ExportGenericListToExcel(students, "Students.xlsx");Code uses this helper .NET component for Excel.