基於JXL/POI excel單元格內容的判斷(公式單元格)
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.eval.NotImplementedException;
import jxl.Cell;
import jxl.CellType;
import jxl.FormulaCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Test1 {
public static void main(String[] args) {
/*
* JXL讀取excel將單元格中存在錯誤的單元格進行過濾
*/
//程式中對公式單元格的型別判斷並輸出內容,當單元格的公式存在錯誤的場合使用POI獲取workbook
//就會發生異常,以下注釋的程式碼可以檢查存在的錯誤並給以處理,但這種處理不能修改公式,
//而是將公式去掉只保留單元格內容,這樣在判斷公式單元格的時候輸出型別就沒差別了。
/*
try {
WritableWorkbook workbook = Workbook.createWorkbook(new File("E:/test1.xls"),
Workbook.getWorkbook(new File("E:/test.xls")));
WritableSheet[] sheets =workbook.getSheets();
for(int ix=0; ix<sheets.length; ix++){
for(int iy=0; iy<sheets[ix].getRows();iy++){
for(int iz=0; iz<sheets[ix].getColumns();iz++){
WritableCell wcell = sheets[ix].getWritableCell(iz,iy);
if(wcell.getType() == CellType.NUMBER_FORMULA || wcell.getType() == CellType.STRING_FORMULA || wcell.getType() == CellType.BOOLEAN_FORMULA
|| wcell.getType() == CellType.DATE_FORMULA || wcell.getType() == CellType.FORMULA_ERROR){
Label nCell = new Label(iz, iy, wcell.getContents(), wcell.getCellFormat());
sheets[ix].addCell(nCell);
}
}
}
}
workbook.write();
workbook.close();
} catch (Exception e) {
System.out.println("JXL獲取workbook報錯!" + e.toString());
}
*/
HSSFWorkbook hssfwb = null;
try {// POI獲取workbook
fileIn = new FileInputStream("E:/test.xls");
hssfwb = new HSSFWorkbook(fileIn, true);
} catch (Exception e) {
System.out.println("POI獲取workbook報錯!" + e.toString());
} finally {
if (fileIn != null) {
try {
fileIn.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
try {
// JXL獲取workbook
Workbook wb = Workbook.getWorkbook(new File("E:/test.xls"));
// 取得sheet數
int sheetNum = wb.getNumberOfSheets();
for(int i=0; i<sheetNum; i++){
System.out.println("第 "+ (i+1)+ " 個SHEET!");
printCellFormatValue(wb.getSheet(i),hssfwb.getSheetAt(i));
}
} catch (Exception e) {
System.out.println("JXL獲取workbook報錯!" + e.toString());
}
}
/**
* 通過jxl、poi輸出excel單元格內容
*/
public static void printCellFormatValue(Sheet sheet,HSSFSheet sheetPOI){
for(int i=0; i<sheet.getRows(); i++){
Cell[] cells = sheet.getRow(i);
for(int j=0; j<cells.length; j++){
// 判斷單元格是否為公式型別
if (cells[j] instanceof FormulaCell) {
// 在前面處理過公式,故hcell肯定不為null
HSSFCell hcell = sheetPOI.getRow(i).getCell(j);
// POI計算公式,得出結果
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(sheetPOI.getWorkbook());
try{
evaluator.setCurrentRow(sheetPOI.getRow(i));
int eva = evaluator.evaluateFormulaCell(hcell);
switch (eva) {
case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN:
System.out.println("第" + (i+1) + "行,第" + (j+1) + "列單元格值為:"+ hcell.getBooleanCellValue() + ",型別為:CELL_TYPE_BOOLEAN");
break;
case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC:
NumberCell numCell = (NumberCell) cells[j];
System.out.println("第" + (i+1) + "行,第" + (j+1) + "列單元格值為:"+ numCell.getNumberFormat().format(hcell.getNumericCellValue()) + ",型別為:CELL_TYPE_NUMERIC");
break;
case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING:
System.out.println("第" + (i+1) + "行,第" + (j+1) + "列單元格值為:"+ hcell.getStringCellValue() + ",型別為:CELL_TYPE_STRING");
break;
case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK:
break;
case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_ERROR:
break;
case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA:
String value = String.valueOf(hcell.getNumericCellValue());
System.out.println("第" + (i+1) + "行,第" + (j+1) + "列單元格值為:"+ value + ",型別為:CELL_TYPE_FORMULA");
//如果獲取的資料值為非法值,則轉換為獲取字串
if(value.equals("NaN")){
value = hcell.getRichStringCellValue().toString();
System.out.println("第" + (i+1) + "行,第" + (j+1) + "列單元格資料值非法值,值為:"+ value + ",型別為:CELL_TYPE_FORMULA");
}
break;
}
}catch(NotImplementedException ee){
System.out.println("第" + (i+1) + "行,第" + (j+1) + "列單元格資料值非法值,型別為:CELL_TYPE_FORMULA");
}
}else{
System.out.println("第" + (i+1) + "行,第" + (j+1) + "列單元格值:"+ cells[j].getContents());
}
}
}
}
}