【问题标题】:excel cell value precisionexcel单元格值精度
【发布时间】:2017-05-27 11:41:31
【问题描述】:

我正在使用 Apache POI 检索单元格双精度值:

1.以openxml格式存储的cell实际值为“5.259761432023309”, 2.同一单元格的格式化显示值为“5.26%”。 3.但是MS Excel公式栏上显示的值是“5.25976143202331%”

使用 Apache POI,我可以检索:

值为1,使用cell.getNumericValue();

我也可以检索 值 2,使用 DataFormatter df = new DataFormatter();String asItLooksInExcel = df.formatCellValue(cell);

但是,我无法检索值 3,即公式栏上显示的值,请建议检索方法。

【问题讨论】:

    标签: java excel apache apache-poi aspose


    【解决方案1】:

    我无法证实你的观察。

    如果我有以下 Excel:

    正如您在A1 中看到的5.25976143202331%。它是德语Excel,所以小数点分隔符是逗号。但这没关系。

    XML 是:

    <sheetData>
     <row r="1" spans="1:1" x14ac:dyDescent="0.25">
      <c r="A1" s="1">
       <v>5.2597614320233098E-2</v>
      </c>
     </row>
    </sheetData>
    

    还有下面的代码

    import org.apache.poi.xssf.usermodel.*;
    import org.apache.poi.ss.usermodel.*;
    
    import org.apache.poi.ss.format.CellNumberFormatter;
    
    import java.io.*;
    
    import java.math.BigDecimal;
    import java.util.Locale;
    
    class ExcelProcentValuePrecision {
    
     public static void main(String[] args) throws Exception{
    
      InputStream inp = new FileInputStream("ExcelProcentValuePrecision.xlsx");
      Workbook workbook = WorkbookFactory.create(inp);
    
      Cell cell = workbook.getSheetAt(0).getRow(0).getCell(0);
    
      String s = ((XSSFCell)cell).getCTCell().getV();
      System.out.println(s);
    
      BigDecimal bd = new BigDecimal(s);
      System.out.println(bd);
    
      double d = cell.getNumericCellValue();
      System.out.println(d);
    
      Locale.setDefault(Locale.US);
    
      DataFormatter dataformatter = new DataFormatter();
      s = dataformatter.formatCellValue(cell);
      System.out.println(s);
    
      CellNumberFormatter formatter = new CellNumberFormatter("#.#################%");
      s = formatter.format(cell.getNumericCellValue());
      System.out.println(s);
    
      workbook.close();
    
     }
    }
    

    导致:

    所以5.2597614320233098E-2 是直接来自XML 的值。 0.052597614320233098BigDecimal 的值。 0.0525976143202331 是浮点值double根据IEEE floating point5.26% 是格式化为Excel 中显示的值。

    5.25976143202331%Excel 的公式栏中显示的值。百分比值是一个例外情况,因为 Excel 也在公式栏中显示 % 格式,但有效数字的计数(最多 15 个)。

    % 值的例外是因为Excel 中的% 格式不仅是格式,而且是值的更改。 1 = 100%。因此,如果您将100% 放入Excelcell 中,则会存储值1。如果将10% 放入Excel单元格中,则会存储值0.1

    【讨论】:

      【解决方案2】:

      如果是 Aspose.Cells API,请使用以下示例 Excel 文件测试给定的示例代码。另请查看其图像和控制台输出。

      所有这些都说明 Aspose.Cells API 以真正的精度准确地检索单元格 A1 值。

      示例代码

      //Load source workbook
      Workbook wb = new Workbook(dirPath + "sampleExcelCellValuePrecision.xlsx");
      
      //Access first worksheet
      Worksheet ws = wb.getWorksheets().get(0);
      
      //Access cell A1
      Cell a1 = ws.getCells().get("A1");
      
      //The value is double which is shown as
      System.out.println("Double Value: " + a1.getValue());
      
      //This is the value shown by Microsoft Excel
      System.out.println("String Value: " + a1.getStringValue());
      

      控制台输出

      Double Value: 0.0525976143202331
      String Value: 5.26%
      

      注意:我在 Aspose 担任开发人员宣传员

      【讨论】:

        【解决方案3】:

        在这里查看我的答案: https://stackoverflow.com/a/58732907/12332761

        这个函数应该产生你在公式栏中看到的同样的东西:

            private static BigDecimal stringedDouble(Cell cell) {
                    BigDecimal result =  new BigDecimal(String.valueOf(cell.getNumericCellValue())).stripTrailingZeros();
                    result = result.scale() < 0 ? result.setScale(0) : result;
                    return result;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-04-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多