2016-11-11 259 views
1

我是新来的Apache POI但不能对Java。我一直在试图将数据从一个Excel文件转换为另一种,改变任何分隔符数据的新细胞。虽然大多数新的数据是正确的,但有一些例外没有关注的空白单元格的规则。 这里是输入文件https://ufile.io/bce15 这里是输出文件https://ufile.io/55020的Java的Apache POI空白单元格

在图像下面记录4和5不遵守游戏规则,我不能找出原因!

enter image description here

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

回答

2

在代码中,你正在使用cellIterator

Iterator<Cell> cells = row.cellIterator(); 

cellIterator不包括为空细胞。

而不是使用cellIterator的,你必须从第一单元到最后一个单元格开始循环,然后检查单元格是否为空或不是。

您可以参考下面的示例代码 -

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 

}