使用jexcelapi生成excel保存在客户端

使用jexcelapi导出Excel,可以很容易的生成Excel.但生成的excel是在服务器端。但是看了伟库的导出excel的功能,是像我们在网上下载文件一样,生成后弹出一个对话框让用户选择保存地址,下载到客户端本地。于是研究了下觉得应该以流的形式来处理

action代码如下
public ActionForward exeExcel(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception {
System.out.println("exeExcel");

try
{
response.reset();
// response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition","attachment; filename=ete_vip_data.xls");
response.setContentType("application/msexcel");
//打开文件
//WritableWorkbook book= Workbook.createWorkbook(new File("c://测试.xls"));//这样写就生成在服务器端c盘了
WritableWorkbook book=Workbook.createWorkbook(response.getOutputStream());//生成的文件以流的形式输出

//生成名为“第一页”的工作表,参数0表示这是第一页
WritableSheet sheet=book.createSheet("第一页",0);
//在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
//单元格内容为test
Label label=new Label(0,0,"test");
//将定义好的单元格添加到工作表中
sheet.addCell(label);

book.write();
book.close();

response.getOutputStream().flush();
response.getOutputStream().close();

}catch(Exception e)
{
System.out.println(e);
}
return null;
}

直接访问此action就会提示出下载excel.
jexcelapi下载:[url]http://jexcelapi.sourceforge.net[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值