Apache POI blank values

2019-07-20 04:43发布

问题:

I am using Apache POI to import data from excel file to database.(newbie to APACHE POI)

In which I am allowing user to select columns from excel sheet and Map those columns to the Database columns. After mapping the columns, when I try to insert the records from Excel to Database then:

  • If Columns with NO blank values in them are Mapped then Proper data is inserted into the database
  • If columns are Mapped with BLANK values in them, then if a Excel Cell has blank value then previous value of that column is assigned.

Source Code:

FileInputStream file = new FileInputStream(new File("C:/Temp.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file); //Get the workbook instance for XLS file
HSSFSheet sheet = workbook.getSheetAt(0);   //Get first sheet from the workbook
Iterator<Row> rowIterator = sheet.iterator(); //Iterate through each rows from first sheet
while (rowIterator.hasNext())
{
  HSSFRow hssfRow = (HSSFRow) rowIterator.next();
  Iterator<Cell> iterator = hssfRow.cellIterator();
  int current = 0, next = 1;
  while (iterator.hasNext())
  {
    HSSFCell hssfCell = (HSSFCell) iterator.next();
    current = hssfCell.getColumnIndex();
    for(int i=0;i<arrIndex.length;i++)    //arrayIndex is array of Excel cell Indexes selected by the user
    {
      if(arrIndex[i] == hssfCell.getColumnIndex())
      {
        if(current<next) 
        {
                    //System.out.println("Condition Satisfied");     
        }
        else 
        {
          System.out.println( "pstmt.setString("+next+",null);");
          pstmt.setString(next,null);
          next = next + 1;
        }
        System.out.println( "pstmt.setString("+next+","+((Object)hssfCell).toString()+");");
        pstmt.setString(next,((Object)hssfCell).toString());
        next = next + 1;
      }
    }
  }
  pstmt.addBatch();
  }

I have look for similar questions on SO, but still not able to solve the issue.. So any help will be appreciated.

Thanks in advance..

回答1:

You've made a very common mistake, which has been covered in rather a lot of past StackOverflow questions

As the Apache POI documentation on cell iterating says

In some cases, when iterating, you need full control over how missing or blank cells are treated, and you need to ensure you visit every cell and not just those defined in the file. (The CellIterator will only return the cells defined in the file, which is largely those with values or stylings, but it depends on Excel).

It sounds like you are in that situation, where you need to care about hitting every row/cell, and not just grabbing all the available cells without worrying about the gaps

You'll want to change you code to look somewhat like the example in the POI docs:

// Decide which rows to process
int rowStart = Math.min(15, sheet.getFirstRowNum());
int rowEnd = Math.max(1400, sheet.getLastRowNum());

for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
   Row r = sheet.getRow(rowNum);

   int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);

   for (int cn = 0; cn < lastColumn; cn++) {
      Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
      if (c == null) {
         // The spreadsheet is empty in this cell
         // Mark it as blank in the database if needed
      } else {
         // Do something useful with the cell's contents
      }
   }
}