JasperPrint 生成包含.XLSX的ZIP压缩文件下载
2016-10-19 11:26
323 查看
1、如何生成打印数据和JasperPrint可看我前面博客,这里不再多说;找不到可点这里 JasperPrint 如何生成
2、其实改变很小,只需将 原先 生成的 .XLSX 文件 添加到 ZIP 里面去就可以;各位看官请看下面代码
//打印预览
@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping(value = "pzip/{prvUuid}", method = {RequestMethod.POST, RequestMethod.GET})
public void pzip(@PathVariable("prvUuid") String prvUuid, HttpServletRequest req, HttpServletResponse resp) {
try {
Object data = req.getSession().getAttribute(prvUuid);//获得 打印相关参数
if (data instanceof Map) {
Map params = (Map) data;
List<String> fileNames = new ArrayList<String>();
Object fileList = params.get(Consts.PRT_JR_DATA_SOURCE);
if(fileList instanceof List){//设置 生成.xlsx文件名
List<CommandVo> list = (List<CommandVo>)fileList;
for(CommandVo vo : list){
String fileName = vo.getCarreierText() + vo.getInitAddrName() + vo.getWaybillNo();
if(StringUtils.isNotBlank(vo.getCarreierText())){
fileName = vo.getCarreierText() + " - ";
}
if(StringUtils.isNotBlank(vo.getInitAddrName())){
fileName = fileName + vo.getInitAddrName() + " - ";
}
fileName = fileName + vo.getWaybillNo();
fileNames.add(fileName);
}
}
ServletContext sc = req.getSession().getServletContext();
prepare(params, sc);//验证模版、转换打印数据属性对应模版属性
ApplicationContext applicationContext = ContextUtil.getApplicationContext();
//获得数据集合
List<ByteArrayOutputStream> osList = new ArrayList<ByteArrayOutputStream>();
Object zipData = params.get(Consts.PRT_JR_DATA_SOURCE);
if(zipData instanceof List){//一条数据一份.xlsx 文件;
List objs = (List)zipData;
for(Object obj : objs){
List<Object> skus = new ArrayList<Object>();
skus.add(obj);
params.put(Consts.PRT_JR_DATA_SOURCE, skus);
JasperPrint jasperPrint = fillReport(params, applicationContext);
ByteArrayOutputStream os = getOutputStream(jasperPrint);
osList.add(os);
}
}
OutputStream os = null;
ZipOutputStream zipOut = null;
String fileName = params.get(Consts.PRT_P_FILENAME) + String.valueOf(SystemClock.getTimeInMillis()) + ".zip";
try {
os = resp.getOutputStream();
resp.setContentType("application/octet-stream;charset=UTF-8");
resp.setHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
zipOut = new ZipOutputStream(os);
// 将单个文件的流添加到压缩文件中
for (int i = 0; i < osList.size(); i++) {
compressFile(osList.get(i), zipOut, fileNames.get(i)+".xlsx");
}
/**zip写入磁盘**/
zipOut.flush();
} catch (Exception e) {
logger.error("报表打包出现错误", e);
} finally {
IOUtils.closeQuietly(zipOut);
}
}
} catch (Exception e) {
logger.error("pexcel()-", e);
}
}
//将打印生成的数据转换成 数据流
private static ByteArrayOutputStream getOutputStream(JasperPrint jasperPrint) throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, os);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter.exportReport();
os.flush();
return os;
}
/**
* 打包zip
*/
private static void compressFile(ByteArrayOutputStream baos, ZipOutputStream zos, String fileName) throws IOException {
ZipEntry entry = new ZipEntry(fileName);
zos.putNextEntry(entry);
zos.write(baos.toByteArray());
zos.flush();
}如此就完成了;
其实就是如此简单。不信你可以自己亲自尝试一下
2、其实改变很小,只需将 原先 生成的 .XLSX 文件 添加到 ZIP 里面去就可以;各位看官请看下面代码
//打印预览
@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping(value = "pzip/{prvUuid}", method = {RequestMethod.POST, RequestMethod.GET})
public void pzip(@PathVariable("prvUuid") String prvUuid, HttpServletRequest req, HttpServletResponse resp) {
try {
Object data = req.getSession().getAttribute(prvUuid);//获得 打印相关参数
if (data instanceof Map) {
Map params = (Map) data;
List<String> fileNames = new ArrayList<String>();
Object fileList = params.get(Consts.PRT_JR_DATA_SOURCE);
if(fileList instanceof List){//设置 生成.xlsx文件名
List<CommandVo> list = (List<CommandVo>)fileList;
for(CommandVo vo : list){
String fileName = vo.getCarreierText() + vo.getInitAddrName() + vo.getWaybillNo();
if(StringUtils.isNotBlank(vo.getCarreierText())){
fileName = vo.getCarreierText() + " - ";
}
if(StringUtils.isNotBlank(vo.getInitAddrName())){
fileName = fileName + vo.getInitAddrName() + " - ";
}
fileName = fileName + vo.getWaybillNo();
fileNames.add(fileName);
}
}
ServletContext sc = req.getSession().getServletContext();
prepare(params, sc);//验证模版、转换打印数据属性对应模版属性
ApplicationContext applicationContext = ContextUtil.getApplicationContext();
//获得数据集合
List<ByteArrayOutputStream> osList = new ArrayList<ByteArrayOutputStream>();
Object zipData = params.get(Consts.PRT_JR_DATA_SOURCE);
if(zipData instanceof List){//一条数据一份.xlsx 文件;
List objs = (List)zipData;
for(Object obj : objs){
List<Object> skus = new ArrayList<Object>();
skus.add(obj);
params.put(Consts.PRT_JR_DATA_SOURCE, skus);
JasperPrint jasperPrint = fillReport(params, applicationContext);
ByteArrayOutputStream os = getOutputStream(jasperPrint);
osList.add(os);
}
}
OutputStream os = null;
ZipOutputStream zipOut = null;
String fileName = params.get(Consts.PRT_P_FILENAME) + String.valueOf(SystemClock.getTimeInMillis()) + ".zip";
try {
os = resp.getOutputStream();
resp.setContentType("application/octet-stream;charset=UTF-8");
resp.setHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
zipOut = new ZipOutputStream(os);
// 将单个文件的流添加到压缩文件中
for (int i = 0; i < osList.size(); i++) {
compressFile(osList.get(i), zipOut, fileNames.get(i)+".xlsx");
}
/**zip写入磁盘**/
zipOut.flush();
} catch (Exception e) {
logger.error("报表打包出现错误", e);
} finally {
IOUtils.closeQuietly(zipOut);
}
}
} catch (Exception e) {
logger.error("pexcel()-", e);
}
}
//将打印生成的数据转换成 数据流
private static ByteArrayOutputStream getOutputStream(JasperPrint jasperPrint) throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, os);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter.exportReport();
os.flush();
return os;
}
/**
* 打包zip
*/
private static void compressFile(ByteArrayOutputStream baos, ZipOutputStream zos, String fileName) throws IOException {
ZipEntry entry = new ZipEntry(fileName);
zos.putNextEntry(entry);
zos.write(baos.toByteArray());
zos.flush();
}如此就完成了;
其实就是如此简单。不信你可以自己亲自尝试一下
相关文章推荐
- PHP网页上打包文件生成压缩文件zip,并弹出下载
- Struts2下载zip压缩文件-不生成临时文件
- C#压缩指定的文件并生成zip文件
- IE下载docx(xlsx,pptx)扩展名的文件时,会自动将扩展名变为zip
- Android 下载zip压缩文件并解压
- 下载包含src,tgz,zip的文件
- PB8调用使用C++与C#分别编写生成解压缩带有密码的zip压缩文件的动态链接库dll(部分内容转自互联网)
- 利用apache commons生成压缩文件下载
- asp.net 文件压缩zip下载
- IE下载xlsx、docx、pptx文件时自动变成zip文件问题
- Java实现zip压缩多个文件下载
- php生成zip压缩文件实例代码讲解
- 提供下载:批量图片压缩(生成缩略图或指定大小图片)及文件批量改名器
- zip压缩、文件下载
- Java批量下载生成zip文件
- 【转】Django下载大文件和压缩zip文件
- SEO_ASP.net SEO优化(包含URL地址重写\viewState移动和压缩至服务器\SEO信息XML生成_根据URL地址重写文件)\web网站内容压缩 源码公开.本人授权可使用于商业项目。
- zip与unzip命令谅解及,压缩文件但不包含某目录的操作。
- IE下载docx(xlsx,pptx)文件时,会自动将扩展名变为zip
- Java打包下载ZIP压缩文件