2013-12-16 75 views
0

我正在使用以下代碼,在「獲取單元格內容」標題下的「http://poi.apache.org/spreadsheet/quick-guide.html#Iterator」上閱讀。即使在編譯之前,它也會出現很多錯誤。我在最後提到了錯誤。我想要做的是將excel表單中的單元格的內容與其Web應用中字段的內容進行比較,該字段的ID可以說是「label22」獲取Excell文件的單元格中的值不起作用

import java.io.FileInputStream; 
import java.io.InputStream; 

import org.apache.poi.hssf.extractor.ExcelExtractor; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.hssf.util.CellReference; 
import org.apache.poi.poifs.filesystem.POIFSFileSystem; 
import org.apache.poi.ss.usermodel.DateUtil; 
import org.apache.poi.ss.usermodel.Row; 



public class ReadCell { 
public static void main(String[] args) { 
InputStream inp = new FileInputStream("D:\\rptViewer.xls"); 
HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp)); 
ExcelExtractor extractor = new ExcelExtractor(wb); 

extractor.setFormulasNotResults(true); 
extractor.setIncludeSheetNames(false); 
String text = extractor.getText(); 



/* 
* Read excel file using j excel file 
*/ 

Sheet sheet1 = wb.getSheetAt(0); 
for (Row row : sheet1) { 
for (Cell cell : row) { 
    CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex()); 
    System.out.print(cellRef.formatAsString()); 
    System.out.print(" - "); 

    switch (cell.getCellType()) { 
     case Cell.CELL_TYPE_STRING: 
      System.out.println(cell.getRichStringCellValue().getString()); 
      break; 
     case Cell.CELL_TYPE_NUMERIC: 
      if (DateUtil.isCellDateFormatted(cell)) { 
       System.out.println(cell.getDateCellValue()); 
      } else { 
       System.out.println(cell.getNumericCellValue()); 
      } 
      break; 
     case Cell.CELL_TYPE_BOOLEAN: 
      System.out.println(cell.getBooleanCellValue()); 
      break; 
     case Cell.CELL_TYPE_FORMULA: 
      System.out.println(cell.getCellFormula()); 
      break; 
     default: 
      System.out.println(); 
     } 
    } 
} 
} 

錯誤是:

getColumnIndex : The method getColumnIndex() is undefined for the type Cell 
wb.getSheetAt(0): Type mismatch: cannot convert from HSSFSheet to Sheet 
formatAsString()); : The method formatAsString() is undefined for the type CellReference 
getCellType()) { :The method getCellType() is undefined for the type Cell 
getRichStringCellValue() : The method getRichStringCellValue() is undefined for the type Cell 
CELL_TYPE_NUMERIC: : CELL_TYPE_NUMERIC cannot be resolved or is not a field 
+0

? – TheLostMind

+0

讓我編輯它。 – newLearner

回答

1

你的進口是錯誤的。使用方法:的

import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Sheet; 

代替:

import jxl.Cell; 
import jxl.Sheet; 
+0

導入上述內容後,只剩下兩個錯誤: 'formatAsString());':方法formatAsString()未定義爲類型CellReference – newLearner

+0

,另一個爲:'wb.getSheetAt(0);' 類型不匹配:無法從HSSFSheet轉換爲工作簿 – newLearner

+1

請查看POI javadoc。查看類名稱,方法和返回類型。這樣你就可以自己解決所有的錯誤,甚至從中學到一些東西...... – BobTheBuilder

1

JXL(現 'JExcel')和Apache POI可用於Java開發者兩個單獨的Excel的API與Microsoft Excel的接口連接。

你可以肯定不會期望這兩者相互兼容,這就是爲什麼你會得到類型不匹配錯誤。

(你應該只使用POI或僅JXL) 的POI可以導入

import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFCell; 

,改變你的代碼,你爲什麼要同時使用POI和JXL

HSSFSheet sheet1 = wb.getSheetAt(0); 
+0

我的觀點正是......我不知道他爲什麼使用JXL和POI ...... – TheLostMind