'I want to convert data table to epplus Excel sheet, I am getting data from via angularjs that data I want to receive and convert to Excel format
public byte[] GetLeaveReportExcel(List<dynamic> details)
{
try
{
var json = JsonConvert.SerializeObject(details);
DataTable dataTable = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
// DataTable dt = StringHelper.ClassToDataTable(details);
//List<string> excelData = new List<string>();
//byte[] bin = File.ReadAllBytes("C:\\Leave Report.xlsx");
//byte[] bin = File.ReadAllBytes(Server.MapPath("Templates\TaskReport.xlsx "));
//DataTable dataTable = new DataTable();
using (MemoryStream stream = new MemoryStream())
using (ExcelPackage excelPackage = new ExcelPackage(stream))
{
//ExcelCellBase table = new ExcelCellBase (dataTable.Columns.Count);
foreach (ExcelWorksheet (dataTable.Columns.Count) in excelPackage.Workbook.Worksheets)
{
//loop all rows
for (int i = 0; i < dataTable.Rows.Count; i++)
{
//loop all columns in a row
for (int j = 0; j < dataTable.Columns.Count; j++)
{
//add the cell data to the List
if (dataTable.Columns.Count.Cells[i, j].Value != null)
{
details.Add(dataTable.Columns.Count.Cells[i, j].Value.ToString());
}
}
}
}
byte[] content = stream.ToArray();
return content;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (session != null)
{
session.Close();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
