EPPlus转换csv并从双重中删除逗号

时间:2016-04-28 23:12:54

标签: c# c#-4.0 cultureinfo epplus

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

        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。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

看起来像CultureInfo问题。例如,在美国,我们使用.代替,来标记双精度的十进制值。因此,如果您导入这样的数字,则会忽略逗号。如果您使用将NumberDecimalSeparator设置为逗号的文化,则该文化应该有效。例如:

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

应该解决问题。您可以从这里获得一份很好的文化清单:

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