Excel处理开发工具Aspose.Cells功能演示:使用Java以编程方式在Excel中创建数据透视表

使用数据透视表汇总按客户或产品分组的发票。在本文中,将学习如何以编程方式处理Excel中的数据透视表。

Excel电子表格中的数据透视表用于以交互方式汇总数据。假设在工作表中有许多发票的数据。在这种情况下,可以使用数据透视表汇总按客户或产品分组的发票。在本文中,将学习如何以编程方式处理Excel中的数据透视表。特别是,将了解如何在Java中创建数据透视表并基于该数据透视表生成图表。

  • 使用Java在Excel中创建数据透视表
  • 使用数据透视表生成图表

为了使用Excel数据透视表,我们将使用

使用Java在Excel中创建数据透视表

以下是使用Java在Excel中创建数据透视表的步骤。

  • 使用工作簿类创建一个新的或加载一个现有的Excel文件。
  • 用数据填充工作表(可选)。
  • 使用Worksheet.getPivotTables()方法将数据透视表收集到一个PivotTableCollection对象中。
  • 使用PivotTableCollection.add(string, string, string)方法添加一个新的透视表,并在PivotTable对象中获得其引用。
  • 设置选项,如总计、格式化等。
  • 使用PivotTable.addFieldToArea(int, int)方法向区域添加字段。
  • 使用Workbook.save(string)方法保存工作簿。

下面的代码示例演示如何使用Java在Excel中添加数据透视表。

// Instantiate an Workbook objectWorkbook workbook = new Workbook("worksheet.xlsx");// Access the sheetWorksheet sheet2 = workbook.getWorksheets().get(1);// Get the pivottables collection in the sheetPivotTableCollection pivotTables = sheet2.getPivotTables();// Add a PivotTable to the worksheetint index = pivotTables.add("=Data!A1:F30", "B3", "PivotTable1");// Access the instance of the newly added PivotTablePivotTable pivotTable = pivotTables.get(index);// Show the grand totalspivotTable.setRowGrand(true);pivotTable.setColumnGrand(true);// Set the PivotTable report is automatically formattedpivotTable.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 fieldpivotTable.getDataFields().get(0).setNumber(7);// Save the Excel fileworkbook.save("pivotTable.xls");

Excel表格

Excel处理开发工具Aspose.Cells功能演示:使用Java以编程方式在Excel中创建数据透视表

数据透视表

Excel处理开发工具Aspose.Cells功能演示:使用Java以编程方式在Excel中创建数据透视表

使用Java中的Excel数据透视表生成图表

以下是使用Java中的Excel数据透视表生成图表的步骤。

  • 使用工作簿类创建一个新的或加载一个现有的Excel文件。
  • 填充工作表(可选)。
  • 使用Workbook.getWorksheets().add(SheetType.CHART)方法添加一个新的图表类型的工作表,并在一个工作表对象中获得其引用。
  • 使用Worksheet.getCharts().add()方法添加一个新的图表,并在Chart对象中获得其引用。
  • 使用Chart.setPivotSource(string)方法将透视表设置为图表的数据源。
  • 使用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的全部完整功能,可联系在线客服获取30天临时授权体验。


还想要更多吗可以点击阅读【Aspose最新资源在线文库】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年3月24日
下一篇 2021年3月24日

相关推荐

发表回复

登录后才能评论