Créer un tableau en utilisant des lignes de bordure pour une plage
Contents
[
Hide
]
Parfois, vous voulez créer un tableau en ajoutant des lignes de bordure pour une Plage/Zone de cellules en fonction de l’adresse des cellules que vous avez. Vous pouvez utiliser la méthode Cells.createRange pour créer une plage de cellules. La méthode Cells.createRange renvoie un objet Range. Vous pouvez créer un objet Style et spécifier les options de bordures (haut, gauche, bas, droite) en conséquence. Plus tard, vous pouvez obtenir les cellules de la Range et appliquer la mise en forme souhaitée aux cellules.
L’exemple suivant montre comment créer un Range et spécifier les limites pour les cellules de la plage.
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"); |
Après avoir exécuté le code ci-dessus, nous pouvons obtenir le fichier Excel généré contenant le tableau formaté; voici la capture d’écran du fichier.