1

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

2 回答 2

3

它似乎被解释为DateTime,也许是一个错误。

尝试这个:

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 = "@";

如果这不起作用,请尝试LoadFromText在 CSV 之前设置格式。

例如:

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

注意:您必须为每一列提供一个 DataType。但是我已经用它进行了测试null,如果您不想指定类型,它似乎可以工作。如果无法编译,请随意更改(从 VB.NET 手动转换)。

于 2011-12-02T16:47:57.977 回答
2

如果您没有通过ExcelTextFormat对象的 Datatypes 属性指定列类型,则 EPPlus 假定该列是未知的。由于您有 14 列,并且您在第 14 列中的数据遇到问题,因此您需要为之前的每一列指定类型。在不知道您的数据类型的情况下,我刚刚列出了eDataTypes.String, 但是eDataTypes.Number等的十四个实例也可以。

EPPlus 在为列分配类型时会进行猜测。

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();
    }
}
于 2011-12-03T02:54:51.887 回答