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 ExcelTextFormat object 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 of eDataTypes.String, but eDataTypes.Number etc. would also work.

EPPlus makes a guess when assigning a type to a column.

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 = '|', 
            DataTypes = new eDataTypes[] { eDataTypes.String, eDataTypes.String, 
                eDataTypes.String, eDataTypes.String, eDataTypes.String,
                eDataTypes.String, eDataTypes.String, eDataTypes.String,
                eDataTypes.String, eDataTypes.String, eDataTypes.String, 
                eDataTypes.String, eDataTypes.String, eDataTypes.String } }); 

        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();
    }
}

其他提示

It seems to be interpreted as DateTime, maybe a bug.

Try this:

var format = New ExcelTextFormat()
format.Delimiter = '|';
ws.Cells("A1").LoadFromText(csv, format);
var colNumber = 14;
var colRange = ws.Cells(1, colNumber, ws.Dimension.End.Row, colNumber);
colRange.Style.Numberformat.Format = "@";

If that does not work, try to set the format before you LoadFromText the CSV.

For example:

var csv = IO.File.ReadAllText(csvPath);
var ws = pck.Workbook.Worksheets.Add("Test");
var format As New ExcelTextFormat();
format.Delimiter = '|';
// if columns 4 shall not be interpreted like a DateTime 
format.DataTypes = {
    null,
    null,
    null,
    eDataTypes.String
};
var range = ws.Cells("A1").LoadFromText(csv, format);

Note: You must provide a DataType for every column. But i have tested it with null, seem to work if you don't want to specify the type. If it does not compile, feel free to change(manually converted from VB.NET).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top