Gembox在保存为CSV时删除精度

本文关键字:删除 精度 CSV 保存 Gembox | 更新日期: 2023-09-27 18:07:21

我正在使用Gembox电子表格将一些数据导出到。csv,但在输出文件中没有得到任何小数。

当导出为XLSX时,一切看起来都像预期的那样。

我尝试了Gembox 3.7和3.9,但结果是一样的。

使用以下代码重现此问题。

        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
        var ef = new ExcelFile();
        var ws = ef.Worksheets.Add("NumberFormatTest");
        ws.Cells[0, 0].Value = "Expected";
        ws.Cells[1, 0].Value = "0";
        ws.Cells[2, 0].Value = "0.0";
        ws.Cells[3, 0].Value = "0.00";
        ws.Cells[0, 1].Value = "Actual";
        ws.Cells[1, 1].Value = 0m;
        ws.Cells[1, 1].Style.NumberFormat = "0";
        ws.Cells[2, 1].Value = 0m;
        ws.Cells[2, 1].Style.NumberFormat = "0.0";
        ws.Cells[3, 1].Value = 0m;
        ws.Cells[3, 1].Style.NumberFormat = "0.00";
        ef.Save("Numberformat test.csv");
        ef.Save("Numberformat test.xlsx");

如何在不使用.ToString("0.00")的情况下得到正确的结果?

Gembox在保存为CSV时删除精度

如果别人感兴趣的话。我联系了他们的支持人员,他们告诉我数字格式目前不能导出为CSV文件格式,应该使用以下解决方案。

foreach (var row in ws.Rows)
    foreach (var cell in row.AllocatedCells)
        if (cell.Value != null)
            cell.Value = cell.GetFormattedValue();

ef.Save("Numberformat test.csv");

澳大利亚VB用户

    Dim enAU As CultureInfo = CultureInfo.CreateSpecificCulture("en-AU")
    Dim SaveOptions As New CsvSaveOptions(CsvType.CommaDelimited)
    With SaveOptions
        .FormatProvider = enAU
    End With
    For Each row In ws.Rows
        For Each cell In row.AllocatedCells
            If Not (String.IsNullOrEmpty(cell.Value)) Then
                cell.Value = cell.GetFormattedValue()
            End If
        Next
    Next
    ws.Save(sSaveAsFullFilename, New CsvSaveOptions(CsvType.CommaDelimited) With {.FormatProvider = enAU}) 

使用较新的版本(GemBox。电子表格4.5及以上),这可以通过使用CsvSaveOptions.UseFormattedValues属性来简化。

例如:

var options = new CsvSaveOptions(CsvType.CommaDelimited);
options.UseFormattedValues = true;
ef.Save("Numberformat test.csv", options);