2012-02-07 11 views
3

Apache POIを使用してxlsxファイルを読み込んでいますが、うまくいきます。行がヌルになったときに私はあなたに質問します、どうすればそれを処理できますか?私のファイルには500行が含まれていますが、行には105667行、残りの行にはnullが見つかりました。Apache POIを使用してNull行を処理する方法は?

は、コードを使用:

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シート内のNULL列を処理する方法を、私を助けてください?

答えて

3

行をフェッチしてnullに戻すと、その行のファイルにデータが格納されていないことを意味します。完全に空白です。

POIはデフォルトでファイルの内容を示します。セルでは、MissingCellPolicyを設定して、欠落および空白セルの処理方法を制御できます。 the Apache POI docsにこれを使用した例がいくつかあります。行は、そこにあるかどうかに関係なく、行をフェッチするときにはnullをチェックする必要があります。

+0

このトピックについてもう少し詳しく解説しますか?実際に 'MissingCellPolicy'を使用して、それらの 'null'/'空白'の行に対して 'NullPointerException'を防ぐ方法を教えてください。 –

+0

私はこれに関するPOIドキュメントへのリンクを追加しました。これにはコード例が含まれています。それはあなたが何をしたのかをカバーしていますか? – Gagravarr

+0

私はPOIドキュメントを読んだが、コード例は見つからなかった。私はそれを調べます。さらなる研究の後、私は私の質問に答えることができました。私はあなたの返事に感謝します。 –

0

.xlsxファイルに空白セルの書式が含まれている場合、poiの読みはnullとして扱われませんが、値を印刷する場合はNullPointerExceptionが返されます。それを理解するために、私はシートを作成し、最初のカラム境界を10行分の "All Border"にマークしましたが、値は与えられていません。次のコードを適用すると、sheet.lastRowNum()の出力が10、RowCountWithNullValueは990、RowCountWithoutNullValueは10です。ただし、シートは完全に空白です。 printステートメントのコメントを外すと、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(i)'ではなく 'sheet.getRow(0)'を常に呼び出すのはなぜですか? – Gagravarr

関連する問題