Reputation: 513
So in my java class I have the following code.
int rowNum = 11;
Row myRow = null;
Cell myCell = null;
for (Object obj : details) {
Object[] objArr = (Object[]) obj;
String header = "";
String value = "";
if (objArr[0] != null) {
header = objArr[0].toString();
myRow = sheet.createRow((short) rowNum);
myCell = myRow.createCell(1);
myCell.setCellValue(header);
}
if (objArr[1] != null) {
value = objArr[1].toString();
myCell = myRow.createCell(6);
myCell.setCellValue(value);
myCell.setCellStyle(style);
}
rowNum++;
}
and for border
for (int i = 0; i < objArr.length; i++) {
Cell columnHeaderCell = myRow.createCell(i);
columnHeaderCell.setCellValue((Double) objArr[i]);
columnHeaderCell.setCellStyle(columnHeaderStyle);
}
The problem is the border is over lapping on top of the data and is creating border for all the cells. How do i create a 2 X 10*(x) table dynamically in java?
Upvotes: 1
Views: 2258
Reputation: 513
I achieved it by doing the following. Getting the data's as object. So for empty cells, validating for object!=null.
My Code:
int rowNum = 1;
Row myRow = null;
Row emptyRow = null;
Cell myCell = null;
Cell emptyCell =null;
for (Object obj : details) {
Object[] objArr = (Object[]) obj;
String header = "";
String value = "";
if(objArr[0]==null){
emptyRow = sheet.createRow((short) rowNum);
emptyCell = emptyRow.createCell(0);
emptyCell.setCellValue("");
emptyCell.setCellStyle(style);
}
if(objArr[1]==null){
emptyCell = emptyRow.createCell(1);
emptyCell.setCellValue(value);
emptyCell.setCellStyle(style);
}
For building Rows dynamically with borders:
Building Header Column:
if (objArr[0] != null) {
header = objArr[0].toString();
myRow = sheet.createRow((short) rowNum);
myCell = myRow.createCell(0);
for (int i = 0; i < 2; i++) {
Cell columnHeaderCell = myRow.createCell(i);
columnHeaderCell.setCellValue("");
columnHeaderCell.setCellStyle(style);
}
}
For building Value Column:
if (objArr[1] != null) {
value = objArr[1].toString();
myCell = myRow.createCell(1);
myCell.setCellValue(value);
myCell.setCellStyle(style);
}
rowNum++;
}
The above parenthesis is for closing for each
loop.
For Border
HSSFCellStyle style=wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
Upvotes: 1
Reputation: 2209
Using Apache POI the borders can be set as below.
Find below sample, which might help you.
/* Create Workbook and Worksheet */
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Colored Cell Border");
HSSFCellStyle style = workbook.createCellStyle();
/* Set Borders thickness */
style.setBorderLeft(HSSFCellStyle.BORDER_THICK);
style.setBorderRight(HSSFCellStyle.BORDER_THICK);
style.setBorderTop(HSSFCellStyle.BORDER_THICK);
style.setBorderBottom(HSSFCellStyle.BORDER_THICK);
/* Get Color Index */
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
/* Add border color to a cell */
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Different border colors for a Cell");
cell.setCellStyle(my_style);
Upvotes: 2