由于某种原因,Apache POI Cellstyle不适用于第41行之后
正如你可以从标题了解到的,第41行之后,我的风格不适用,即使有debugging,我看到代码运行良好。
我的function是:
private void writeTable(Table table,Row row,Workbook wb){ CellStyle cellStyle = wb.createCellStyle(); if(row.getRowNum() % 2 == 0) { cellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex()); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); cellStyle.setAlignment(CellStyle.ALIGN_CENTER); cellStyle.setBorderTop(CellStyle.BORDER_THIN); cellStyle.setWrapText(true); cellStyle.setTopBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBottomBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); cellStyle.setBorderRight(CellStyle.BORDER_THIN); cellStyle.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setLeftBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); }else{ cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex()); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); cellStyle.setAlignment(CellStyle.ALIGN_CENTER); cellStyle.setBorderTop(CellStyle.BORDER_THIN); cellStyle.setWrapText(true); cellStyle.setTopBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBottomBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); cellStyle.setBorderRight(CellStyle.BORDER_THIN); cellStyle.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setLeftBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); } Cell cell = row.createCell(0); cell.setCellValue(Table.index); cell.setCellStyle(cellStyle); cell = row.createCell(1); cell.setCellValue(strCorrecter(Table.Name).isEmpty() ? "-" : strCorrecter(Table.Name)); cell.setCellStyle(cellStyle); cell = row.createCell(2); cell.setCellValue(strCorrecter(Table.Surname.toString()).isEmpty() ? "-" : strCorrecter(Table.Surname.toString())); cell.setCellStyle(cellStyle); cell = row.createCell(3); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(4); cell.setCellValue(strCorrecter(Table.Age.toString()).isEmpty() ? "-" : strCorrecter(Table.Age.toString())); cell.setCellStyle(cellStyle); cell = row.createCell(5); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell =row.createCell(6); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(7); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(8); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(9); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(10); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(11); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(12); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(13); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(14); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); }
而且我看到,如果在函数开头的其他语句,所有行都会经过。 但是,当我看着excel文件时,他们似乎没有任何属性。 这就是我称之为这个function的部分:
int rowCount = 3; for (Table table : tableList){ Row row = sheet.createRow(++rowCount); writeInterlock(table,row,workbook); }
我不知道发生了什么,所以任何帮助将不胜感激
使两个cellstyle参数,并发送他们的function,而不是工作簿。
if(row.getRowNum() % 2 == 0){ writeTable(table,row,style1); }else{ writeTable(table,row,style2); } private static void writeTable(Row row,CellStyle style){...}
工作簿中的唯一单元格格式/单元格样式的最大数量有一个Excel限制 。
所以不要为每一行创build一个单元格样式。 据我所知,你只需要两种不同的单元格样式。 因此,在方法之外创build这两个cellStyle1
和cellStyle2
:
... CellStyle cellStyle1 = wb.createCellStyle(); //set all the needed settings CellStyle cellStyle2 = wb.createCellStyle(); //set all the needed settings ...
然后在方法中只使用两个:
private void writeTable(Table table,Row row,Workbook wb) { ... if(row.getRowNum() % 2 == 0) { //here use cellStyle1 } else { //here use cellStyle2 } ... }
你的回答是完全正确的,有一个限制,好吧,但这个peoblem发生在你身上,因为你正在使用一个excel 97'( – 2007)的实现,这里有一个链接 。
如果您确实不需要.xls格式的文件,那么您可以在下面的工作示例(与您的类似)中以静态方式pipe理variables,就像您在接受的答案中一样。
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class MainWriteCells { public MainWriteCells() { // TODO Auto-generated constructor stub } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub FileInputStream is = new FileInputStream(new File("C:\\ook.xlsx")); XSSFWorkbook wb = new XSSFWorkbook(is); XSSFSheet sheet = wb.getSheet("Sheet1"); for(int i=0;i<1000;i++){ writeTable(sheet.createRow(i),wb); } wb.write(new FileOutputStream(new File("C:\\ook.xlsx"))); } private static void writeTable(Row row,Workbook wb){ CellStyle cellStyle = wb.createCellStyle(); if(row.getRowNum() % 2 == 0) { cellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex()); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); cellStyle.setAlignment(CellStyle.ALIGN_CENTER); cellStyle.setBorderTop(CellStyle.BORDER_THIN); cellStyle.setWrapText(true); cellStyle.setTopBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBottomBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); cellStyle.setBorderRight(CellStyle.BORDER_THIN); cellStyle.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setLeftBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); }else{ cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex()); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); cellStyle.setAlignment(CellStyle.ALIGN_CENTER); cellStyle.setBorderTop(CellStyle.BORDER_THIN); cellStyle.setWrapText(true); cellStyle.setTopBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBottomBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); cellStyle.setBorderRight(CellStyle.BORDER_THIN); cellStyle.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setLeftBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); } Cell cell = row.createCell(0); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(1); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(2); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(3); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(4); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(5); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell =row.createCell(6); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(7); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(8); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(9); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(10); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(11); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(12); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(13); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); cell = row.createCell(14); cell.setCellValue("TODO"); cell.setCellStyle(cellStyle); } }
正如你所看到的,我更改了两个对象和.xslx中的文件扩展名
XSSFWorkbook wb = new XSSFWorkbook(is); XSSFSheet sheet = wb.getSheet("Sheet1");
代替
HSSFWorkbook wb = new HSSFWorkbook(is); HSSFSheet sheet = wb.getSheet("Sheet1");
- 尝试将webtable导出为excel时出现无效的select器错误
- 我如何使用POI Excel以编程方式拉伸列以适应文本?
- 如何将活动单元格移动到使用XSSFWorkbook的范围apache poi左上方的单元格?
- 如何使用Java将Excel(2003和2007版本)文件导入到MySQL?
- 更新Java Apache POI中的现有Excel文件
- Excel公式:重叠表格中的date
- response.flushBuffer()给我一个错误,同时在客户端以编程方式打开excel文件
- 使用Apache POI创build简单工作簿时出错
- 替代getocellTypeEnum()的poi 3.17中的getcelltype()