如何使用 apache poi 处理 Excel 文件中的空或空白单元格

bha*_*005 2 java apache-poi

我目前正在研究将数据从一个 Excel 工作表复制到另一个工作簿的概念,如果存在空白单元格,则应将其复制到输出文件。下面是输入文件的屏幕截图:
在此输入图像描述

这是我执行复制功能的代码

import org.apache.poi.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.examples.CreateCell;

import java.io.*;
import java.util.*;
public class openwb_test {


    public static void main(String[] args) throws Exception {

        File inputFile=new File("input.xlsx");
        FileInputStream fis=new FileInputStream(inputFile);
        XSSFWorkbook inputWorkbook=new XSSFWorkbook(fis);
        int inputSheetCount=inputWorkbook.getNumberOfSheets();
        System.out.println("Input sheetCount: "+inputSheetCount);


        File outputFile=new File("output.xlsx");
        FileOutputStream fos=new FileOutputStream(outputFile);


        XSSFWorkbook outputWorkbook=new XSSFWorkbook();


        for(int i=0;i<inputSheetCount;i++) 
        { 
            XSSFSheet inputSheet=inputWorkbook.getSheetAt(i); 
            String inputSheetName=inputWorkbook.getSheetName(i); 
            XSSFSheet outputSheet=outputWorkbook.createSheet(inputSheetName); 


            copySheet(inputSheet,outputSheet); 
        }


        outputWorkbook.write(fos); 

        fos.close(); 

        outputWorkbook.close();
    }

    public static void copySheet(XSSFSheet inputSheet,XSSFSheet outputSheet) 
    { 
        int rowCount=inputSheet.getLastRowNum(); 
        System.out.println(rowCount+" rows in inputsheet "+inputSheet.getSheetName()); 

        int currentRowIndex=0; if(rowCount>0)
        {
            Iterator<Row> rowIterator=inputSheet.iterator();
            //XSSFRow row=(XSSFRow) rowIterator.next();
            while(rowIterator.hasNext())
            {
                int currentCellIndex=0;
                Iterator<Cell> cellIterator=((XSSFRow) rowIterator.next()).cellIterator();
                while(cellIterator.hasNext())
                {

                    String cellData=cellIterator.next().toString();


                    if(currentCellIndex==0 )
                        outputSheet.createRow(currentRowIndex).createCell(currentCellIndex).setCellValue(cellData);
                    else
                        outputSheet.getRow(currentRowIndex).createCell(currentCellIndex).setCellValue(cellData);



                    currentCellIndex++; 
                } 

                currentRowIndex++;
                System.out.println(currentRowIndex);
            }
            System.out.println((currentRowIndex-1)+" rows added to outputsheet "+outputSheet.getSheetName());
            System.out.println();
        }
    }




}
Run Code Online (Sandbox Code Playgroud)

但是,如果我运行上面的代码,结果将被复制,但只要存在空白单元格,代码就会被修剪掉。例如下面是输出片段:
在此输入图像描述

谁能建议我如何在这种情况下处理空白单元格,因为我的期望是输入文件数据应该被复制,因为它包括空白单元格到输出文件中

HRH*_*eeb 9

getCell 方法有第二个参数,用于指定处理空单元格的策略。

在一个好的 for 循环中使用它:

    for (int i=0; i<row.getLastCellNum(); i++) {
        Cell cell = row.getCell(i, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
        if (cell == null) {
            // ...
        } else {
            // ...
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 您仍然可能遇到跳过空行的问题,例如在使用sheet.rowIterator()时。为了避免这种情况,请使用行号迭代行并测试 null。 (2认同)