1

How to add Borders for all four sides for a range of dynamically generated cells using java in XSSFWorkbook .

XSSFSheet objSheet = objWorkbook.getSheetAt(0);
objSheet.shiftRows(5, objSheet.getLastRowNum() + 1, 10, true, true);

I have used this code. I am getting cells generated but i want to add all side borders for the generated cells.

KishanCS
  • 1,357
  • 1
  • 19
  • 38
  • Please go through this might help: http://stackoverflow.com/questions/5720049/add-borders-to-cells-in-poi-generated-excel-file – Sumit Sagar Dec 01 '16 at 06:24
  • 1
    I tried style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM); style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM); style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM); style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM); but it is only to set for a particular cell. but i need it for a range .(Not setting each cell value manually ) – KishanCS Dec 01 '16 at 06:42
  • style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); using xssf document – KishanCS Dec 01 '16 at 06:45

1 Answers1

1

Take a look at http://poi.apache.org/spreadsheet/quick-guide.html#Borders, specifically

// Create a cell and put a value in it.
Cell cell = row.createCell(1);
cell.setCellValue(4);

// Style the cell with borders all around.
CellStyle style = wb.createCellStyle();
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.BLUE.getIndex());
style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
cell.setCellStyle(style);

If you want to have it for more cells you will need to iterate over them and apply the style to each of those.

Make sure to apply the same style-object to as many cells as possible, the Excel format has limits on how many styles you can have in one file.

centic
  • 15,565
  • 9
  • 68
  • 125