六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 36|回复: 0

jxl导出excel并下载

[复制链接]

升级  30%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
15
 楼主| 发表于 2013-1-28 19:23:28 | 显示全部楼层 |阅读模式
我有这样一段代码,是要导出excel并下载,请高手帮我看看错在什么地方?
public String excelExport() throws Exception{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();

Workbook workbook = Workbook.getWorkbook(new File("D:\\test1.xls"));
Sheet sheet = workbook.getSheet(0);

double rowTotal = 0;
double colTotal = 0;
List<String> rowTotalList = new ArrayList<String>();
String rowContent;
try{
int col = sheet.getColumns();
int row = sheet.getRows();
String result[][] = new String[col][row];
Cell cell;
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
try {
cell = sheet.getCell(i, j);
result[i][j] = cell.getContents();
} catch (Exception e) {
}
}
}
for (int i = 0; i < col; i++) {
for (int j = 1; j < row; j++) {
try {
//System.out.println(result[i][j]);

} catch (Exception e) {
e.printStackTrace();
}
}
}
for (int j = 1; j < row; j++) {
//System.out.println(testjobid);
TestPerson tp = new TestPerson();
tp.setTestname(result[0][j]);
tp.setEmail(result[1][j]);
tp.setTelephone(result[2][j]);
tp.setTestjob(testjobManager.get(testjobid));
testPersonManager.save(tp);
rowContent = "<td><div align='center'>" + result[0][j]
+ "</div></td><td><align='center'>" + result[1][j]
+ "</div></td><td><align='center'>" + result[2][j]                       
+ "</div></td>";
rowTotalList.add(rowContent);
rowTotal = 0;
rowContent = null;
}
request.setAttribute("rowTotal", rowTotalList);
request.setAttribute("colTotal", colTotal);
workbook.close();
OutputStream os = response.getOutputStream();
String filename = "D:\\test.xls";
        response.reset();
response.setHeader("Content-disposition", "attachment; filename=test.xls");
response.setContentType("application/msexcel");
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表