如何在java中使用POI替换excel中的空白单元格

时间:2022-04-06 22:19:34
FileInputStream file= new FileInputStream(new File(path)); //Read the spreadsheet that needs to be updated            
            HSSFWorkbook wb = new HSSFWorkbook(file); //Access the workbook            
            HSSFSheet worksheet = wb.getSheetAt(0); //Access the worksheet, so that we can update / modify it.            
            Cell cell = null; // declare a Cell object
/////////////////////////////////// PRENDRE LES LIGNE ET LES COLLOLNES A MODIFER             
            int nbrow=Integer.parseInt(tx1.getText());
            int nbClmn=Integer.parseInt(tx2.getText());
//            int nbClmn = worksheet.getRow(0).getPhysicalNumberOfCells();            
                  System.out.println(nbClmn);    
                  for(int j=0;j<nbrow;j++){
                  for(int i=0;i<nbClmn;i++){   
                  cell = worksheet.getRow(j).getCell(i);}
for(int i=1;i<=nbClmn;i++){  
                   for(int j=1;j<nbrow;j++){  
                  System.out.println(wb.getSheetAt(0).getRow(j).getCell(i));    
                  String r=wb.getSheetAt(0).getRow(j).getCell(i).toString();  
                if(r.equals("BT") || r.equals("CB") || r.equals("CX") || r.equals("EB") || r.equals("EC") || r.equals("EP") || r.equals("NA") || r.equals("PP")){
                cell = worksheet.getRow(j).getCell(i);
                     cell.setCellValue("BT");
                  }else{ cell = worksheet.getRow(j).getCell(i);

                  }                  
        if(r.equals("CM") || r.equals("EM") || r.equals("GC") || r.equals("HT") || r.equals("MT")){
                cell = worksheet.getRow(j).getCell(i);
                     cell.setCellValue("MT");
                  }else{ cell = worksheet.getRow(j).getCell(i);

                  }}}
file.close(); //Close the InputStream                 
                FileOutputStream output_file =new FileOutputStream(new File(export));  //Open FileOutputStream to write updates                  
                wb.write(output_file);

                output_file.close();

How can I fill all the blank cells in an Excel file with the word "Not Available", using Java Apache POI? I have 10 columns in my Excel file and I want to fill the 11th column with a calculated number and it is unable to fill because it has a null value. If you can help me, I would realy appreciate it.

如何使用Java Apache POI用“不可用”一词填写Excel文件中的所有空白单元格?我的Excel文件中有10列,我想用计算出的数字填充第11列,但由于它具有空值,因此无法填充。如果你能帮助我,我真的很感激。

1 个解决方案

#1


1  

If the cell doesn't exist in the row you need to create it. If you don't know whether the cell exists or not you can do following

如果行中不存在单元格,则需要创建它。如果您不知道细胞是否存在,您可以进行以下操作

...
Cell cell  = row.getCell(index);
if (cell == null) {
    cell = row.createCell(index)
}
...

#1


1  

If the cell doesn't exist in the row you need to create it. If you don't know whether the cell exists or not you can do following

如果行中不存在单元格,则需要创建它。如果您不知道细胞是否存在,您可以进行以下操作

...
Cell cell  = row.getCell(index);
if (cell == null) {
    cell = row.createCell(index)
}
...