您的位置:首页 > 编程语言 > ASP

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();
}如此就完成了;

其实就是如此简单。不信你可以自己亲自尝试一下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: