POI读取Excel 03/07
来源:互联网 发布:从零开始学编程 编辑:程序博客网 时间:2024/06/11 07:27
Java Application可以成功读取excel 03及excel 07,但是在JSP servlet中却莫名其妙不能读取excel 07(.xlsx)中的内容,只能读取excel 03(.xls)中的内容。问题没有解决。
/**
* 需要如下jar包.
* poi-3.7-20101029.jar, poi-ooxml-3.7-20101029.jar,
* xmlbeans-2.3.0.jar, poi-ooxml-schemas-3.7-20101029.jar, dom4j-1.6.1.jar
*/
import java.io.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
public class POIExcelUtils {
public POIExcelUtils() {
}
/**
* 读取excel,支持excel 97~03 / excel 07
* @param fileName : 文件名
*/
public void read(String fileName) {
Workbook wb = null;
File f = new File(fileName);
FileInputStream is;
try {
is = new FileInputStream(f);
wb = WorkbookFactory.create(is);
readWB(wb);
is.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (InvalidFormatException e) {
System.out.println(e.getMessage());
}
}
/**
* 读取excel,支持excel 97~03 / excel 07
* @param is : 文件流
*/
public void read(InputStream is) {
Workbook wb;
try {
wb = WorkbookFactory.create(is);
readWB(wb);
is.close();
} catch (InvalidFormatException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
* 读取Workbook
* @param wb
* @throws Exception
*/
private void readWB(Workbook wb){
try {
// 读取sheet0
//for (int k = 0; k < wb.getNumberOfSheets(); k++) {
//sheet
//Sheet sheet = wb.getSheetAt(k);
Sheet sheet = wb.getSheetAt(0);
readRows(sheet); // 按行读取
//-- test
/*
System.out.println("PhysicalNumberOfRows:"+sheet.getPhysicalNumberOfRows());
System.out.println("FirstRowNum:"+sheet.getFirstRowNum());
System.out.println("LastRowNum:"+sheet.getLastRowNum());
*/
//}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* 读取每一行
* @param rows : 有效行数 /非空行数
*/
private void readRows(Sheet sheet) {
int rows = sheet.getPhysicalNumberOfRows();
System.out.println(rows);
int rowIndex = 0; //每行索引
int notnullRowIndex = 0; //非空行索引
while (notnullRowIndex < rows) {
Row row = sheet.getRow(rowIndex);
rowIndex++;
if (row != null) {
readCells(row);
notnullRowIndex++;
}
}
}
/**
* 读取每一行的单元格
* @param row : 所在行数据
*/
private void readCells(Row row) {
int cells = row.getPhysicalNumberOfCells();
int cellIndex = 0; //单元格索引
int notnullCellIndex = 0; //非空单元格索引
while(notnullCellIndex < cells) {
Cell cell = row.getCell(cellIndex);
cellIndex++;
if (cell != null) {
String value = null;
switch (cell.getCellType()) {
case Cell.CELL_TYPE_FORMULA:
value = "FORMULA value=" + cell.getCellFormula();
break;
case Cell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell)){
value = "DATE value=" + cell.getDateCellValue();
}else{
value = "NUMERIC value=" + cell.getNumericCellValue();
}
break;
case Cell.CELL_TYPE_STRING:
value = "STRING value=" + cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
value = "BOOLEAN value="
+ cell.getBooleanCellValue();
break;
default:
}
notnullCellIndex++;
System.out.println(value);
}
}
}
}
- POI读取Excel 03/07
- POI读取Excel 03/07
- java poi 读取excel(兼容03,07)
- poi 读取excel(03和07版本)--小结
- POI-----POI读取Excel实例
- POI读取EXCEL
- POI读取EXCEL教程
- POI读取EXCEL
- poi 读取excel
- 通过POI读取Excel
- POI读取Excel
- POI 读取 Excel
- POI读取Excel浅谈
- POI 读取EXcel
- POI读取EXCEL教程
- POI读取Excel
- POI读取Excel
- POI读取Excel浅谈
- C语言 猜字游戏
- 判断两个矩形是否存在交集
- JAX-WS和JAX-RPC方式调用Web Service的Java客户端
- linux目录结构
- 周鸿祎谈做手机:我觉得马化腾不可能不动
- POI读取Excel 03/07
- Pingdom:全球排名前1万网站有24.3%使用了Facebook官方插件
- [转]C# GDI+ 简单绘图(一)
- 用NSMutableArray 存储结构体 CLLocationCoordinate2D
- vs2010 包含库头文件,库文件 配置
- java执行ping命令
- ibatis 返回list<String[]>----resultClass="hashMap" 可以实现
- c# is和as的区别
- 查询数据库中所有记录总数不为0的数据表名称