What if POI failed to copy the EXCEL cell style?
Today, I will talk to you about how to do if POI copy EXCEL cell style fails. Many people may not know much about it. In order to let you know more, Xiaobian summarizes the following contents for you. I hope you can gain something according to this article.
XSSFCell Two ways to copy cell styles
1. Load the original cell style directly into the new cell
XSSFRow newRow = sheet.createRow(row + 5);for (int cell = 0; cell < maxCellNum; cell++) { XSSFCell newCell = newRow.createCell(cell); newCell.setCellStyle(sheet.getRow(5).getCell(cell).getCellStyle());}
Copy a new style using CellStyle's cloneStryleFrom method
XSSFRow newRow = sheet.createRow(row + 5);for (int cell = 0; cell < maxCellNum; cell++) { XSSFCell newCell = newRow.createCell(cell); CellStyle cStyle = workbook.createCellStyle(); cStyle.cloneStyleFrom(newCell.getCellStyle()); newCell.setCellStyle(cStyle);}
The second method is recommended in general, and method one may cause cell styles to be overwritten.
Problem background: through excel template, specific data will be filled into the corresponding cells to generate a list of goods file. The template is as follows (for intuitive purposes, the last two columns of background colors are set to orange and red for easy comparison)
The final excel file is as follows
The problem encountered this time is that cells cannot be styled properly using either of the above methods.
After investigation, it was found that the style was not assigned to the last cell in each row, so the style would not take effect. The last cell in each row must be assigned a style to display the style normally.
excelRow.getCell(10).setCellValue("");
The modified export file is as follows
After reading the above, do you have any further understanding of how to do if POI fails to copy EXCEL cell style? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.