Wie kann ich in Apache Poi zusammengeführte Zellen Grenzen hinzufügen?
Posted: 09 Apr 2025, 22:48
In Apache POI, wie kann ich zusammengeführte Zellen Grenzen hinzufügen?
Das tatsächliche Ergebnis ist jedoch: nicht korrekt sind. Das Ergebnis ist jedoch, dass der Text auf die gesamte zusammengeführte Zelle angewendet wird, aber die Grenzen scheinen nicht zu>
Code: Select all
private void mergeAndWriteCell(Sheet theSheet, int rowIndex, int columnIndex, String content, CellStyle theCellStyle) {
int lastRowIndex;
CellRangeAddress theRange;
Row theRow;
Cell theCell;
lastRowIndex = rowIndex + 2;
theRange = new CellRangeAddress(rowIndex, lastRowIndex, columnIndex, columnIndex);
theSheet.addMergedRegion(theRange);
theRow = theSheet.getRow(rowIndex);
if (theRow == null) {
theRow = theSheet.createRow(rowIndex);
}
theCell = theRow.getCell(columnIndex);
if (theCell == null) {
theCell = theRow.createCell(columnIndex, CellType.STRING);
theCell.setCellValue(content);
} else {
theCell.setBlank();
theCell.setCellValue(content);
}
theCell.setCellStyle(theCellStyle);
}
private void generateExcel() throws Exception {
XSSFWorkbook theXSSFWorkbook;
Workbook theWorkbook;
Sheet theSheet;
CellStyle theCellStyle;
ByteArrayOutputStream theByteArrayOutputStream;
OutputStream theOutputStream;
byte[] theBytes;
String saveFilePath;
File theFile;
FileOutputStream theFileOutputStream;
theXSSFWorkbook = new XSSFWorkbook();
theWorkbook = theXSSFWorkbook;
theSheet = theWorkbook.createSheet("TestTable");
theCellStyle = theWorkbook.createCellStyle();
theCellStyle.setBorderTop(BorderStyle.THIN);
theCellStyle.setBorderLeft(BorderStyle.THIN);
theCellStyle.setBorderRight(BorderStyle.THIN);
theCellStyle.setBorderBottom(BorderStyle.THIN);
theCellStyle.setAlignment(HorizontalAlignment.CENTER);
theCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
mergeAndWriteCell(theSheet, 1, 1, "as", theCellStyle);
theByteArrayOutputStream = new ByteArrayOutputStream();
theOutputStream = theByteArrayOutputStream;
theWorkbook.write(theOutputStream);
theBytes = theByteArrayOutputStream.toByteArray();
//set saveFilePath to some file path
saveFilePath = "SOME FILE PATH";
theFile = new File(saveFilePath);
theFileOutputStream = new FileOutputStream(theFile);
theFileOutputStream.write(theBytes);
theFileOutputStream.close();
}