如何使用 java Apache POI 在 excel 中动态构建边界
How to Build Border dynamically in excel using java Apache POI
所以在我的 java class 中,我有以下代码。
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++;
}
和边框
for (int i = 0; i < objArr.length; i++) {
Cell columnHeaderCell = myRow.createCell(i);
columnHeaderCell.setCellValue((Double) objArr[i]);
columnHeaderCell.setCellStyle(columnHeaderStyle);
}
问题是边框重叠在数据之上并且正在为所有单元格创建边框。
我如何在 java 中动态创建 2 X 10*(x) table?
使用 Apache POI 可以如下设置边框。
查找以下示例,这可能对您有所帮助。
/* 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);
我通过执行以下操作实现了它。获取数据为 object。因此,对于空单元格,验证 object!=null.
我的代码:
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);
}
用于动态构建带有边框的行:
建筑物Header列:
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);
}
}
用于构建值列:
if (objArr[1] != null) {
value = objArr[1].toString();
myCell = myRow.createCell(1);
myCell.setCellValue(value);
myCell.setCellStyle(style);
}
rowNum++;
}
上面的括号是为了结束for each
循环。
边框
HSSFCellStyle style=wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
所以在我的 java class 中,我有以下代码。
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++;
}
和边框
for (int i = 0; i < objArr.length; i++) {
Cell columnHeaderCell = myRow.createCell(i);
columnHeaderCell.setCellValue((Double) objArr[i]);
columnHeaderCell.setCellStyle(columnHeaderStyle);
}
问题是边框重叠在数据之上并且正在为所有单元格创建边框。 我如何在 java 中动态创建 2 X 10*(x) table?
使用 Apache POI 可以如下设置边框。
查找以下示例,这可能对您有所帮助。
/* 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);
我通过执行以下操作实现了它。获取数据为 object。因此,对于空单元格,验证 object!=null.
我的代码:
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);
}
用于动态构建带有边框的行:
建筑物Header列:
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);
}
}
用于构建值列:
if (objArr[1] != null) {
value = objArr[1].toString();
myCell = myRow.createCell(1);
myCell.setCellValue(value);
myCell.setCellStyle(style);
}
rowNum++;
}
上面的括号是为了结束for each
循环。
边框
HSSFCellStyle style=wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);