You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
6.2 KiB
Java
138 lines
6.2 KiB
Java
3 years ago
|
package com.glxp.api.util;
|
||
|
|
||
|
import cn.hutool.json.JSONUtil;
|
||
|
import com.alibaba.fastjson.JSON;
|
||
|
import net.sf.jasperreports.engine.*;
|
||
|
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
|
||
|
import net.sf.jasperreports.engine.query.JsonQueryExecuterFactory;
|
||
|
import net.sf.jasperreports.engine.util.JRLoader;
|
||
|
import net.sf.jasperreports.export.SimpleExporterInput;
|
||
|
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
|
||
|
import org.slf4j.Logger;
|
||
|
import org.slf4j.LoggerFactory;
|
||
|
import org.springframework.stereotype.Component;
|
||
|
|
||
|
import javax.servlet.ServletOutputStream;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.*;
|
||
|
import java.nio.charset.StandardCharsets;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Locale;
|
||
|
import java.util.Map;
|
||
|
|
||
|
/**
|
||
|
* Jasperreport工具类
|
||
|
*
|
||
|
* @author chenqf
|
||
|
* @date 2021-11-10 22:23
|
||
|
*/
|
||
|
@Component
|
||
|
public class JasperUtils {
|
||
|
private static Logger logger = LoggerFactory.getLogger(JasperUtils.class);
|
||
|
|
||
|
public static final String PDF_TYPE = "pdf";
|
||
|
public static final String WORD_TYPE = "word";
|
||
|
public static final String EXCEL_TYPE = "excel";
|
||
|
public static final String HTML_TYPE = "html";
|
||
|
|
||
|
/*
|
||
|
* data 传入报表数据
|
||
|
* jasperResource 模板文件路径
|
||
|
*/
|
||
|
public static void jasperReport(HttpServletRequest request, HttpServletResponse response, Map<String, Object> data, String jasperResource, String type) throws IOException, JRException {
|
||
|
try {
|
||
|
HashMap<String, Object> map = new HashMap<String, Object>();
|
||
|
logger.error(JSON.toJSONString(data));
|
||
|
InputStream is = new ByteArrayInputStream(JSONUtil.toJsonStr(data).getBytes(StandardCharsets.UTF_8));
|
||
|
map.put("JSON_INPUT_STREAM", is); //填充报表数据
|
||
|
map.put(JsonQueryExecuterFactory.JSON_LOCALE, Locale.ENGLISH);
|
||
|
FileInputStream jasperStream = new FileInputStream(new File(jasperResource));
|
||
|
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
|
||
|
//报表填充
|
||
|
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map);
|
||
|
String fileName = new String("export.pdf".getBytes("GBK"), "ISO8859_1");
|
||
|
ServletOutputStream os = response.getOutputStream();
|
||
|
response.setDateHeader("Expires", 0);
|
||
|
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
|
||
|
if (type != null && WORD_TYPE.equals(type)) {
|
||
|
response.setContentType("appliction/msword; charset=utf-8");
|
||
|
JRDocxExporter exporter = new JRDocxExporter();
|
||
|
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
||
|
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(os));
|
||
|
exporter.exportReport();
|
||
|
} else {
|
||
|
response.setContentType("application/pdf; charset=utf-8");
|
||
|
JasperExportManager.exportReportToPdfStream(jasperPrint, os);
|
||
|
os.flush();
|
||
|
os.close();
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void jasperReport(HttpServletRequest request, HttpServletResponse response, String data, String jasperResource, String type) throws IOException, JRException {
|
||
|
try {
|
||
|
HashMap<String, Object> map = new HashMap<String, Object>();
|
||
|
logger.error(JSON.toJSONString(data));
|
||
|
InputStream is = new ByteArrayInputStream(data.getBytes("UTF-8"));
|
||
|
map.put("JSON_INPUT_STREAM", is); //填充报表数据
|
||
|
map.put(JsonQueryExecuterFactory.JSON_LOCALE, Locale.ENGLISH);
|
||
|
FileInputStream jasperStream = new FileInputStream(new File(jasperResource));
|
||
|
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
|
||
|
//报表填充
|
||
|
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map);
|
||
|
String fileName = new String("export.pdf".getBytes("GBK"), "ISO8859_1");
|
||
|
ServletOutputStream os = response.getOutputStream();
|
||
|
response.setDateHeader("Expires", 0);
|
||
|
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
|
||
|
if (type != null && WORD_TYPE.equals(type)) {
|
||
|
response.setContentType("appliction/msword; charset=utf-8");
|
||
|
JRDocxExporter exporter = new JRDocxExporter();
|
||
|
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
||
|
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(os));
|
||
|
exporter.exportReport();
|
||
|
} else {
|
||
|
response.setContentType("application/pdf; charset=utf-8");
|
||
|
JasperExportManager.exportReportToPdfStream(jasperPrint, os);
|
||
|
os.flush();
|
||
|
os.close();
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
|
||
|
e.printStackTrace();
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
public static void jasperReportToFile(String filePath, Map<String, Object> data, String jasperResource) throws IOException, JRException {
|
||
|
try {
|
||
|
HashMap<String, Object> map = new HashMap<String, Object>();
|
||
|
logger.error(JSON.toJSONString(data));
|
||
|
InputStream is = new ByteArrayInputStream(JSONUtil.toJsonStr(data).getBytes(StandardCharsets.UTF_8));
|
||
|
map.put("JSON_INPUT_STREAM", is); //填充报表数据
|
||
|
map.put(JsonQueryExecuterFactory.JSON_LOCALE, Locale.ENGLISH);
|
||
|
// ClassPathResource resource = new ClassPathResource(jasperResource);
|
||
|
FileInputStream jasperStream = new FileInputStream(new File(jasperResource));
|
||
|
// InputStream jasperStream = resource.getInputStream();
|
||
|
|
||
|
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
|
||
|
//报表填充
|
||
|
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map);
|
||
|
File createFile = new File(filePath);
|
||
|
if (!createFile.exists()) {
|
||
|
createFile.createNewFile();
|
||
|
}
|
||
|
JasperExportManager.exportReportToPdfFile(jasperPrint, filePath);
|
||
|
|
||
|
} catch (Exception e) {
|
||
|
|
||
|
e.printStackTrace();
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|