【发布时间】:2021-12-27 03:25:02
【问题描述】:
我能够单独使用 apache POI 创建数据透视表和数据透视图。但我正在尝试创建柱形图表单数据透视表,而不是直接从工作表数据。我试过在这里寻找指导,但找不到任何指导。如果可能的话,你能指导我正确的方向吗?谢谢。
更新
下面是我之前的示例代码,它在一个工作表中填充数据,然后使用第一个工作表中填充的数据在另一个工作表中创建数据透视表和数据透视图。
但我希望将数据透视表作为数据透视图的来源,而不是来自其他工作表的原始数据。这样我可以动态隐藏某些记录。例如,如果我需要在图表中只显示亚洲国家,我可以在数据透视表中过滤它们,这将只显示图表中的那些国家。我不知道如何才能做到这一点。
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.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class ColumnChart {
public static void main(String[] args) {
String workbookName = "Population.xlsx";
String dataSheetName = "DataSheet";
String pivotSheetName = "PivotSheet";
try (XSSFWorkbook workbook = new XSSFWorkbook();
FileOutputStream fos = new FileOutputStream(workbookName)) {
prepareDataSheet(workbook, dataSheetName);
createPivotTable(workbook, pivotSheetName, dataSheetName);
createPivotChart(workbook, pivotSheetName, dataSheetName);
workbook.write(fos);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void prepareDataSheet(XSSFWorkbook workbook, String dataSheetName) {
List<String> continentList = Arrays.asList("Asia", "Asia", "America", "Asia", "Asia", "America", "Africa", "Asia");
List<String> countryList = Arrays.asList("China", "India", "United States", "Indonesia", "Pakistan", "Brazil", "Nigeria", "Bangladesh");
List<Long> populationList = Arrays.asList(1411778724L, 1386020955L, 332943364L, 271350000L, 225200000L, 214130142L, 211401000L, 171933178L);
XSSFSheet dataSheet = workbook.createSheet(dataSheetName);
Row row = dataSheet.createRow(0);
row.createCell(0).setCellValue("Continent");
row.createCell(1).setCellValue("Country");
row.createCell(2).setCellValue("Population");
int rowCounter = 1;
for (rowCounter = 1; rowCounter <= countryList.size(); rowCounter++) {
row = dataSheet.createRow(rowCounter);
row.createCell(0).setCellValue(continentList.get(rowCounter - 1));
row.createCell(1).setCellValue(countryList.get(rowCounter - 1));
row.createCell(2).setCellValue(populationList.get(rowCounter - 1));
}
}
private static void createPivotTable(XSSFWorkbook workbook, String pivotSheetName, String dataSheetName) {
XSSFSheet pivotSheet = workbook.createSheet(pivotSheetName);
XSSFSheet dataSheet = workbook.getSheet(dataSheetName);
CellReference leftTop = new CellReference(0, 0);
CellReference rightBottom = new CellReference(8, 2);
CellReference pivotLocation = new CellReference(1, 1);
AreaReference sourceDataAreaRef = new AreaReference(leftTop, rightBottom, SpreadsheetVersion.EXCEL2007);
XSSFPivotTable pivotTable = pivotSheet.createPivotTable(sourceDataAreaRef, pivotLocation, dataSheet);
pivotTable.addRowLabel(0);
pivotTable.addRowLabel(1);
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 2);
}
private static void createPivotChart(XSSFWorkbook workbook, String pivotSheetName, String dataSheetName) {
XSSFSheet pivotSheet = workbook.getSheet(pivotSheetName);
XSSFSheet dataSheet = workbook.getSheet(dataSheetName);
XSSFDrawing drawing = pivotSheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 4, 2, 10, 20);
XSSFChart chart = drawing.createChart(anchor);
chart.setTitleText("Population By Countries");
chart.setTitleOverlay(false);
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.BOTTOM);
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle("Country");
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle("Population");
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
XDDFDataSource<String> countries = XDDFDataSourcesFactory.fromStringCellRange(dataSheet,
new CellRangeAddress(0, 8, 1, 1));
XDDFNumericalDataSource<Double> values = XDDFDataSourcesFactory.fromNumericCellRange(dataSheet,
new CellRangeAddress(0, 8, 2, 2));
XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
XDDFChartData.Series series1 = data.addSeries(countries, values);
series1.setTitle("Country", null);
data.setVaryColors(false);
XDDFBarChartData bar = (XDDFBarChartData) data;
bar.setBarDirection(BarDirection.COL);
CTSolidColorFillProperties fillProp = CTSolidColorFillProperties.Factory.newInstance();
CTSRgbColor rgb = CTSRgbColor.Factory.newInstance();
rgb.setVal(new byte[]{(byte) 233, (byte) 87, (byte)162});
fillProp.setSrgbClr(rgb);
CTShapeProperties ctShapeProperties = CTShapeProperties.Factory.newInstance();
ctShapeProperties.setSolidFill(fillProp);
chart.getCTChart().getPlotArea().getBarChartList().get(0).getSerList().get(0).setSpPr(ctShapeProperties);
chart.plot(data);
}
}
【问题讨论】:
-
嗨乔纳森 - 欢迎来到 Stackoverflow。这个问题不遵循 stackoverflow 指导方针 - stackoverflow.com/help/how-to-ask。您需要证明您已经做出了自己的一些编码工作。甚至尝试谷歌搜索。这里有一些链接可以帮助您入门 - poi.apache.org/components/spreadsheet/examples.html - stackoverflow.com/questions/60887605/…
标签: excel apache-poi pivot-table pivot-chart