如何使用Apache POI设置数据透视表字段格式单元格

我想设置数据透视表数值格式的单元格 的平衡总和# ##0

使用基于官方POI示例CreatePivotTable的代码创build的数据透视表

下面的代码createget CTPivotField pivotField 。 但如何设置其数字格式?

 pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 2); CTPivotField pivotField = pivotTable .getCTPivotTableDefinition() .getPivotFields() .getPivotFieldArray(2); 

在MS Excel中,这是通过接下来的步骤(见截图):

  1. 右键单击平衡数据透视表值的总和
  2. select字段设置
  3. 点击号码…
  4. 设置格式单元格

帮助请与决定,build议或任何想法。

使用Microsoft Excel设置数据透视表的数字格式单元格

数据透视表字段的格式由值为 CTPivotField.setNumFmtId(long numFmtId) ,列和行CTDataField.setNumFmtId(long numFmtId)设置。

numFmtId是格式代码的ID号。 可用格式代码在格式单元格列表中显示 – 自定义类别: 在这里输入图像说明 预定义的格式代码,感谢Ji Zhou – MSFT ,在这里:

 1 0 2 0.00 3 #,##0 4 #,##0.00 5 $#,##0_);($#,##0) 6 $#,##0_);[Red]($#,##0) 7 $#,##0.00_);($#,##0.00) 8 $#,##0.00_);[Red]($#,##0.00) 9 0% 10 0.00% 11 0.00E+00 12 # ?/? 13 # ??/?? 14 m/d/yyyy 15 d-mmm-yy 16 d-mmm 17 mmm-yy 18 h:mm AM/PM 19 h:mm:ss AM/PM 20 h:mm 21 h:mm:ss 22 m/d/yyyy h:mm 37 #,##0_);(#,##0) 38 #,##0_);[Red](#,##0) 39 #,##0.00_);(#,##0.00) 40 #,##0.00_);[Red](#,##0.00) 45 mm:ss 46 [h]:mm:ss 47 mm:ss.0 48 ##0.0E+0 49 @ 

MSDN中NumberingFormat类的预定义格式代码的完整列表

这里是一个应用格式数据透视表字段的例子:

 package ru.inkontext.poi; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.usermodel.DataConsolidateFunction; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.usermodel.XSSFPivotTable; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataFields; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.Optional; public class CreatePivotTableSimple { private static void setFormatPivotField(XSSFPivotTable pivotTable, long fieldIndex, Integer numFmtId) { Optional.ofNullable(pivotTable .getCTPivotTableDefinition() .getPivotFields()) .map(pivotFields -> pivotFields .getPivotFieldArray((int) fieldIndex)) .ifPresent(pivotField -> pivotField .setNumFmtId(numFmtId)); } private static void setFormatDataField(XSSFPivotTable pivotTable, long fieldIndex, long numFmtId) { Optional.ofNullable(pivotTable .getCTPivotTableDefinition() .getDataFields()) .map(CTDataFields::getDataFieldList) .map(List::stream) .ifPresent(stream -> stream .filter(dataField -> dataField.getFld() == fieldIndex) .findFirst() .ifPresent(dataField -> dataField.setNumFmtId(numFmtId))); } public static void main(String[] args) throws IOException, InvalidFormatException { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); //Create some data to build the pivot table on setCellData(sheet); XSSFPivotTable pivotTable = sheet.createPivotTable( new AreaReference("A1:C6", SpreadsheetVersion.EXCEL2007), new CellReference("E3")); pivotTable.addRowLabel(1); // set second column as 1-th level of rows setFormatPivotField(pivotTable, 1, 9); //set format of row field numFmtId=9 0% pivotTable.addRowLabel(0); // set first column as 2-th level of rows pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 2); // Sum up the second column setFormatDataField(pivotTable, 2, 3); //set format of value field numFmtId=3 # ##0 FileOutputStream fileOut = new FileOutputStream("stackoverflow-pivottable.xlsx"); wb.write(fileOut); fileOut.close(); wb.close(); } private static void setCellData(XSSFSheet sheet) { String[] names = {"Jane", "Tarzan", "Terk", "Kate", "Dmitry"}; Double[] percents = {0.25, 0.5, 0.75, 0.25, 0.5}; Integer[] balances = {107634, 554234, 10234, 22350, 15234}; Row row = sheet.createRow(0); row.createCell(0).setCellValue("Name"); row.createCell(1).setCellValue("Percents"); row.createCell(2).setCellValue("Balance"); for (int i = 0; i < names.length; i++) { row = sheet.createRow(i + 1); row.createCell(0).setCellValue(names[i]); row.createCell(1).setCellValue(percents[i]); row.createCell(2).setCellValue(balances[i]); } } } 

https://github.com/stolbovd/PoiSamples