jxls根据模板导出excel

本文介绍了一个Java程序示例,该程序用于从数据库获取数据并将其导出为Excel文件。通过使用模板文件和XLSTransformer工具类,可以方便地实现数据到Excel表格的转换。文章提供了完整的代码实现及必要的资源链接。

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

public class ExportExcel{
HttpServletRequest request;
HttpServletResponse response;
List saff;
String fileName;
public ExportExcel(HttpServletRequest request, HttpServletResponse response, List saff, String fileName ){
this.request=request;
this.response=response;
this.saff=saff;
this.fileName=fileName;
}
public void exportProject(String sourceName)throws Exception{
Map<String,Object> map=new HashMap<>();
map.put("fileName",fileName);
map.put("testforms",saff);
//获得模板路径
String path=request.getServletContext().getRealPath("excel_export");
String wjpath=path+ File.separator+sourceName;
//准备输出流
OutputStream os=null;
try {
//设置响应头
response.setContentType("application/x-excel");
//response.setHeader("Content-Disposition", "attachment;filename=studentInfo.xls");
response.setHeader("Content-Disposition", "attachment;filename="+new String((fileName+".xls").getBytes(),"iso8859-1"));
response.setContentType("application/ms-excel");
os = response.getOutputStream();
XLSTransformer transformer = new XLSTransformer();
try {
//获得模板的输入流
InputStream in = new BufferedInputStream(new FileInputStream(wjpath));
//将beans通过模板输入流写到workbook中
Workbook workbook = transformer.transformXLS(in, map);
//将workbook中的内容用输出流写出去
workbook.write(os);
os.flush();

}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
} catch (Exception e) {
e.printStackTrace();
}
}

}


demo下载地址  http://download.youkuaiyun.com/download/maple980326/9959219

相关博客地址: http://blog.youkuaiyun.com/yuliqi0429/article/details/41907711

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值