使用边框线创建范围的表格
Contents
[
Hide
]
有时,您希望根据您拥有的单元格地址为Range/CellArea添加边框线创建表。您可以使用Cells.createRange方法创建一系列单元格。Cells.createRange方法返回一个Range对象。您可以创建一个Style对象,并据此规定上、左、下、右的边框选项。稍后,您可以获取Range的单元格,并对单元格应用您期望的格式。
以下示例演示如何创建一个Range并为范围单元格指定边框线。
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文件;以下是文件的屏幕截图。