简体   繁体   English

如何使用Apache POI处理Excel文件中的空白单元格

[英]How to Deal with empty or blank cell in excel file using apache poi

I am currently working on the concept of copying the data from one excel sheet to another workbook with if blank cells exist then the same should be copied to the output file. 我目前正在研究将数据从一个Excel工作表复制到另一工作簿的概念,如果存在空白单元格,则应将其复制到输出文件。 below is the screenshot for input file: 以下是输入文件的屏幕截图:
在此处输入图片说明

here is my code to perform copy function 这是我执行复制功能的代码

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();
        }
    }




}

But if I run the above code, the results will be copied but wherever the blank cells exists, code will trim off. 但是,如果我运行上面的代码,结果将被复制,但是无论空白单元格在哪里,代码都会被修剪掉。 for example below is the output snip: 例如,下面是输出片段:
在此处输入图片说明

Could anyone suggest me how we can handle the blank cell in this scenario as my expectation is input file data should be copied as it is including blank cells to the output file 有人可以建议我在这种情况下如何处理空白单元格,因为我期望输入文件数据应被复制,因为它包括空白单元格到输出文件

The getCell method has a second argument that specifies a policy for handling empty cells. getCell方法具有第二个参数,该参数指定用于处理空单元格的策略。

Use it in a good ol' for loop: 在良好的循环中使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM