struts2上传Excel文件,再解析...非常头疼,请高手现身!!!!!!!!!!!!!!!!

baiiiu 2011-09-09 12:47:24
用struts2上传excel(07版本)文件到tomcat服务器之后,如何再采用poi来解析excel文件!解析就出问题了报错误:
org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: 'c:\常用链接.xlsx'

由:XSSFWorkbook wb = new XSSFWorkbook(file.getPath()); 这个抛出异常!
分析:是struts2上传文件的时候,需要将临时文件写到一个新的excel文件中去,导致文件部分内存无法读取,或者丢失内容.......

请问大侠们怎么解决此内问题呢?
...全文
1089 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
baiiiu 2011-09-09
  • 打赏
  • 举报
回复
就是这样写的呢!!!!!!
try
{
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];
while (in.read(buffer) > 0)
{
out.write(buffer);
}
}
finally
{
if (null != in)
{
in.close();
}
if (null != out)
{
out.close();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}

[Quote=引用 2 楼 wang12 的回复:]
使用流方式写入,不会出现你说的文件内容不完整的情况。
[/Quote]
wang12 2011-09-09
  • 打赏
  • 举报
回复
使用流方式写入,不会出现你说的文件内容不完整的情况。
baiiiu 2011-09-09
  • 打赏
  • 举报
回复
大牛们呢????没人遇到此内问题么????????????????????
baiiiu 2011-09-09
  • 打赏
  • 举报
回复
谢谢softroad 大牛啊!!!!!!!
我在csdn上面提了几个过问题都是你的回复帮我解决了问题,而且很直接明了!非常感谢....向大牛学习!!问题解决了!
[Quote=引用 8 楼 softroad 的回复:]
XSSFWorkbook(file.getPath())

参数是流,不是String
XSSFWorkbook(new FileInputStream(file))

参考我写的例子http://topic.csdn.net/u/20110830/11/b96a4b05-d775-494b-bb68-fb41e540ac9d.html
[/Quote]
softroad 2011-09-09
  • 打赏
  • 举报
回复
XSSFWorkbook(file.getPath())

参数是流,不是String
XSSFWorkbook(new FileInputStream(file))

参考我写的例子http://topic.csdn.net/u/20110830/11/b96a4b05-d775-494b-bb68-fb41e540ac9d.html
安心逍遥 2011-09-09
  • 打赏
  • 举报
回复
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("对不起,读取出错...");
}
}
}
安心逍遥 2011-09-09
  • 打赏
  • 举报
回复
我给你贴代码,保证没错。前不久刚做的。给分吧
baiiiu 2011-09-09
  • 打赏
  • 举报
回复
哥哥我就是用流写的呢!但是就是这样出问题了!!LS的就是我自己写的啊...
[Quote=引用 4 楼 yanjinye 的回复:]
不要用poi 按LS说的,用流的方式试试!
[/Quote]
随风醉舞 2011-09-09
  • 打赏
  • 举报
回复
不要用poi 按LS说的,用流的方式试试!

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧