struts2上传Excel文件 poi再解析.
package com.triman.utils;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ReadExcelUtil {
private HSSFWorkbook wb = null;
private HSSFSheet sheet = null;
private HSSFRow row = null;
private int sheetNum = 0;
private int rowNum = 0;
private FileInputStream fis = null;
private File file = null;
public ReadExcelUtil() {
}
public ReadExcelUtil(File file) {
this.file = file;
}
public void setRowNum(int rowNum) {
this.rowNum = rowNum;
}
public void setSheetNum(int sheetNum) {
this.sheetNum = sheetNum;
}
public void setFile(File file) {
this.file = file;
}
/**
* 读取excel文件获得HSSFWorkbook对象
*/
public void open() throws IOException {
fis = new FileInputStream(file);
wb = new HSSFWorkbook(new POIFSFileSystem(fis));
fis.close();
}
/**
* 返回sheet表数目
*/
public int getSheetCount() {
int sheetCount = -1;
sheetCount = wb.getNumberOfSheets();
return sheetCount;
}
/**
* sheetNum下的记录行数
*/
public int getRowCount() {
if (wb == null)
System.out.println(“=============>WorkBook为空”);
HSSFSheet sheet = wb.getSheetAt(this.sheetNum);
int rowCount = -1;
rowCount = sheet.getLastRowNum();
return rowCount;
}
/**
* 读取指定sheetNum的rowCount
*/
public int getRowCount(int sheetNum) {
HSSFSheet sheet = wb.getSheetAt(sheetNum);
int rowCount = -1;
rowCount = sheet.getLastRowNum();
return rowCount;
}
/**
* 得到指定行的内容
*/
public String[] readExcelLine(int lineNum) {
return readExcelLine(this.sheetNum, lineNum);
}
/**
* 指定工作表和行数的内容
*/
public String[] readExcelLine(int sheetNum, int lineNum) {
if (sheetNum < 0 || lineNum < 0)
return null;
String[] strExcelLine = null;
try {
sheet = wb.getSheetAt(sheetNum);
row = sheet.getRow(lineNum);
int cellCount = row.getLastCellNum();
strExcelLine = new String[cellCount + 1];
for (int i = 0; i <= cellCount; i++) {
strExcelLine[i] = readStringExcelCell(lineNum, i);
}
} catch (Exception e) {
e.printStackTrace();
}
return strExcelLine;
}
/**
* 读取指定列的内容
*/
public String readStringExcelCell(int cellNum) {
return readStringExcelCell(this.rowNum, cellNum);
}
/**
* 指定行和列编号的内容
*/
public String readStringExcelCell(int rowNum, int cellNum) {
return readStringExcelCell(this.sheetNum, rowNum, cellNum);
}
/**
* 指定工作表、行、列下的内容
*/
public String readStringExcelCell(int sheetNum, int rowNum, int cellNum) {
if (sheetNum < 0 || rowNum < 0)
return “”;
String strExcelCell = “”;
try {
sheet = wb.getSheetAt(sheetNum);
row = sheet.getRow(rowNum);
if (row.getCell((short) cellNum) != null) {
switch (row.getCell((short) cellNum).getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
strExcelCell = “FORMULA “;
break;
case HSSFCell.CELL_TYPE_NUMERIC:
strExcelCell = String.valueOf(row.getCell((short) cellNum).getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
strExcelCell = row.getCell((short) cellNum).getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
strExcelCell = “”;
break;
default:
strExcelCell = “”;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return strExcelCell;
}
/**
* 主函数用于测试
*/
public static void main(String args[]) {
try {
File file = new File(“D:\\test.xls”);
ReadExcelUtil readExcel = new ReadExcelUtil(file);
try {
readExcel.open();
} catch (IOException e) {
e.printStackTrace();
}
readExcel.setSheetNum(0);
int count = readExcel.getRowCount();
for (int i = 0; i <= count; i++) {
String[] rows = readExcel.readExcelLine(i);
for (int j = 0; j < rows.length; j++) {
System.out.print(rows[j] + ” “);
}
System.out.print(“\n”);
}
} catch (Exception e) {
System.out.println(“对不起,读取出错…”);
}
}
}
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理
softroad
softroad
softroad
本版等级:T7
Blank
#8 得分:30 回复于: 2011-09-09 13:25:38
XSSFWorkbook(file.getPath())
参数是流,不是String
XSSFWorkbook(new FileInputStream(file))
参考我写的例子http://topic.csdn.net/u/20110830/11/b96a4b05-d775-494b-bb68-fb41e540ac9d.html
对我有用[0] 丢个板砖[1] 引用 | 举报 | 管理
baiiiu
baiiiu
baiiiu
本版等级:T1
#9 得分:0 回复于: 2011-09-09 14:13:10
谢谢softroad 大牛啊!!!!!!!
我在csdn上面提了几个过问题都是你的回复帮我解决了问题,而且很直接明了!非常感谢….向大牛学习!!问题解决了!
引用 8 楼 softroad 的回复:
XSSFWorkbook(file.getPath())
参数是流,不是String
XSSFWorkbook(new FileInputStream(file))
参考我写的例子http://topic.csdn.net/u/20110830/11/b96a4b05-d775-494b-bb68-fb41e540ac9d.html
http://bbs.csdn.net/topics/370180761