Java 使用ApachePOI对.xls中行的前n列应用样式
我在java中使用POI创建excel,在创建的表上设置样式时遇到问题 由于表格数据庞大,在每个单元格上设置样式需要花费大量时间。当我使用rowstyle时,样式也发生在表外的列上。有没有一种方法可以有效地对有限的列(比如每行的前n列)应用样式Java 使用ApachePOI对.xls中行的前n列应用样式,java,excel,formatting,apache-poi,Java,Excel,Formatting,Apache Poi,我在java中使用POI创建excel,在创建的表上设置样式时遇到问题 由于表格数据庞大,在每个单元格上设置样式需要花费大量时间。当我使用rowstyle时,样式也发生在表外的列上。有没有一种方法可以有效地对有限的列(比如每行的前n列)应用样式 public XSSFWorkbook createReport(XSSFWorkbook wb, Map<String, List<ReportDto>> sheetData){ int rowIndex = 1;
public XSSFWorkbook createReport(XSSFWorkbook wb, Map<String, List<ReportDto>> sheetData){
int rowIndex = 1;
int columnIndex = 0;
int index = 1;
wb.getSheetAt(0);
XSSFRow tempRow;
XSSFCell tempCell;
wb.getSheetAt(0);
for (String hexa : sheetData.keySet()) {
List<COAReportSheet2Dto> itemList = sheetData.get(hexa);
ListIterator<COAReportSheet2Dto> itemIterator = itemList.listIterator();
while (itemIterator.hasNext()) {
COAReportSheet2Dto eachItemData = itemIterator.next();
if (!eachItemData.getGeneSymbol().equals("")) {
tempRow = wb.getSheetAt(0).createRow(rowIndex++);
tempCell = tempRow.createCell(columnIndex++);
tempCell.setCellValue(index++);
tempCell = tempRow.createCell(columnIndex++);
tempCell.setCellValue(eachItemData.getPosition());
tempCell = tempRow.createCell(columnIndex++);
tempCell.setCellValue(eachItemData.getSymbol());
tempCell = tempRow.createCell(columnIndex++);
tempCell.setCellValue(hexa);
tempCell = tempRow.createCell(columnIndex++);
tempCell.setCellValue(eachItemData.getViperId);
tempCell = tempRow.createCell(columnIndex);
tempCell.setCellValue(eachItemData.getViperPole);
}
columnIndex = 0;
}
}
return wb;
}
我不确定你是如何做到这一点的,但你可以做到:
public static XSSFWorkbook createReport(XSSFWorkbook wb, Map<String, List<COAReportSheet2Dto>> sheetData){
int rowIndex = 1;
int columnIndex = 0;
int index = 1;
wb.getSheetAt(0);
XSSFRow tempRow;
XSSFCell tempCell;
wb.getSheetAt(0);
// Create style
CellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
for (String hexa : sheetData.keySet()) {
List<COAReportSheet2Dto> itemList = sheetData.get(hexa);
ListIterator<COAReportSheet2Dto> itemIterator = itemList.listIterator();
while (itemIterator.hasNext()) {
COAReportSheet2Dto eachItemData = itemIterator.next();
if (!eachItemData.getGeneSymbol().equals("")) {
tempRow = wb.getSheetAt(0).createRow(rowIndex++);
tempCell = tempRow.createCell(columnIndex++);
tempCell.setCellValue(index++);
tempCell.setCellStyle(style); // style is set only on this column
tempCell = tempRow.createCell(columnIndex++);
tempCell.setCellValue(eachItemData.getPosition());
tempCell = tempRow.createCell(columnIndex++);
tempCell.setCellValue(eachItemData.getSymbol());
tempCell = tempRow.createCell(columnIndex++);
tempCell.setCellValue(hexa);
tempCell = tempRow.createCell(columnIndex++);
tempCell.setCellValue(eachItemData.getViperId());
tempCell = tempRow.createCell(columnIndex);
tempCell.setCellValue(eachItemData.getViperPole());
}
columnIndex = 0;
}
}
return wb;
}
然后,可以仅在所需的列中设置样式。关于性能,记住只创建一次CellStyle。这样,您就可以在所需的每个单元格中设置相同的单元格样式。如果为创建的每个单元格创建新的单元格样式,则程序可能会消耗不必要的内存
另一种方法是使用工作表中的setDefaultColumnStyle:
你能在问题中分享一下你是如何编码的吗?否则我不能给你一个正确的答案。@FagnerFonseca我已经附上了代码片段。外部循环中条目的格式必须不同,但当我设置行样式时,它会在该行的所有列上设置样式,尽管我只想在6列上设置边框格式。您在哪里设置单元格样式?谢谢Fagner!我在每个tempCell.setCellValue之后设置样式,但是对于大数据,程序需要2-3分钟来生成.xls。我希望在行级别设置样式将提高性能。我有另一种方法,生成大约有13列的工作表,在单元格级别设置单元格样式需要9-10分钟。对于大数据,可以使用SXSSFWorkbook。再次感谢,我来试试SXXSFWorkbook。因此,要设置行中有限列的样式,唯一的选择是对单个单元格进行样式设置,对吗?您可以使用来自工作表的setDefaultColumnStyle,我用一个示例更新了答案。是的,我确实尝试过这一点,但要求设置每行的特定列的样式,而setDefaultColumnStyle也将设置表外所有列的样式。
CellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
Sheet sheet = wb.getSheetAt(0);
CellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
sheet.setDefaultColumnStyle(0, style);
sheet.setDefaultColumnStyle(1, style);