In some previous posts we have seen how we can create excel and also add various fonts to the excel file using Apache POI.
Now we will see how to add borders in excel using Apache POI.
Simple Borders
Border are added here per cell, so similar to fonts we need to create a CellStyle that has the desired borders and then add that CellStyle to the required cell.
1 2 3 4 5 |
CellStyle style = workbook.createCellStyle(); style.setBorderBottom(BorderStyle.MEDIUM); style.setBorderLeft(BorderStyle.DOUBLE); style.setBorderRight(BorderStyle.THICK); style.setBorderTop(BorderStyle.DASHED); |
In the above code we have added various styles to a single cell style for all the directions of the border.
There are many more styles that can be added to the border. Some of the examples are
1 2 3 4 5 6 7 |
BorderStyle.THIN BorderStyle.THICK BorderStyle.NONE BorderStyle.MEDIUM BorderStyle.DOUBLE BorderStyle.DOTTED BorderStyle.DASHED |
Colors to the Border
We can also add colors to the Border.
We need to add the colors to the specific border on the CellStyle.
1 2 3 4 |
style.setBottomBorderColor(IndexedColors.BLUE.getIndex()); style.setLeftBorderColor(IndexedColors.YELLOW.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BROWN.getIndex()); |
Sample Code: Borders in Excel using Apache POI
Lets see a Sample code where we create a excel file, add a row and cell and then add borders and colors to that cell.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package com.kscodes.sampleproject; import java.io.File; import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class WriteExcelUsingPOI { public static void main(String[] args) { // Create instance of the class and call the writeExcelFile() WriteExcelUsingPOI writeExcelUsingPOI = new WriteExcelUsingPOI(); writeExcelUsingPOI.writeExcelFile("C:\\kscodes\\EmployeeDetails.xlsx"); } public void writeExcelFile(String fileName) { XSSFWorkbook workbook = null; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(new File(fileName)); // Create a Workbook workbook = new XSSFWorkbook(); // Create an Empty Sheet XSSFSheet sheet = workbook.createSheet("Employee Details"); Row row = sheet.createRow(2); Cell cell = row.createCell((short) 2); cell.setCellValue("Border Test - kscodes"); CellStyle style = workbook.createCellStyle(); style.setBorderBottom(BorderStyle.MEDIUM); style.setBorderLeft(BorderStyle.DOUBLE); style.setBorderRight(BorderStyle.THICK); style.setBorderTop(BorderStyle.DASHED); style.setBottomBorderColor(IndexedColors.BLUE.getIndex()); style.setLeftBorderColor(IndexedColors.YELLOW.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BROWN.getIndex()); cell.setCellStyle(style); workbook.write(fileOutputStream); System.out.println("Excel File created and written !!!!"); } catch (Exception e) { System.out.println("An Exception occured while writing Excel" + e); } finally { try { if (fileOutputStream != null) { fileOutputStream.close(); } if (workbook != null) { workbook.close(); } } catch (Exception e) { } } } } |
Output