2012-02-07 80 views
3

我使用Apache POI來讀取xlsx文件,它運行良好。當行被發現爲空時,我有問題,我能夠如何處理它?我的文件包含500行,但它顯示105667行,其餘行發現爲空。如何使用Apache POI處理空行?

使用的代碼:

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.text.SimpleDateFormat; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.DateUtil; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.poi.ss.usermodel.WorkbookFactory; 
import org.apache.poi.xssf.usermodel.XSSFRow; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

/** 
* 
* @author SAMEEK 
*/ 
public class readXLSXFile { 
public int getNumberOfColumn(String fileName, int sheetIndex) throws FileNotFoundException, IOException { 
    File inputFile = null; 
    FileInputStream fis = null; 
    XSSFWorkbook workbook = null; 
    XSSFSheet sheet = null; 
    XSSFRow row = null; 
    int lastRowNum = 0; 
    int lastCellNum = 0; 


    // Open the workbook 
    inputFile = new File(fileName); 
    fis = new FileInputStream(inputFile); 
    workbook = new XSSFWorkbook(fis); 
    sheet = workbook.getSheetAt(sheetIndex); 
    lastRowNum = sheet.getLastRowNum(); 

    for (int i = 0; i < lastRowNum; i++) { 

     row = sheet.getRow(i); 
     if (row != null) { 
      if (row.getLastCellNum() > lastCellNum) { 
       lastCellNum = row.getLastCellNum(); 
      } 
     } 
    } 

    return lastCellNum; 
} 

public int getNumberOfRow(String fileName, int sheetIndex) throws FileNotFoundException, IOException { 
    File inputFile = null; 
    FileInputStream fis = null; 
    XSSFWorkbook workbook = null; 
    XSSFSheet sheet = null; 
    int lastRowNum = 0; 

    // Open the workbook 
    inputFile = new File(fileName); 
    fis = new FileInputStream(inputFile); 
    workbook = new XSSFWorkbook(fis); 
    sheet = workbook.getSheetAt(sheetIndex); 
    lastRowNum = sheet.getLastRowNum(); 
    return lastRowNum; 
} 

public String[] getSheetName(String fileName) throws FileNotFoundException, IOException { 
    int totalsheet = 0; 
    int i = 0; 
    String[] sheetName = null; 
    File inputFile = null; 
    FileInputStream fis = null; 
    XSSFWorkbook workbook = null; 

    // Open the workbook 
    inputFile = new File(fileName); 
    fis = new FileInputStream(inputFile); 
    workbook = new XSSFWorkbook(fis); 
    totalsheet = workbook.getNumberOfSheets(); 
    sheetName = new String[totalsheet]; 
    while (i < totalsheet) { 
     sheetName[i] = workbook.getSheetName(i); 
     i++; 
    } 

    return sheetName; 
} 

public int getNumberOfSheet(String fileName) throws FileNotFoundException, IOException { 
    int totalsheet = 0; 
    File inputFile = null; 
    FileInputStream fis = null; 
    XSSFWorkbook workbook = null; 
    XSSFSheet sheet = null; 
    int lastRowNum = 0; 

    // Open the workbook 
    inputFile = new File(fileName); 
    fis = new FileInputStream(inputFile); 
    workbook = new XSSFWorkbook(fis); 
    totalsheet = workbook.getNumberOfSheets(); 
    return totalsheet; 
} 

public String[][] getSheetData(String fileName, int sheetIndex) throws FileNotFoundException, IOException, InvalidFormatException { 
    String[][] data = null; 
    int i = 0; 
    int j = 0;Cell cell=null; 
    long emptyrowcount = 0; 
    InputStream inputStream = new FileInputStream(
      fileName); 
    // Create a workbook object. 
    Workbook wb = WorkbookFactory.create(inputStream); 
    wb.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK); 
    Sheet sheet = wb.getSheetAt(sheetIndex); 
    // Iterate over all the row and cells 
    int noOfColumns = getNumberOfColumn(fileName, sheetIndex); 
    System.out.println("noOfColumns::" + noOfColumns); 
    int noOfRows = getNumberOfRow(fileName, sheetIndex) + 1; 
    System.out.println("noOfRows::" + noOfRows); 
    data = new String[noOfRows][noOfColumns]; 

    for (int k = 0; k < noOfRows; k++) { 
     Row row = sheet.getRow(k); 
     if (row == null) { 


     } else { 
      j = 0; 
      for (int l = 0; l < noOfColumns; l++) { 
       // Cell cell = cit.next(); 
       cell = row.getCell(j); 


       if (cell.getCellType() == cell.CELL_TYPE_BLANK) { 
        cell = row.getCell(j, Row.CREATE_NULL_AS_BLANK); 
       } 

       data[i][j] = getCellValueAsString(cell); 
       j++; 

      } 
      i++; 

     } 
    } 

    return data; 
} 

/** 
* This method for the type of data in the cell, extracts the data and 
* returns it as a string. 
*/ 
public static String getCellValueAsString(Cell cell) { 
    String strCellValue = null; 
    if (cell != null) { 
     switch (cell.getCellType()) { 
      case Cell.CELL_TYPE_STRING: 
       strCellValue = cell.toString(); 
       break; 
      case Cell.CELL_TYPE_NUMERIC: 
       if (DateUtil.isCellDateFormatted(cell)) { 
        SimpleDateFormat dateFormat = new SimpleDateFormat(
          "dd/MM/yyyy"); 
        strCellValue = dateFormat.format(cell.getDateCellValue()); 
       } else { 
        Double value = cell.getNumericCellValue(); 
        Long longValue = value.longValue(); 
        strCellValue = new String(longValue.toString()); 
       } 
       break; 
      case Cell.CELL_TYPE_BOOLEAN: 
       strCellValue = new String(new Boolean(
         cell.getBooleanCellValue()).toString()); 
       break; 
      case Cell.CELL_TYPE_BLANK: 
       strCellValue = ""; 
       break; 

     } 
    } 

    return strCellValue; 
} 

public static void main(String s[]) { 
    try { 
     readXLSXFile readXLSxFile = new readXLSXFile(); 
     String[][] sheetData = readXLSxFile.getSheetData("F:/work.xlsx", 0); 

     int columnLength = 0; 
     columnLength = readXLSxFile.getNumberOfColumn("F:/work.xlsx", 0); 
     int rowLength = 0; 
     rowLength = readXLSxFile.getNumberOfRow("F:/work.xlsx", 0); 


     int h = 0; 
     int j = 0; 
     while (j < rowLength) { 
      h = 0; 
      while (h < columnLength) { 
       System.out.print("\t  " + sheetData[j][h]); 
       h++; 
      } 
      System.out.println(""); 
      j++; 
     } 

    } catch (InvalidFormatException ex) { 
     Logger.getLogger(readXLSFile.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (FileNotFoundException ex) { 
     Logger.getLogger(readXLSFile.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (IOException ex) { 
     Logger.getLogger(readXLSFile.class.getName()).log(Level.SEVERE, null, ex); 


     } 
    } 
} 

請幫助我如何處理excel表格空行?

回答

3

如果您獲取一行並取回null,那麼這意味着該行的文件中沒有存儲數據 - 它完全是空白的。

POI默認爲您提供文件中的內容。通過單元格,您可以設置一個MissingCellPolicy來控制丟失和空白單元格的處理方式。有一些使用the Apache POI docs的例子。對於行,它們不是在那裏,所以你在檢索一行時需要檢查空值。

+0

你想介紹一下這個話題。您如何實際使用'MissingCellPolicy'來防止'null'/'空白'行出現'NullPointerException'。 – 2014-01-06 23:42:34

+0

我已在此添加了POI文檔的鏈接,其中包含代碼示例。這是否涵蓋了你之後的事情? – Gagravarr 2014-01-07 12:29:36

+0

我已閱讀POI文檔,但未找到代碼示例。我會研究這一點。經過一些額外的研究,我已經能夠回答我的問題。我很感激你的回覆。 – 2014-01-07 14:08:28

0

如果您的.xlsx文件包含空白單元格的任何格式,則poi讀數不會將其視爲null,但是如果要打印它的值,則會給出NullPointerException。爲了理解它,我創建了一張工作表,並將第一列邊界標記爲「所有邊界」爲10行,但未給出任何值。現在應用以下代碼片段將顯示output sheet.lastRowNum()爲10,而RowCountWithNullValue爲990,RowCountWithoutNullValue爲10.但該表完全爲空。如果取消打印語句的註釋,它將顯示NullPointerException。

public class Rough { 
public static void main(String args[]) throws IOException{ 
    public static void main(String args[]) throws IOException{ 
    FileInputStream fin = new FileInputStream(AddressOfxlsxFile); 
    XSSFWorkbook wb = new XSSFWorkbook(fin); 
    XSSFSheet sheet = wb.getSheetAt(1); 
    int RowCountWithNullValue=0, RowCountWithoutNullValue=0; 
    for (int i=0;i<1000;i++){ 
     if (sheet.getRow(i)==null) 
      RowCountWithNullValue++; 
     else{ 
      RowCountWithoutNullValue++; 
     // System.out.println(sheet.getRow(0).getCell(0)); 
     } 
    } 
    System.out.println(sheet.getLastRowNum()); 
    System.out.println(RowCountWithNullValue+","+RowCountWithoutNullValue); 
    } 
} 

我不一樣的是發生在你到底要不要,但如果你說你的文件包含500行,但它顯示105667行,這可能是原因之一。

+1

這不是一個新問題,而不是一個答案?另外,爲什麼總是在else塊中調用'sheet.getRow(0)'而不是'sheet.getRow(i)'? – Gagravarr 2013-04-10 21:16:10