Aspose.Cells for Java(点击下载)是Excel电子表格处理API,开发人员能够在其本身的应用程序中嵌入读取,编写,操做,转换和打印电子表格的功能,而无需Microsoft Excel应用程序。less
在最新发布的v19.11中,新增了许多很是有趣且实用的新功能,例如提供基于某些条件的排序和隐藏数据来获取报表和演示的紧凑信息,能够加强数据透视表的处理;使用QueryTable做为数据源读写表;检索OData链接信息等等。接下来咱们用示例来演示如何实现这些新功能。ide
在某些状况下,数据透视表中须要清晰的信息。咱们可能须要某些功能来对数据透视表中的数据进行排序,而后根据某些条件隐藏行。如下示例代码演示了此功能:函数
// 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");
下图显示了对示例数据运行此代码以前和以后的数据透视表。spa
查看上述代码片断中使用的源和输出excel文件,请点击这里下载。excel
具备查询表做为数据源的表很是常见。咱们可能须要阅读这些表并进行修改,例如显示总数等。此功能较早可用,可是提供了对XLS文件的支持。如下示例代码读取一个表,而后对其进行更改以在末尾显示总数。orm
// 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()函数的工做方式:blog
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),咱们很高兴为您提供查询和咨询。