bharat005
bharat005

Reputation: 57

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. below is the screenshot for input file:
enter image description here

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:
enter image description here

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

Upvotes: 2

Views: 6813

Answers (1)

HRHeeb
HRHeeb

Reputation: 361

The getCell method has a second argument that specifies a policy for handling empty cells.

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 {
            // ...
        }
    }

Upvotes: 10

Related Questions