代码之家  ›  专栏  ›  技术社区  ›  hotmeatballsoup

在apachepoi中应用价格和百分比格式

  •  0
  • hotmeatballsoup  · 技术社区  · 6 年前

    这里是Java8和ApachePOI4.1.x。

    Excel的预期“单元格格式行为”如下所示,这是我作为Excel用户的大部分时间收集到的:

    • 似乎有一个潜在的 一个细胞,然后是 可视化
    • 内部值是一个数字、值等,即该单元格的实际/实际值
    • 可视化是一种 可选择的 向最终用户表示内部值的方式
      • 示例:单元格的内部值可能是 0.678261 ,但该单元格的格式可能是将小数作为百分数处理,精度为百分之一,因此最终用户可能会看到该单元格表示为 67.83% . 但是如果他们在公式中使用它,或者修改它的值,那么

    我正在想办法用POI做同样的事。

    也就是说,我想使用POI的API将一个内部值写入一个单元格,然后将该单元格配置为(直观地)用不同的格式表示该值。

    我的两个用例是:

    1. 203.9483495949 作为 $203.95 最终用户,或 0.8375733 作为 $0.84
    2. 以有效百分比表示数字/小数(例如,直观地表示 0.009383484 作为 1.00% 最终用户,或 0.53282 作为 53.28%

    目前,我将这些值编写如下:

    BigDecimal validPrice = BigDecimal.valueOf(203.9483495949);
    BigDecimal validPct = BigDecimal.valueOf(0.53282);
    
    Row nextRow = sheet.createRow(rowNum);
    
    nextRow.createCell(0).setCellValue(validPrice.doubleValue());
    nextRow.createCell(1).setCellValue(validPct.doubleValue());
    

    但当我将数据写入Excel文件时,我只看到那些相同的原始/内部值( 203.9483495949 分别)列中,我必须 手动 打开文件后设置格式。相反,我希望POI在代码中应用格式,而不是强制最终用户手动应用此格式,以便在打开文件时,将其格式化为 53.28% 阿莱迪。

    0 回复  |  直到 6 年前
        1
  •  2
  •   Axel Richter    6 年前

    Quick-guide - DataFormats 您需要使用单元格样式来格式化 Excel 具有特殊数字格式的单元格。

    那些 CellStyle 在工作簿级别,可以根据需要创建,如上面链接的示例所示。但你不应该创造完全相同的 蜂窝式 limits for unique cell formats/cell styles 在里面 擅长

    更灵活的方法是使用 CellUtil . 在那里:

    处理样式的各种方法允许您创建 需要。如果没有,那么它将创建一个新样式。这是为了防止 创建的样式太多。

    使用它,我们可以创建一个结构,其中包含每行和每单元格的数据对象,以及一个结构,其中包含每列的数据格式。

    import java.io.FileOutputStream;
    
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.ss.util.CellUtil;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    class CreateExcelFormattedValues {
    
     public static void main(String[] args) throws Exception {
    
      Workbook workbook = new XSSFWorkbook();
      DataFormat format = workbook.createDataFormat();
    
      // structure which holds data objects per row and cell 
      Object[][] data = new Object[][]{
       new Object[]{"Price", "Percent"},
       new Object[]{203.9483495949, 0.53282},
       new Object[]{0.8375733, 0.009383484}
      };
    
      // structure which holds data formats per column
      short currencyDataFormat = format.getFormat("$#,##0.00");
      short percentDataFormat = format.getFormat("0.00%");
      short[] dataFormats = {currencyDataFormat, percentDataFormat};
    
      Sheet sheet = workbook.createSheet();
      Row row;
      int firstRow = 1; // first row is second row
      int r = 0; // loop variable for row
      Cell cell;
      int firstCol = 1; // first column is column B
      int c = 0; // loop variable for column
      for (Object[] dataRow : data) {
       row = sheet.createRow(firstRow + r);
       c = 0;
       for (Object dataValue : dataRow) {
        cell = row.createCell(firstCol + c);
        CellUtil.setCellStyleProperty(cell, CellUtil.DATA_FORMAT, dataFormats[c]);
        if (dataValue instanceof String) {
         cell.setCellValue((String)dataValue);
        } else if (dataValue instanceof Double) {
         cell.setCellValue((Double)dataValue);
        }
        c++;
       }
       r++;
      }
    
      FileOutputStream out = new FileOutputStream("Excel.xlsx");
      workbook.write(out);
      out.close();
      workbook.close();
    
     }
    }