EPPlus转换CSV并从双精度中丢失逗号 [英] EPPlus converting csv and missing the comma from a double

查看:194
本文介绍了EPPlus转换CSV并从双精度中丢失逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码将CSV文件转换为xlsx文件:

I'm trying to convert a CSV file to xlsx file using the following code:

        string csvFileName = path;

        string nomeArquivo = Path.Combine(serverpath, RandomString(10) + ".xlsx");

        string worksheetsName = "b2w";
        bool firstRowIsHeader = false;
        var format = new ExcelTextFormat();
        format.Delimiter = ';';
        format.EOL = "\n";              // DEFAULT IS "\r\n";
                                          // format.TextQualifier = '"';

        using (ExcelPackage package = new ExcelPackage(new FileInfo(nomeArquivo)))
        {
            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(worksheetsName);
            worksheet.Cells["A1"].Value = "Status Pedido";
            worksheet.Cells["B1"].Value = "Site Origem";
            worksheet.Cells["C1"].Value = "Número Pedido";
            worksheet.Cells["D1"].Value = "Número Entrega";
            worksheet.Cells["E1"].Value = "Total Pedido";
            worksheet.Cells["F1"].Value = "CPF";
            worksheet.Cells["G1"].Value = "Nome Completo";
            worksheet.Cells["H1"].Value = "Endereço";
            worksheet.Cells["I1"].Value = "Número";
            worksheet.Cells["J1"].Value = "Complemento";
            worksheet.Cells["K1"].Value = "Referencia";
            worksheet.Cells["L1"].Value = "Bairro";
            worksheet.Cells["M1"].Value = "CEP";
            worksheet.Cells["N1"].Value = "Estado";
            worksheet.Cells["O1"].Value = "Cidade";
            worksheet.Cells["P1"].Value = "Merda1";
            worksheet.Cells["Q1"].Value = "Telefone Fixo";
            worksheet.Cells["R1"].Value = "Telefone Celular";
            worksheet.Cells["S1"].Value = "Merda2";
            worksheet.Cells["T1"].Value = "Sku Lojista";
            worksheet.Cells["U1"].Value = "Merda3";
            worksheet.Cells["V1"].Value = "Quantidade";
            worksheet.Cells["W1"].Value = "Valor unitário";
            worksheet.Cells["X1"].Value = "Valor Frete";
            worksheet.Cells["Y1"].Value = "Data Entrega";
            worksheet.Cells["Z1"].Value = "Valor Adicional";
            worksheet.Cells["A2"].LoadFromText(new FileInfo(csvFileName), format, OfficeOpenXml.Table.TableStyles.Medium27, firstRowIsHeader);
            package.Save();

            B2wExcelInsertPrimeiro InsertData = new B2wExcelInsertPrimeiro(nomeArquivo);
            InsertData.Insert();
        }

转换后,它会保存文件,但像319,98这样的双精度值被保存为31998。

After the convertion, it saves the file but the double values like 319,98 are being saved as 31998.

我在做什么错了?

推荐答案

看起来像是 CultureInfo 问题。例如,在美国,我们使用代替来标记双精度十进制值。因此,如果您输入这样的数字,则逗号将被忽略。如果您使用将 NumberDecimalSeparator 设置为逗号的区域性,它应该可以工作。例如:

Looks like a CultureInfo problem. For example, in the US we use . instead of , to demark the decimal value of a double. So if you import a number like that the comma would be ignored. If you use a culture which has the NumberDecimalSeparator set to a comma it should work. For example:

format.Culture = new CultureInfo("de-DE"); //Using German cultureinfo

应该可以解决问题。您可以从这里获得良好的文化列表:

should solve the problem. You can get a good list of cultures from here:

http://www.csharp-examples.net/culture-names/

这篇关于EPPlus转换CSV并从双精度中丢失逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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