KUMARA VEL
asked on
C# list to excel
how to create dynamic column name while converting ilist to excel based on the field values in the list
Kumara,
Without additional details, the best I can offer you is my spreadsheets class and a link to the DLL I use to create the output. This is in a web app, so you will have to translate if you are using WinForms:
Without additional details, the best I can offer you is my spreadsheets class and a link to the DLL I use to create the output. This is in a web app, so you will have to translate if you are using WinForms:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing;
using System.Web.SessionState;
using OfficeOpenXml;
using OfficeOpenXml.Style;
namespace ClearConnect_v3.Classes
{
public struct SheetMapHeader
{
public int FieldIndex { get; set; }
public string FieldValue { get; set; }
public string FieldType { get; set; }
}
public struct SheetMapRows
{
public SheetMapHeader Header { get; set; }
public int FieldIndex { get; set; }
}
public class Spreadsheets
{
public Spreadsheets() { }
public ExcelPackage ExportRecordsAll(List<MyClass> yourClass)
{
int r = 1;
if (yourClass.Count == 0)
return null;
ExcelPackage pkg = new ExcelPackage();
ExcelWorksheet ws = pkg.Workbook.Worksheets.Add("My Class Report");
ws.InsertRow(1, 1);
ws.Cells[r, 1].Value = "Property1";
ws.Cells[r, 2].Value = "Property2";
ws.Cells[r, 3].Value = "Property3";
ws.Cells[r, 4].Value = "Property4";
//You could continue this for as many properties as are in that class.
r++;
foreach(MyClass c in yourClass)
{
ws.InsertRow(r, 1);
ws.Cells[r, 1].Value = c.Property1;
ws.Cells[r, 2].Value = c.Property2;
ws.Cells[r, 3].Value = c.Property3;
ws.Cells[r, 4].Value = c.Property4;
r++;
}
return pkg;
}
}
}
I got the OfficeOpenXML library from the EPPlus CodePlex site
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Is this a List<class> or a List<string> or what?