poi读取excel,包含所有excel版本,兼容excel2003和2007以上版本
- 格式:docx
- 大小:15.07 KB
- 文档页数:6
import ermodel.HSSFWorkbook;
import ermodel.Cell;
import ermodel.DateUtil;
private static final String EXTENSION_XLS = "xls";
private static final String EXTENSION_XLSX = "xlsx";
/**
*判断EXCEL版本
* @param in
* @param filename
* @return
//读取数据行
for (int rowIndex = 0; rowIndex <= lastRowIndex; rowIndex++) {
Row currentRow = sheet.getRow(rowIndex);//当前行
int firstColumnIndex = currentRow.getFirstCellNum(); //首列
String currentCellValue = this.getCellValue(currentCell, true);//当前单元格的值
System.out.print(currentCellValue + "\t");
}
System.out.println("");
}
System.out.println("\t");
break;
case Cell.CELL_TYPE_BLANK: //空白
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_ERROR: //错误
cellValue = "错误";
break;
case Cell.CELL_TYPE_FORMULA: //公式
String cellValue = null;
int cellType = cell.getCellType();
switch (cellType) {
case Cell.CELL_TYPE_STRING: //文本
cellValue = cell.getStringCellValue();
break;
int lastColumnIndex = currentRow.getLastCellNum();//最后一列
for (int columnIndex = firstColumnIndex; columnIndex <= lastColumnIndex; columnIndex++) {
Cell currentCell = currentRow.getCell(columnIndex);//当前单元格
*/
public void readExcel(String filePath) throws FileNotFoundException,FileFormatException {
//检查
preReadCheck(filePath);
//获取workbook对象
Workbook workbook = null;
FileFormatException {
//常规检查
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException("传入的文件不存在:" + filePath);
}
if (!(filePath.endsWith(EXTENSION_XLS) || filePath
* @param filePath
* @throws FileNotFoundException
* @throws FileFormatException
*/
private void preReadCheck(String filePath) throws FileNotFoundException,
//if (workbook != null) {
//try {
//workbook.close();
//} catch (IOException e) {
//e.printStackTrace();
//}
//}
}
}
/**
*取单元格的值
*
* @param cell
*单元格对象
* @param treatAsStr
.endsWith(EXTENSION_XLSX))) {
throw new FileFormatException("传入的文件不是excel");
}
}
/**
*读取EXCEL
* @param filePath
* @throws FileNotFoundException
* @throws FileFormatException
FileFormatException {
TestReadExcelAll e3 = new TestReadExcelAll();
e3.readExcel("D://1.xls");
e3.readExcel("D://2.xlsx");
}
}
cellValue = "错误";
break;
default:
cellValue = "错误";
}
return cellValue;
}
/**
*测试
* @param args
* @throws FileNotFoundException
* @throws FileFormatException
*/
public static void main(String[] args) throws FileNotFoundException,
int lastRowIndex = sheet.getLastRowNum();
if (firstRowIndex != lastRowIndex && lastRowIndex != 0) {
System.out.println("======================="+ sheet.getSheetName()+ "=========================");
wb = new HSSFWorkbook(in);//Excel 2003
} else if(filename.endsWith(EXTENSION_XLSX)){
wb = new XSSFWorkbook(in);//Excel 2007
}
return wb;
}
/**
*文件校验是否是excel
*
*为true时,当做文本来取值(取到的是文本,不会把“1”取成“1.0”)
ring getCellValue(Cell cell, boolean treatAsStr) {
if (cell == null) {
return "";
}
if (treatAsStr) {
import ermodel.XSSFWorkbook;
import org.apache.xmlbeans.impl.piccolo.io.FileFormatException;
/**
*读取EXCEL内容
* @author lxr
*
*/
public class TestReadExcelAll {
* @throws IOException
*/
public static Workbook getWorkbook(InputStream in,String filename)throws IOException
{
Workbook wb = null;
if(filename.endsWith(EXTENSION_XLS)){
package com.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
case Cell.CELL_TYPE_NUMERIC: //数字、日期
if (DateUtil.isCellDateFormatted(cell)) {
cellValue = fmt.format(cell.getDateCellValue()); //日期型
} else {
cell.setCellType(Cell.CELL_TYPE_STRING);
cellValue = String.valueOf(cell.getNumericCellValue()); //数字
}
break;
case Cell.CELL_TYPE_BOOLEAN: //布尔型
cellValue = String.valueOf(cell.getBooleanCellValue());
for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {