仅复制范围数据和样式

复制具有样式的区域数据

Aspose.Cells 提供了一系列用于处理范围的类和方法,例如 createRange()StyleFlagapplyStyle(),等等。

此示例:

  1. 创建一个工作簿。
  2. 在第一个工作表中填充多个单元格的数据。
  3. 创建一个范围。
  4. 创建具有指定格式属性的样式对象。
  5. 将样式应用到数据范围。
  6. 创建第二个单元格范围。
  7. 将第一个范围的带有格式的数据复制到第二个范围。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(CopyRangeDataWithStyle.class);
// Instantiate a new Workbook
Workbook workbook = new Workbook();
// Get the first Worksheet Cells
Cells cells = workbook.getWorksheets().get(0).getCells();
// Fill some sample data into the cells
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 10; j++) {
cells.get(i, j).putValue(i + "," + j);
}
}
// Create a range (A1:D3).
Range range = cells.createRange("A1", "D3");
// Create a style object.
Style style = workbook.createStyle();
// Specify the font attribute.
style.getFont().setName("Calibri");
// Specify the shading color.
style.setForegroundColor(Color.getYellow());
style.setPattern(BackgroundType.SOLID);
// Specify the border attributes.
style.getBorders().getByBorderType(BorderType.TOP_BORDER).setLineStyle(CellBorderType.THIN);
style.getBorders().getByBorderType(BorderType.TOP_BORDER).setColor(Color.getBlue());
style.getBorders().getByBorderType(BorderType.BOTTOM_BORDER).setLineStyle(CellBorderType.THIN);
style.getBorders().getByBorderType(BorderType.BOTTOM_BORDER).setColor(Color.getBlue());
style.getBorders().getByBorderType(BorderType.LEFT_BORDER).setLineStyle(CellBorderType.THIN);
style.getBorders().getByBorderType(BorderType.LEFT_BORDER).setColor(Color.getBlue());
style.getBorders().getByBorderType(BorderType.RIGHT_BORDER).setLineStyle(CellBorderType.THIN);
style.getBorders().getByBorderType(BorderType.RIGHT_BORDER).setColor(Color.getBlue());
// Create the styleflag object.
StyleFlag flag = new StyleFlag();
// Implement font attribute
flag.setFontName(true);
// Implement the shading / fill color.
flag.setCellShading(true);
// Implment border attributes.
flag.setBorders(true);
// Set the Range style.
range.applyStyle(style, flag);
// Create a second range (L9:O11)
Range range2 = cells.createRange("L9", "O11");
// Copy the range data with formatting.
range2.copy(range);
// Save the Excel file.
workbook.save(dataDir + "CopyRangeDataWithFormatting.xlsx", SaveFormat.XLSX);