隐藏数据透视表数据!Java版Excel文档处理控件Aspose.Cells这些不得不看的新功能
Aspose.Cells for Java(点击下载)是Excel电子表格处理API,开发人员可以在其自己的应用程序中嵌入读取,编写,操作,转换和打印电子表格的功能,而无需Microsoft Excel应用程序。
在最新发布的v19.11中,新增了许多非常有趣且实用的新功能,例如提供基于某些条件的排序和隐藏数据来获取报表和演示的紧凑信息,可以增强数据透视表的处理;使用QueryTable作为数据源读写表;检索OData连接信息等等。接下来我们用示例来演示如何实现这些新功能。
在数据透视表中排序和隐藏数据
在某些情况下,数据透视表中需要清晰的信息。我们可能需要某些功能来对数据透视表中的数据进行排序,然后根据某些条件隐藏行。以下示例代码演示了此功能:
// The path to the output directory. String sourceDir = Utils.Get_SourceDirectory(); String outputDir = Utils.Get_OutputDirectory(); Workbook workbook = new Workbook(sourceDir + "PivotTableHideAndSortSample.xlsx"); Worksheet worksheet = workbook.getWorksheets().get(0); PivotTable pivotTable = worksheet.getPivotTables().get(0); CellArea dataBodyRange = pivotTable.getDataBodyRange(); int currentRow = 3; int rowsUsed = dataBodyRange.EndRow; // Sorting score in descending PivotField field = pivotTable.getRowFields().get(0); field.setAutoSort(true); field.setAscendSort(false); field.setAutoSortField(0); pivotTable.refreshData(); pivotTable.calculateData(); // Hiding rows with score less than 60 while (currentRow < rowsUsed) { Cell cell = worksheet.getCells().get(currentRow, 1); double score = (double) cell.getValue(); if (score < 60) { worksheet.getCells().hideRow(currentRow); } currentRow++; } pivotTable.refreshData(); pivotTable.calculateData(); // Saving the Excel file workbook.save(outputDir + "PivotTableHideAndSort_out.xlsx");
下图显示了对示例数据运行此代码之前和之后的数据透视表。
带有查询表数据源的读写表
具有查询表作为数据源的表非常常见。我们可能需要阅读这些表并进行修改,例如显示总数等。此功能较早可用,但是提供了对XLS文件的支持。以下示例代码读取一个表,然后对其进行更改以在末尾显示总数。
// The path to the output directory. String sourceDir = Utils.Get_SourceDirectory(); String outputDir = Utils.Get_OutputDirectory(); // Load workbook object Workbook workbook = new Workbook(sourceDir + "SampleTableWithQueryTable.xls"); Worksheet worksheet = workbook.getWorksheets().get(0); ListObject table = worksheet.getListObjects().get(0); // Check the data source type if it is query table if (table.getDataSourceType() == TableDataSourceType.QUERY_TABLE) { table.setShowTotals(true); } // Save the file workbook.save(outputDir + "SampleTableWithQueryTable_out.xls");
下图显示了setShowTotals()函数的工作方式:
获取OData连接信息
OData可用于从RESTful API获取提要或数据,也可在Excel文件中使用。可以使用Apose.Cells API和Workbook的DataMashup类从Excel文件中检索此信息。从具有PowerQueryFormula和PowerQueryFormulaItem的PowerQueryFormulas属性中获取所需的信息。
// The path to the directories. String sourceDir = Utils.Get_SourceDirectory(); Workbook workbook = new Workbook(sourceDir + "ODataSample.xlsx"); PowerQueryFormulaCollction PQFcoll = workbook.getDataMashup().getPowerQueryFormulas(); for (Object obj : PQFcoll) { PowerQueryFormula PQF = (PowerQueryFormula)obj; System.out.println("Connection Name: " + PQF.getName()); PowerQueryFormulaItemCollection PQFIcoll = PQF.getPowerQueryFormulaItems(); for (Object obj2 : PQFIcoll) { PowerQueryFormulaItem PQFI = (PowerQueryFormulaItem)obj2; System.out.println("Name: " + PQFI.getName()); System.out.println("Value: " + PQFI.getValue()); } }
下图显示了一个示例文件,该文件在上面的代码示例中用于获取OData连接信息。
这是使用示例文件的程序输出:
还想要更多吗?如果您有下载或购买需求,请随时加入Aspose技术交流群(642018183),我们很高兴为您提供查询和咨询。