使用范围的边界线创建表格
Contents
[
Hide
]
有时,您想通过为范围/单元格区域根据您拥有的单元格的地址。您可以使用[Cells.createRange](https://reference.aspose.com/cells/java/com.aspose.cells/cells#createRange(int,%20int,%20boolean) 方法来创建一系列单元格。这Cells.createRange 方法返回一个范围目的。您可以创建一个风格对象并相应地指定边框(上、左、下、右)选项。稍后,您可能会得到范围并将所需的格式应用于单元格。
下面的例子展示了如何创建一个范围并指定范围单元格的边界线。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getDataDir(CreateTableforRange.class); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the newly added worksheet | |
int sheetIndex = workbook.getWorksheets().add(); | |
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Creating a range of cells based on cells Address. | |
Range range = worksheet.getCells().createRange("A1:F10"); | |
// Specify a Style object for borders. | |
Style style = cell.getStyle(); | |
// Setting the line style of the top border | |
style.setBorder(BorderType.TOP_BORDER, CellBorderType.THICK, Color.getBlack()); | |
// Setting the line style of the bottom border | |
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.THICK, Color.getBlack()); | |
// Setting the line style of the left border | |
style.setBorder(BorderType.LEFT_BORDER, CellBorderType.THICK, Color.getBlack()); | |
// Setting the line style of the right border | |
style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.THICK, Color.getBlack()); | |
Iterator cellArray = range.iterator(); | |
while (cellArray.hasNext()) { | |
Cell temp = (Cell) cellArray.next(); | |
// Saving the modified style to the cell. | |
temp.setStyle(style); | |
} | |
// Saving the Excel file | |
workbook.save(dataDir + "borders_out.xls"); |
运行上述代码后,我们可以生成包含格式化表格的excel文件;这是文件的屏幕截图。