I have JSON data that I bind to a table:
Now I would like to export this data to Excel, so I have written this code that involves data formatting of an Excel spreadsheet.
private static MemoryStream JsonToStream<T>(string data, string html, string movieReportName)
{
IEnumerable<T> serializedData = data.JsonDeserialize<T>().AsEnumerable<T>();
List<SelectListItem> colOrder = ParseColumnsToList(html);
DataTable dataTable = serializedData.ToDataTable<T>(colOrder);
ExcelPackage epackage = new ExcelPackage();
ExcelWorksheet excel = epackage.Workbook.Worksheets.Add(movieReportName);
excel.Cells["A1"].LoadFromDataTable(dataTable, true);
int rowCount = dataTable.Rows.Count;
int columnCount = dataTable.Columns.Count;
for (int i = 2; i < rowCount + 2; i++)
{
for (int j = 1; j <= columnCount; j++)
{
if (value.Contains("%"))
{
if ((dataTable.Columns[j - 1].ColumnName != "Movie Code") {
value = value.Replace("%", "");
if (value.Contains("."))
{
double number = 0.0;
double.TryParse(value, out number);
excel.Cells[i, j].Value = number / 100;
excel.Cells[i, j].Style.Numberformat.Format = "0.00##%";
}
}
}
}
}
MemoryStream ms = new MemoryStream(epackage.GetAsByteArray());
epackage.Dispose();
return ms;
}
This works fine but it runs very slowly. Do you think I have to move data formatting to a database?
Update What are different better option to export large data from HTML to excel?
JsonToStream
be doing anything at all in excel or data tables? Very misleading! \$\endgroup\$JsonToStream
should only convert JSON to a stream. \$\endgroup\$