使用EPPlus如何生成一个电子表格,其中数字是数字而不是文本 [英] Using EPPlus how can I generate a spreadsheet where numbers are numbers not text

查看:170
本文介绍了使用EPPlus如何生成一个电子表格,其中数字是数字而不是文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 LoadFromArrays

数组的第一个条目是标题,其他条目可能是数字,文本或日期(但对于列表中的每个数组都是相同的)。

The first entry of the array is a title, the other entries are possibly numbers, text or dates (but the same for each array in the list).

生成的Excel表格有绿色三角形警告,数字格式为文本。

The generated Excel sheet has the green triangle warning that numbers are formatted as text.

我循环遍历所有单元格并将其格式设置为Number像这样 ws.Cells [i,j] .Style.Numberformat.Format =0;

I loop through all the cells and set their format to Number like so ws.Cells[i, j].Style.Numberformat.Format = "0";

问题仍然存在,我仍然看到绿色警告,即使当我查看格式单元格... 对话框时,数字格式设置为数字。

However the problem remains and I still see the green warning, even though the number format is set to number when I look in the Format Cell... dialogue.

我在这里有什么选择?我有可能更多地了解每列中的类型,但是如何设置列标题?

What are my options here? It is possible for me to know a bit more about what type is in each column, but how do I then set a column title?

有没有比EPPlus更好的解决方案?或者在下载之前可以进行一些更多的后处理电子表格?

Is there a better solution than EPPlus? or some more post processing of the spreadsheet I can do before downloading it?

推荐答案

由于使用对象数组,因此可以包含数字和字符串看起来像数字,你将必须通过每个对象并确定其类型:

Since you are using objects arrays they can contain numbers and strings that look like numbers you will have to go through each object and determine its type:

[TestMethod]
public void Object_Type_Write_Test()
{
    //http://stackoverflow.com/questions/31537981/using-epplus-how-can-i-generate-a-spreadsheet-where-numbers-are-numbers-not-text
    var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
    if (existingFile.Exists)
        existingFile.Delete();

    //Some data
    var list = new List<Object[]>
    {
        new object[]
        {
            "111.11",
            111.11,
            DateTime.Now
        }
    };

    using (var package = new ExcelPackage(existingFile))
    {
        var ws = package.Workbook.Worksheets.Add("Sheet1");
        ws.Cells[1, 1, 2, 2].Style.Numberformat.Format = "0";
        ws.Cells[1, 3, 2, 3].Style.Numberformat.Format = "[$-F400]h:mm:ss\\ AM/PM";

        //This will cause numbers in string to be stored as string in excel regardless of cell format
        ws.Cells["A1"].LoadFromArrays(list);

        //Have to go through the objects to deal with numbers as strings
        for (var i = 0; i < list.Count; i++)
        {
            for (var j = 0; j < list[i].Count(); j++)
            {

                if (list[i][j] is string)
                    ws.Cells[i + 2, j + 1].Value = Double.Parse((string) list[i][j]);
                else if (list[i][j] is double)
                    ws.Cells[i + 2, j + 1].Value = (double)list[i][j];
                else
                    ws.Cells[i + 2, j + 1].Value = list[i][j];

            }
        }

        package.Save();
    }
}

使用上述,您将看到下面的图像输出注意左上角的单元格带有绿色箭头,因为它是一个由 LoadFromArray 编写的字符串,看起来像一个数字:

With the above, you see the image below as the output Note the upper left corner cell with the green arrow because it was a string that was written by LoadFromArray which looks like a number:

这篇关于使用EPPlus如何生成一个电子表格,其中数字是数字而不是文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆