CSV与EPPlus解析问题 [英] CSV with EPPlus parsing issues

查看:105
本文介绍了CSV与EPPlus解析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EPPlus读取数据。
此数据由 .xlsx .csv 组成。

I am using EPPlus to read data. This data consists of either a .xlsx or .csv.

当文件是 .csv 文件时,我使用 LoadFromText 功能。
但是EPPlus决定还必须解析所有不应的值。

When the file is a .csv file I use the LoadFromText functionality. But EPPlus decides that it also has to parse all the values, which it shouldn't.

例如:

Id;Double;
1;-3,88;

ExcelTextFormat format = new ExcelTextFormat();
format.Delimiter = ';';
worksheet.Cells[1, 1].LoadFromText( "file", format );

结果是 -3,88 值在工作表中变为: -388 。我发现这是因为EPPlus将-3,88视为一个数字并将其解析为一个数字,默认区域性为 InvariantCulture ,在这种情况下,它类似于(类似于) 美国

Result is that the -3,88 value becomes: -388 in the worksheet. This i found out is because EPPlus sees the -3,88 as a number and parses it as a number with the default culture being InvariantCulture which in this case is (similar to) us-US.

我如何实现EPPlus在不解析的情况下加载csv? (将所有值都作为字符串)

How can i achieve that EPPlus loads the csv without parsing? (takes all values as strings)

推荐答案

似乎EPPlus总是使用 en-美国格式。因此,首先将带有十进制值的列作为字符串导入。那样就不会尝试进行转换。

It seems that EPPlus always parses imported data with the en-US format. So first import the column with the decimal values as a string. That way there is no attempt at conversion.

ExcelTextFormat format = new ExcelTextFormat
{
    Delimiter = ';',
    DataTypes = new eDataTypes[] { eDataTypes.Number, eDataTypes.String }
};

然后使用正确的十进制分隔符导入值进行本地化后,循环循环值,将其转换

And after the values are imported with the correct decimal separators for your localization, loop the values, convert them to decimal and set the correct number format.

int columnNumber = 2;

for (int i = 2; i <= rowCount; i++)
{
    worksheet.Cells[i, columnNumber].Style.Numberformat.Format = "0.00";
    worksheet.Cells[i, columnNumber].Value = Convert.ToDecimal(worksheet.Cells[i, columnNumber].Value);
}

这篇关于CSV与EPPlus解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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