I have a C# method, see code below, that generates an Excel spreadsheet using EPPlus. The method takes in 3 strings, one of which is a pipe delimited string named csv that has my data in it. The data I’m using has 14 columns, and in the 14th column I have data that looks like: 103.01.06, or 01.01, or some other numeric values separated by periods. The problem I’m seeing is that if the value is 103.01.06, then EPPlus transforms that into the value -656334. Is there some way to fix this?
private void GenerateSpreadsheet(string id, string csv, string worksheetName)
{
using (ExcelPackage pck = new ExcelPackage())
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(worksheetName);
ws.Cells["A1"].LoadFromText(csv, new ExcelTextFormat{Delimiter = '|'});
Response.AddHeader("Content-Disposition",
String.Format("attachment; filename={0}.xlsx", id));
Response.ContentType =
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.BinaryWrite(pck.GetAsByteArray());
Response.End();
}
}
If you don’t specify a columns type via the Datatypes property of the
ExcelTextFormatobject then EPPlus assumes unknown for the column. Since you have fourteen columns and you are having trouble with data in the fourteenth column you’ll need to specify types for each prior column. Without knowing your Datatypes I’ve just listed fourteen instances ofeDataTypes.String, buteDataTypes.Numberetc. would also work.EPPlus makes a guess when assigning a type to a column.