Reputation: 69
I am new to Apache POI but not new to Java. I been trying to convert data from one excel file to another, changing any delimiter data to new cells. Although most of the new data is correct but a few exceptions are not following the rules for blank cells. Here is the input file https://ufile.io/bce15 and here is the output file https://ufile.io/55020
In the Image below record 4 and 5 are not playing by the rules, and i cant figure out why!!
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
public class testWrite {
public static void main( String [] args ) {
int rowNums = 1;
try {
FileInputStream file = new FileInputStream(new File("ExceptionDataDummy.xlsx"));;
XSSFWorkbook wb = new XSSFWorkbook(file);
XSSFSheet sheet = wb.getSheetAt(0);
String filename = "ExceptionDataResult.xlsx" ;
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet1 = workbook.createSheet("FirstSheet");
Iterator<Row> rows = sheet.rowIterator();
int col = 0;
while( rows.hasNext() ) {
XSSFRow row = (XSSFRow) rows.next();
XSSFRow row1 = sheet1.createRow((short)rowNums);
System.out.println("\n");
Iterator<Cell> cells = row.cellIterator();
while( cells.hasNext() ) {
XSSFCell cell = (XSSFCell) cells.next();
if(XSSFCell.CELL_TYPE_NUMERIC==cell.getCellType()) {
row1.createCell(col).setCellValue(cell.getNumericCellValue());
col++;
}
else
if(XSSFCell.CELL_TYPE_STRING==cell.getCellType()){
String contents = cell.getStringCellValue();
String[] items = contents.split(",");
for (String item : items) {
row1.createCell(col).setCellValue(item);
col++;
}
}
else
if(XSSFCell.CELL_TYPE_BOOLEAN==cell.getCellType()) {
row1.createCell(col).setCellValue(cell.getBooleanCellValue());
col++;
}
else
if((cell.equals("")) || (XSSFCell.CELL_TYPE_BLANK==cell.getCellType()) || (cell == null)) {
cell.setCellType(Cell.CELL_TYPE_BLANK);
col++;
}
else {
System.out.print("Unknown cell type");
}
}
rowNums++;
col=0;
}
FileOutputStream fileOut = new FileOutputStream(filename);
workbook.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated!");
wb.close();
workbook.close();
} catch ( IOException ex ) {
ex.printStackTrace();
}
}
}
Upvotes: 1
Views: 4999
Reputation: 5813
In your code, you are using cellIterator
Iterator<Cell> cells = row.cellIterator();
cellIterator does not include cells which are null.
Instead of using cellIterator, you have to start loop from the first cell to the last cell and then check whether cell is null or not.
You can refer to below sample code -
for (int colNum = 0; colNum < row.getLastCellNum(); colNum++) {
Cell cell = row.getCell(colNum, Row.CREATE_NULL_AS_BLANK);
String cellValue = null;
// do whatever you want to do with the cell or its value
}
Upvotes: 5