setCellValue不起作用

我不能设置细胞的价值,它的工作原理,如果我试试这个

if(1==1){ celltofill.setCellValue("test"); } 

但一旦进入if条件,它将显示syso中的内容,但不会在单元格中插入值。

关于如何解决这个问题的任何想法?

这里是我的代码:

 public class testexcel { public static void main(String[] args) throws InvalidFormatException, IOException, ParseException{ FileInputStream fis = new FileInputStream("D:\\test6.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(fis); FileOutputStream fos=new FileOutputStream("D:\\edit6.xlsx"); XSSFSheet sheet = workbook.getSheetAt(0); XSSFSheet sheet2 = workbook.getSheetAt(1); for(int i=1; i <= sheet.getLastRowNum(); i++) { for(int i1=1;i1<sheet2.getLastRowNum();i1++){ Row row = sheet.getRow(i); Cell cell = row.getCell(2); Row row2 = sheet2.getRow(i1); Cell cell2 = row2.getCell(0); Cell celltofill= row.createCell(5); Cell cellres= row2.getCell(1); if((cell.getStringCellValue()).equals(cell2.getStringCellValue())){ System.out.println((cell.getStringCellValue())+" equals "+cell2.getStringCellValue()); celltofill.setCellType(Cell.CELL_TYPE_STRING); System.out.println("cell filled with "+cellres.getStringCellValue()); celltofill.setCellValue(cellres.getStringCellValue()); } } } workbook.write(fos); fos.close(); } } 

row.createCell(5)完全是这样说的。 每次调用它时会创build一个新的空单元格。 而且,您在sheet2每一行都一次又一次地调用它,尽pipe它是一个单元格。 所以,即使标准没有满足,新的空单元格已经在您的代码中创build。

也许:

 for(int i=1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); Cell cell = row.getCell(2); Cell celltofill= row.getCell(5); if (celltofill == null) celltofill = row.createCell(5); for(int i1=1;i1<sheet2.getLastRowNum();i1++){ Row row2 = sheet2.getRow(i1); Cell cell2 = row2.getCell(0); Cell cellres= row2.getCell(1); 

会更多你想要什么?