2017-09-19 15 views
1

Excelファイルを読み取るプログラムにこの基本レイアウトを使用しようとしています。私はこのコードだけではなく、何らかの理由でコードをビルドするたびに、「注:C:\ Users \ Ryan Kabir \ Documents \ NetBeansProjects \ ExcelReader \ src \ excelreader \ ExcelReader.javaの使用または上書き廃止予定のAPI 注:詳細については、-Xlint:deprecationを使用して再コンパイルしてください。 javac -Xlint:deprecation ExcelReader.javaでコンパイルしようとしましたが、どのメソッドが廃止予定であるかを知ることができませんでした。 私のテストではないExcelファイルは単なる3x6のテーブルです。私のExcelの読み込みが正常に動作するためにApache POIを取得できない

import java.io.File; 
 
import java.io.FileInputStream; 
 
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.ss.usermodel.Sheet; 
 
import org.apache.poi.ss.usermodel.Workbook; 
 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
 
    
 
/** 
 
* A dirty simple program that reads an Excel file. 
 
* @author www.codejava.net 
 
* 
 
*/ 
 
public class ExcelReader { 
 
     
 
    public static void main(String[] args) throws IOException { 
 
     String excelFilePath = "Books.xlsx"; 
 
     FileInputStream inputStream = new FileInputStream(new File(excelFilePath)); 
 
      
 
     Workbook workbook = new XSSFWorkbook(inputStream); 
 
     Sheet firstSheet = workbook.getSheetAt(0); 
 
     Iterator<Row> iterator = firstSheet.iterator(); 
 
      
 
     while (iterator.hasNext()) { 
 
      Row nextRow = iterator.next(); 
 
      Iterator<Cell> cellIterator = nextRow.cellIterator(); 
 
       
 
      while (cellIterator.hasNext()) { 
 
       Cell cell = cellIterator.next(); 
 
        
 
       switch (cell.getCellType()) { 
 
        case Cell.CELL_TYPE_STRING: 
 
         System.out.print(cell.getStringCellValue()); 
 
         break; 
 
        case Cell.CELL_TYPE_BOOLEAN: 
 
         System.out.print(cell.getBooleanCellValue()); 
 
         break; 
 
        case Cell.CELL_TYPE_NUMERIC: 
 
         System.out.print(cell.getNumericCellValue()); 
 
         break; 
 
       } 
 
       System.out.print(" - "); 
 
      } 
 
      System.out.println(); 
 
     } 
 
      
 
     workbook.close(); 
 
     inputStream.close(); 
 
    } 
 

 
}

+1

どのバージョンの 'apache-poi'を使用していますか?依存関係を投稿する – pleft

+0

これはエラーではない、警告です。それはあなたが指定しなかったものが働いていない原因ではありません。 – NickL

答えて

1

Cell.getCellTypeだけでなく、Cell内のすべてのフィールドは推奨されません。代わりにGetting the cell contentsに示すようにCell.getCellTypeEnumCellTypeを使用してください。

それは少しので

error: an enum switch case label must be the unqualified name of an enumeration constant 
    case CellType.STRING: 

で変更する必要があります。しかし、次の作業をする必要があります:

import org.apache.poi.ss.usermodel.CellType; 
... 
      switch (cell.getCellTypeEnum()) { 
       case STRING: 
        System.out.println(cell.getRichStringCellValue().getString()); 
        break; 
       case NUMERIC: 
        if (DateUtil.isCellDateFormatted(cell)) { 
         System.out.println(cell.getDateCellValue()); 
        } else { 
         System.out.println(cell.getNumericCellValue()); 
        } 
        break; 
       case BOOLEAN: 
        System.out.println(cell.getBooleanCellValue()); 
        break; 
       case FORMULA: 
        System.out.println(cell.getCellFormula()); 
        break; 
       case BLANK: 
        System.out.println(); 
        break; 
       default: 
        System.out.println(); 
      } 
... 

Busy Developers' Guide to HSSF and XSSF Featuresを読み取るために、常に良いです。

関連する問題