jxl导出excel并下载

本文分享了一段用于导出并下载Excel文件的Java代码,该代码使用了JExcelAPI来读取Excel文件,并尝试通过HTTP响应将其发送给客户端。作者寻求帮助定位代码中的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我有这样一段代码,是要导出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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值