首页 文章

优于单元格值精度

提问于
浏览
0

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

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

使用Apache POI,我能够检索:

值1,使用cell.getNumericValue();

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

但是,我无法检索值3,这是公式栏上显示的值,plz建议检索相同的方法 .

2 回答

  • 0

    我无法确认您的观察结果 .

    如果我有以下Excel:

    enter image description here

    正如您在 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();
    
     }
    }
    

    导致:

    enter image description here

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

    5.25976143202331%Excel 公式栏中显示的值 . 百分比值是一种特殊情况,因为 Excel 也在公式栏中显示 % 格式,但完全计算有效数字(最多15个) .

    带有 % 值的异常是因为 Excel 中的 % 格式不仅是格式化,而且还是值的更改 . 1 = 100% . 因此,如果将 100% 放在 Excel 单元格中,则会存储值 1 . 如果将 10% 放在 Excel 单元格中,则会存储值 0.1 .

  • 1

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

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

    Screenshot

    Sample Code

    //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());
    

    Console Output

    Double Value: 0.0525976143202331
    String Value: 5.26%
    

    Note: 我在Aspose担任开发人员传播者

相关问题