我正在努力实现这样的目标:
如您所见,有些单元格只有一行,例如name或112233:
此外,还有多行单元格,每行可以有不同的样式(超链接、背景色、纯文本),这在apache poi library for java中是可能的吗?找不到任何相关信息。
我将Java8与ApachePOI4.1.2结合使用。当前已成功创建excel,每行的每列中都有一个特定类型的单元格。
try (Workbook workbook = new XSSFWorkbook()) {
Sheet sheet = workbook.createSheet("sheet");
Row header = sheet.createRow(0);
CellStyle hlinkStyle = workbook.createCellStyle();
CellStyle cellStyle = workbook.createCellStyle();
CellStyle dateStyle = workbook.createCellStyle();
CellStyle numberStyle = workbook.createCellStyle();
CellStyle rowStyle = workbook.createCellStyle();
rowStyle.setWrapText(true);
setHeaderAndStyles(workbook, hlinkStyle, dateStyle, numberStyle, cellStyle);
createHeaders(workbook, sheet, header, null);
int[] rowNum = {1};
data.forEach(ele -> {
Row row = sheet.createRow(rowNum[0]);
row.setRowStyle(rowStyle);
int cellIdx = 0;
// name
cell = row.createCell(cellIdx++);
cell.setCellValue(ele.getName());
cell.setCellStyle(cellStyle);
// ID
cell = row.createCell(cellIdx++);
cell.setCellValue(ele.getId());
cell.setCellStyle(cellStyle);
// hyper link
cell = row.createCell(cellIdx++);
Hyperlink link = workbook.getCreationHelper().createHyperlink(HyperlinkType.URL);
link.setAddress(CONST_URL);
cell.setCellValue(ele.getHyperLinkText());
cell.setHyperlink(link);
cell.setCellStyle(hlinkStyle);
// need to create more cells here with background color
// desc
cell = row.createCell(cellIdx);
cell.setCellValue(ele.getDesc());
cell.setCellStyle(cellStyle);
row.getCell(cellIdx).setCellStyle(rowStyle); // for cells with multiple lines.
// need to create more cells here with plain text
rowNum[0]++;
});
}
谢谢
暂无答案!
目前还没有任何答案,快来回答吧!