翻譯|使用教程|編輯:李顯亮|2021-04-27 09:45:00.907|閱讀 254 次
概述:使用數據透視表匯總按客戶或產品分組的發票。在本文中,將學習如何以編程方式處理Excel中的數據透視表。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Excel電子表格中的數據透視表用于以交互方式匯總數據。假設在工作表中有許多發票的數據。在這種情況下,可以使用數據透視表匯總按客戶或產品分組的發票。在本文中,將學習如何以編程方式處理Excel中的數據透視表。特別是,將了解如何在Java中創建數據透視表并基于該數據透視表生成圖表。
為了使用Excel數據透視表,我們將使用Aspose.Cells for Java,它是一個功能強大的API,可讓您從Java應用程序內部生成,修改和轉換Excel文件。點擊下方按鈕可下載試用。
以下是使用Java在Excel中創建數據透視表的步驟。
下面的代碼示例演示如何使用Java在Excel中添加數據透視表。
// 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表格
數據透視表
以下是使用Java中的Excel數據透視表生成圖表的步驟。
下面的代碼示例顯示了如何在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的全部完整功能,可聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn