简体   繁体   English

Epplus无法将列格式化为数字

[英]Epplus not able to format the column as number

I have assigned the data to a particular column in my code. 我已将数据分配给代码中的特定列。 Converted the columns as number 将列转换为数字

workSheet.Cells["B3:B11"].Style.Numberformat.Format = "#,##0.00";    
workSheet.Cells["B3"].Value = obj.Number1;
workSheet.Cells["B4"].Value = obj.Number2;
workSheet.Cells["B5"].Value = obj.Number3;
workSheet.Cells["B6"].Value = obj.Number4;
workSheet.Cells["B7"].Value = obj.Number5;
workSheet.Cells["B8"].Value = obj.Number6;
workSheet.Cells["B9"].Value = obj.Number7;
workSheet.Cells["B10"].Value = obj.Number8;
workSheet.Cells["B11"].Value = obj.Number9;

When excel is generated, the column are in text format. 生成excel时,该列为文本格式。 I want them to be in number format. 我希望它们采用数字格式。 Need help. 需要帮忙。

If those are strings then Epplus/excel will respect that and save them as strings so the formatting you set will be ignored. 如果这些是strings那么Epplus / excel会尊重这一点,并将其另存为字符串,因此您设置的格式将被忽略。 You will likely get the green triangles in the corners of the cells indication that they look like numbers. 您可能会在单元格的角落看到绿色的三角形,表明它们看起来像数字。

To treat them as numbers, you need to convert them to doubles like this: 要将它们视为数字,您需要将它们转换为doubles如下所示:

workSheet.Cells["B3"].Value = Double.Parse(obj.Number1);

If you are confident that they will never be null or improperly formatted then the above should be good enough. 如果您确信它们永远不会为null或格式不正确,那么以上内容就足够了。 Otherwise, might want to do some checks like this: 否则,可能要进行如下检查:

double number;
if (obj.Number1 != null && Double.TryParse(obj.Number1, out number))
    workSheet.Cells["B3"].Value = number;

You can do something like below which work for me: 您可以像下面这样对我有用:

//To align center
sheetcreate.Cells[1, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

//To align right
eachRowObject.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM