公共 API Aspose.Cells 8.4.2 的变化

添加的 API

改进的图表创建机制

com.aspose.cells.charts.Chart 类公开了 setChartDataRange 方法以简化图表创建任务。 setChartDataRange 方法接受两个参数,其中第一个参数是字符串类型,指定要从中绘制数据系列的单元格区域。第二个参数是指定绘图方向的布尔类型,即;是否按行或按列绘制一系列单元格值的图表数据系列。

以下代码片段展示了如何使用几行代码创建柱形图,假设图表的绘图系列数据存在于从单元格 A1 到 D4 的同一工作表中。

Java

 //Add a new chart of type Column to chart collection

int idx = worksheet.getCharts().add(ChartType.COLUMN, 6, 5, 20, 13);

//Retrieve the newly added chart instance

Chart chart = worksheet.getCharts().get(idx);

//Specify the chart's data series from cell A1 to D4

chart.setChartDataRange("A1:D4", true);

方法 VbaModuleCollection.add 添加

Aspose.Cells for Java 8.4.2 公开了 VbaModuleCollection.add 方法以将新的 VBA 模块添加到工作簿的实例。 VbaModuleCollection.add 方法接受工作表类型的参数以添加工作表特定模块。

以下代码片段显示了如何使用 VbaModuleCollection.add 方法。

Java

 //Create new workbook

Workbook workbook = new Workbook();

//Access first worksheet

Worksheet worksheet = workbook.getWorksheets().get(0);

//Add VBA module

int idx = workbook.getVbaProject().getModules().add(worksheet);

//Access the VBA Module, set its name and code

VbaModule module = workbook.getVbaProject().getModules().get(idx);

module.setName("TestModule");

module.setCodes("Sub ShowMessage()" + "\r\n" +

"    MsgBox \"Welcome to Aspose!\"" + "\r\n" +

"End Sub");

//Save the workbook

workbook.save(output, SaveFormat.XLSM);

重载方法 Cells.copyColumns 添加

Aspose.Cells for Java 8.4.2 公开了 Cells.copyColumns 方法的重载版本以将源列重复到目标。新暴露的方法一共接受5个参数,其中前4个参数与普通的Cells.copyColumns方法相同。但是,最后一个 int 类型的参数指定了源列必须重复的目标列数。

以下代码片段显示了如何使用新公开的 Cells.copyColumns 方法。

Java

 //Load an existing workbook

Workbook workbook = new Workbook(input);

//Access first worksheet

Worksheet worksheet = workbook.getWorksheets().get(0);

//Access cells of first worksheet

Cells cells = worksheet.getCells();

//Copy the first two columns (A & B) along with formatting

//to columns G, H & I.

//Please note, the columns G & H will be replaced by A & B respectively

//whereas, column I will be replaced by the column A

cells.copyColumns(cells, 0, 2, 6, 3);

//Save the workbook

workbook.save(output);

添加了枚举字段 PasteType.DEFAULT 和 PasteType.ALL_EXCEPT_BORDERS

随着 v8.4.2 的发布,Aspose.Cells API 为 PasteType 添加了 2 个新的枚举字段,详述如下。

  • PasteType.DEFAULT:类似于 Excel 的“全部”功能,用于粘贴单元格范围。
  • 粘贴类型.ALL_除了_BORDERS:类似于 Excel 的“除边框外的所有内容”功能,用于粘贴单元格范围。

以下示例代码演示了 PasteType.DEFAULT 字段的使用。

Java

 //Load an existing workbook

Workbook workbook = new Workbook(input);

//Access first worksheet

Worksheet worksheet = workbook.getWorksheets().get(0);

//Access cells of first worksheet

Cells cells = worksheet.getCells();

//Create source & destination ranges

Range source = cells.createRange("A1:B6");

Range destination = cells.createRange("D1:E6");

//Create an instance of PasteOptions and set its PasteType property

PasteOptions options = new PasteOptions();

options.setPasteType(PasteType.DEFAULT);

//Copy the source range onto the destination range with everything except column widths

destination.copy(source, options);

//Save the workbook

workbook.save(output);