Код: Выделить всё
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();
}
Мое понимание состоит в том, что после объединения 3 ячейки ячейка с индексом строки 1 и индекс столбца 1 должна относиться ко всей объединенной ячейке, поэтому текст и границы применяются ко всей объединенной ячейке. Но в результате текст применяется ко всей объединенной ячейке, но границы, кажется, нет.>
Подробнее здесь: https://stackoverflow.com/questions/795 ... rged-cells