POI读取Excel 03/07
程序员文章站
2023-01-22 22:11:12
java application可以成功读取excel 03及excel 07,但是在jsp servlet中却莫名其妙不能读取excel 07(.xlsx)中的内容,只能读取excel 03(.x...
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);
}
}
}
}
作者:angus_17
/**
* 需要如下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);
}
}
}
}
作者:angus_17
上一篇: 炖鸭子怎么做好吃?这样做健康又有营养!
下一篇: 带你了解什么是限流
推荐阅读
-
Java使用poi包读取Excel文档代码分享
-
Java读取、写入Excel全版本(包含xls、xslx格式)通用方法及代码展示(POI)
-
poi读取excel数据(poi读取excel数字类型)
-
poi读取excel数据(poi读取excel数字类型)
-
Java读取、写入Excel全版本(包含xls、xslx格式)通用方法及代码展示(POI)
-
JAVA使用POI(XSSFWORKBOOK)读取EXCEL文件过程解析
-
POI读取Excel 03/07
-
poi xssf读取excel2007
-
poi xssf读取excel2007
-
Excel(大)文件写入03版本和07版本(HSSF-XSSF-SXSSF)