Aspose.Cells功能教程:使用Java以編程方式在Excel中創(chuàng)建數(shù)據(jù)透視表
Excel電子表格中的數(shù)據(jù)透視表用于以交互方式匯總數(shù)據(jù)。假設(shè)在工作表中有許多發(fā)票的數(shù)據(jù)。在這種情況下,可以使用數(shù)據(jù)透視表匯總按客戶或產(chǎn)品分組的發(fā)票。在本文中,將學(xué)習(xí)如何以編程方式處理Excel中的數(shù)據(jù)透視表。特別是,將了解如何在Java中創(chuàng)建數(shù)據(jù)透視表并基于該數(shù)據(jù)透視表生成圖表。
- 使用Java在Excel中創(chuàng)建數(shù)據(jù)透視表
- 使用數(shù)據(jù)透視表生成圖表
為了使用Excel數(shù)據(jù)透視表,我們將使用Aspose.Cells for Java,它是一個功能強大的API,可讓您從Java應(yīng)用程序內(nèi)部生成,修改和轉(zhuǎn)換Excel文件。點擊下方按鈕可下載試用。
使用Java在Excel中創(chuàng)建數(shù)據(jù)透視表
以下是使用Java在Excel中創(chuàng)建數(shù)據(jù)透視表的步驟。
- 使用工作簿類創(chuàng)建一個新的或加載一個現(xiàn)有的Excel文件。
- 用數(shù)據(jù)填充工作表(可選)。
- 使用Worksheet.getPivotTables()方法將數(shù)據(jù)透視表收集到一個PivotTableCollection對象中。
- 使用PivotTableCollection.add(string, string, string)方法添加一個新的透視表,并在PivotTable對象中獲得其引用。
- 設(shè)置選項,如總計、格式化等。
- 使用PivotTable.addFieldToArea(int, int)方法向區(qū)域添加字段。
- 使用Workbook.save(string)方法保存工作簿。
下面的代碼示例演示如何使用Java在Excel中添加數(shù)據(jù)透視表。
// Instantiate an Workbook object Workbook workbook = new Workbook("worksheet.xlsx"); // Access the sheet Worksheet sheet2 = workbook.getWorksheets().get(1); // Get the pivottables collection in the sheet PivotTableCollection pivotTables = sheet2.getPivotTables(); // Add a PivotTable to the worksheet int index = pivotTables.add("=Data!A1:F30", "B3", "PivotTable1"); // Access the instance of the newly added PivotTable PivotTable pivotTable = pivotTables.get(index); // Show the grand totals pivotTable.setRowGrand(true); pivotTable.setColumnGrand(true); // Set the PivotTable report is automatically formatted pivotTable.setAutoFormat(true); // Set the PivotTable autoformat type. pivotTable.setAutoFormatType(PivotTableAutoFormatType.REPORT_6); // Drag the first field to the row area. pivotTable.addFieldToArea(PivotFieldType.ROW, 0); // Drag the third field to the row area. pivotTable.addFieldToArea(PivotFieldType.ROW, 2); // Drag the second field to the row area. pivotTable.addFieldToArea(PivotFieldType.ROW, 1); // Drag the fourth field to the column area. pivotTable.addFieldToArea(PivotFieldType.COLUMN, 3); // Drag the fifth field to the data area. pivotTable.addFieldToArea(PivotFieldType.DATA, 5); // Set the number format of the first data field pivotTable.getDataFields().get(0).setNumber(7); // Save the Excel file workbook.save("pivotTable.xls");
Excel表格
數(shù)據(jù)透視表
使用Java中的Excel數(shù)據(jù)透視表生成圖表
以下是使用Java中的Excel數(shù)據(jù)透視表生成圖表的步驟。
- 使用工作簿類創(chuàng)建一個新的或加載一個現(xiàn)有的Excel文件。
- 填充工作表(可選)。
- 使用Workbook.getWorksheets().add(SheetType.CHART)方法添加一個新的圖表類型的工作表,并在一個工作表對象中獲得其引用。
- 使用Worksheet.getCharts().add()方法添加一個新的圖表,并在Chart對象中獲得其引用。
- 使用Chart.setPivotSource(string)方法將透視表設(shè)置為圖表的數(shù)據(jù)源。
- 使用Workbook.save(string)方法保存工作簿。
下面的代碼示例顯示了如何在Java中使用Excel透視表生成圖表。
// Instantiate an Workbook object Workbook workbook = new Workbook("pivotTable.xls"); // Add a new sheet int sheetIndex = workbook.getWorksheets().add(SheetType.CHART); Worksheet sheet3 = workbook.getWorksheets().get(sheetIndex); // Name the sheet sheet3.setName("PivotChart"); // Add a column chart int chartIndex = sheet3.getCharts().add(ChartType.COLUMN, 0, 5, 28, 16); Chart chart = sheet3.getCharts().get(chartIndex); // Set the pivot chart data source chart.setPivotSource("PivotTable!PivotTable1"); chart.setHidePivotFieldButtons(false); // Save the Excel file workbook.save("pivotChart_test.xls");
如果你想試用Aspose的全部完整功能,可聯(lián)系在線客服獲取30天臨時授權(quán)體驗。
還想要更多嗎?您可以點擊閱讀【Aspose最新資源在線文庫】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術(shù)交流群(761297826),我們很高興為您提供查詢和咨詢。