您的位置:首页 > 其它

读取excel文件,支持doc ,docx格式 附带上传方法

2012-10-18 17:59 323 查看
没有用任何现有框架,以后遇见的在写用框架的吧

首先需要的jar包:poi-3.8-beta2-20110408.jar

poi-examples-3.8-beta2-20110408.jar

poi-excelant-3.8-beta2-20110408.jar

poi-ooxml-3.8-beta2-20110408.jar

poi-ooxml-schemas-3.8-beta2-20110408.jar

xmlbeans-2.3.0.jar

openxml4j-1.0-beta.jar

java 代码:

/*
* Java文件操作 获取文件扩展名
*
*  Created on: 2011-8-2
*      Author: blueeagle
*/
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
/*
* Java读取excel 97-2010及以上
*
*  Created on: 2011-8-2
*      Author: blueeagle
*/
public String readExcel(File file) throws IOException{
setSavePath("tmp");
String filePath = ServletActionContext.getServletContext().getRealPath(
this.getSavePath())
+ "\\" + getUploadFileName();
File filepath = new File(filePath);
copy(file, filepath);
String ExtensionName= getExtensionName(filePath);
System.out.println("后缀名:"+ExtensionName);
String value =null;
if(ExtensionName.equals("xls")){
try {
System.out.println("2003");
// 创建对Excel工作簿文件的引用
HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(
filePath));
// 在Excel文档中,第一张工作表的缺省索引是0
// 其语句为:HSSFSheet sheet = workbook.getSheetAt(0);
HSSFSheet sheet = wookbook.getSheet("Sheet1");
// 获取到Excel文件中的所有行数-
int rows = sheet.getPhysicalNumberOfRows();
// 遍历行-
for (int i = 0; i < rows; i++) {
// 读取左上端单元格
HSSFRow row = sheet.getRow(i + 1);
// 行不为空
if (row != null) {
// 获取到Excel文件中的所有的列
int cells = row.getPhysicalNumberOfCells();

// 遍历列
for (int j = 0; j < cells; j++) {
// 获取到列的值-
try {
HSSFCell cell = row.getCell(j);
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value += cell.getNumericCellValue() + "~";
break;
case HSSFCell.CELL_TYPE_STRING:
value += cell.getStringCellValue() + "~";
break;
default:
value += "0";
break;
}
}
} catch (Exception e) {

}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}else if(ExtensionName.equals("xlsx")) {
System.out.println("2007");
// 构造 XSSFWorkbook 对象,strPath 传入文件路径
XSSFWorkbook xwb = new XSSFWorkbook(filePath);
// 读取第一章表格内容
System.out.println("没有读取第一个表格:");
XSSFSheet sheet = xwb.getSheetAt(0);
System.out.println("读取了第一个表格:");
// 定义 row、cell
XSSFRow row;

// 循环输出表格中的内容
for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {

row = sheet.getRow(i+1);
if(row !=null){
for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {
// 通过 row.getCell(j).toString() 获取单元格内容,
if(!row.getCell(j).toString().isEmpty()){
value += row.getCell(j).toString()+"~";
}

}
}

}
}else{
String string="文件类型错误,不是Excel文件!";
addActionMessage(string);
String errorString="error!";
ServletActionContext.getRequest().setAttribute("ss", errorString);
}
filepath.delete();
System.out.println("value:的值"+value);
return value;

}

// -----------上传文件,工具方法---------
private static final int BUFFER_SIZE = 10 * 1024;

/**
*
* @param src
*            源文件
* @param dst
*            目标位置
*/
private static void copy(File src, File dst) {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐