删除与jasperreport相关的内容
This commit is contained in:
parent
55d5be8b79
commit
b181c48583
@ -1,166 +0,0 @@
|
|||||||
package com.ktg.mes.report.controller;
|
|
||||||
|
|
||||||
import com.itextpdf.text.pdf.PdfReader;
|
|
||||||
import com.itextpdf.text.pdf.PdfStamper;
|
|
||||||
import com.itextpdf.text.pdf.PdfWriter;
|
|
||||||
import com.ktg.common.constant.UserConstants;
|
|
||||||
import com.ktg.mes.report.utils.FormatSuffixEnum;
|
|
||||||
import com.ktg.mes.report.utils.JasperContext;
|
|
||||||
import net.sf.jasperreports.engine.*;
|
|
||||||
import net.sf.jasperreports.engine.export.JRPrintServiceExporter;
|
|
||||||
import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter;
|
|
||||||
import net.sf.jasperreports.engine.type.OrientationEnum;
|
|
||||||
import net.sf.jasperreports.engine.util.JRLoader;
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import javax.print.PrintService;
|
|
||||||
import javax.servlet.ServletOutputStream;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
import java.awt.print.PrinterJob;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@RequestMapping(path = "/jasper-report")
|
|
||||||
@RestController
|
|
||||||
public class JasperReportController {
|
|
||||||
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private DataSource dataSource;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private JasperContext jasperContext;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 导出文件
|
|
||||||
* 支持pdf、html、png预览
|
|
||||||
* 支持导出pdf、word、excel、html、png到用户指定目录
|
|
||||||
* http://localhost:2222/jasper-report/export/pdf/inline/report3/sfdf
|
|
||||||
*/
|
|
||||||
@RequestMapping(path = "/export/{fileType}/{openType}/{sourceName}/{fileName}", method = RequestMethod.GET)
|
|
||||||
public void export(@PathVariable("fileType") final String fileType,
|
|
||||||
@PathVariable("openType") final String openType,
|
|
||||||
@PathVariable("sourceName") final String sourceName,
|
|
||||||
@PathVariable("fileName") final String fileName,
|
|
||||||
@RequestParam(required = false) Map<String, Object> query){
|
|
||||||
FormatSuffixEnum suffixEnum = FormatSuffixEnum.getValue(fileType);
|
|
||||||
if (null == suffixEnum) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!jasperContext.setFormatSuffix(suffixEnum)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
JasperPrint jasperPrint = jasperContext.read(query, sourceName);
|
|
||||||
jasperContext.setFormatSuffix(suffixEnum);
|
|
||||||
jasperContext.executeExport(jasperPrint,openType,fileName,fileType);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 预览并浏览器自动弹出打印
|
|
||||||
*/
|
|
||||||
@RequestMapping(path = "/previewPrint/{reportName}", method = RequestMethod.GET)
|
|
||||||
public void previewPrint(@PathVariable("reportName") final String reportName,
|
|
||||||
@RequestParam(required = false) Map<String, Object> parameters,
|
|
||||||
HttpServletResponse response, HttpServletRequest request)
|
|
||||||
throws SQLException, JRException, IOException {
|
|
||||||
|
|
||||||
parameters = parameters == null ? new HashMap<>() : parameters; //获取文件流
|
|
||||||
ClassPathResource resource = new ClassPathResource(UserConstants.REPORT_JASPER_PATH + reportName + ".jasper");
|
|
||||||
InputStream jasperStream = resource.getInputStream();
|
|
||||||
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
|
|
||||||
parameters.put("VendorName","测试厂商");
|
|
||||||
//byte[] pdfStream = JasperRunManager.runReportToPdf(jasperReport, parameters, dataSource.getConnection());
|
|
||||||
byte[] pdfStream = JasperRunManager.runReportToPdf(jasperReport, parameters, new JREmptyDataSource());
|
|
||||||
PdfReader reader = new PdfReader(pdfStream);
|
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream(pdfStream.length);
|
|
||||||
try {
|
|
||||||
// 给pdf加上脚本实现自动打印
|
|
||||||
StringBuffer script = new StringBuffer();
|
|
||||||
script.append("this.print({bUI:false,bSilent:true,bShrinkToFit:false});");
|
|
||||||
PdfStamper stamp = new PdfStamper(reader, bos);
|
|
||||||
stamp.setViewerPreferences(PdfWriter.HideMenubar
|
|
||||||
| PdfWriter.HideToolbar | PdfWriter.HideWindowUI);
|
|
||||||
stamp.addJavaScript(script.toString());
|
|
||||||
stamp.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
}
|
|
||||||
// 输出pdf
|
|
||||||
byte[] bytes = bos.toByteArray();
|
|
||||||
if (bytes != null && bytes.length > 0) {
|
|
||||||
response.setContentType("application/pdf");
|
|
||||||
response.setContentLength(bytes.length);
|
|
||||||
response.setHeader("Expires", "0");
|
|
||||||
response.setHeader("Cache-Control",
|
|
||||||
"must-revalidate, post-check=0, pre-check=0");
|
|
||||||
response.setHeader("Pragma", "public");
|
|
||||||
ServletOutputStream ouputStream = response.getOutputStream();
|
|
||||||
try {
|
|
||||||
ouputStream.write(bytes, 0, bytes.length);
|
|
||||||
ouputStream.flush();
|
|
||||||
} finally {
|
|
||||||
if (ouputStream != null) {
|
|
||||||
try {
|
|
||||||
ouputStream.close();
|
|
||||||
} catch (IOException ex) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 连接打印机打印
|
|
||||||
*/
|
|
||||||
@RequestMapping(path = "/print/{sourceName}", method = RequestMethod.GET)
|
|
||||||
public void print( @PathVariable("sourceName") final String sourceName,
|
|
||||||
@RequestParam(required = false) Map<String, Object> query){
|
|
||||||
JasperPrint jasperPrint = jasperContext.read(query, sourceName);
|
|
||||||
//设置打印方向 LANDSCAPE横向 PORTRAIT竖向
|
|
||||||
jasperPrint.setOrientation(OrientationEnum.PORTRAIT);
|
|
||||||
//withPrintDialog表示在打印的时候是否显示打印机设置对话框
|
|
||||||
try {
|
|
||||||
JasperPrintManager.printReport(jasperPrint, false);// 改true报错
|
|
||||||
} catch (JRException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置指定打印机打印
|
|
||||||
*/
|
|
||||||
@RequestMapping(path = "/printByPrinterName/{sourceName}", method = RequestMethod.GET)
|
|
||||||
public void printByPrinterName( @PathVariable("sourceName") final String sourceName,
|
|
||||||
@RequestParam(required = false) Map<String, Object> query) throws JRException {
|
|
||||||
JasperPrint jasperPrint = jasperContext.read(query, sourceName);
|
|
||||||
//设置打印方向 LANDSCAPE横向 PORTRAIT竖向
|
|
||||||
jasperPrint.setOrientation(OrientationEnum.PORTRAIT);
|
|
||||||
//withPrintDialog表示在打印的时候是否显示打印机设置对话框
|
|
||||||
JasperPrintManager.printReport(jasperPrint, false);// 改true报错
|
|
||||||
PrintService[] pss = PrinterJob.lookupPrintServices();
|
|
||||||
PrintService ps = null;
|
|
||||||
StringBuffer sb = new StringBuffer();
|
|
||||||
for (int i = 0; i < pss.length; i++) {
|
|
||||||
String sps = pss[i].toString();
|
|
||||||
sb.append(sps + "\n");
|
|
||||||
//如果打印机名称相同
|
|
||||||
if (sps.equalsIgnoreCase("Win32 Printer : Adobe PDF")) {// 可变参数,可让前端传参
|
|
||||||
ps = pss[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
JRAbstractExporter je = new JRPrintServiceExporter();
|
|
||||||
je.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
|
|
||||||
//设置指定打印机
|
|
||||||
je.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, ps);
|
|
||||||
je.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, false);
|
|
||||||
je.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, false);
|
|
||||||
je.exportReport();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,92 +0,0 @@
|
|||||||
package com.ktg.mes.report.utils;
|
|
||||||
|
|
||||||
import net.sf.jasperreports.engine.JRException;
|
|
||||||
import net.sf.jasperreports.engine.JasperFillManager;
|
|
||||||
import net.sf.jasperreports.engine.JasperPrint;
|
|
||||||
import net.sf.jasperreports.engine.JasperReport;
|
|
||||||
import net.sf.jasperreports.engine.util.JRLoader;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author: create by libin
|
|
||||||
* @version: v1.0
|
|
||||||
* @description: pl.piomin.jasperreport.handler
|
|
||||||
* @date:2020/4/15
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public final class JasperContext {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private DataSource dataSource;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private JasperReportExport exportExcel, exportHtml, exportImage, exportPdf, exportWord;
|
|
||||||
|
|
||||||
private JasperReportExport jasperReportExport;
|
|
||||||
|
|
||||||
private JasperContext() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean setFormatSuffix(FormatSuffixEnum suffix) {
|
|
||||||
boolean result = true;
|
|
||||||
switch (suffix) {
|
|
||||||
case doc:
|
|
||||||
jasperReportExport = exportWord;
|
|
||||||
break;
|
|
||||||
case xls:
|
|
||||||
jasperReportExport = exportExcel;
|
|
||||||
break;
|
|
||||||
case pdf:
|
|
||||||
jasperReportExport = exportPdf;
|
|
||||||
break;
|
|
||||||
case html:
|
|
||||||
jasperReportExport = exportHtml;
|
|
||||||
break;
|
|
||||||
case png:
|
|
||||||
jasperReportExport = exportImage;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
result = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取资源
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public JasperPrint read(Map<String, Object> query, String sourceName) {
|
|
||||||
query = query == null ? new HashMap<>() : query; //获取文件流
|
|
||||||
ClassPathResource resource = new ClassPathResource("data" + File.separator + sourceName + ".jasper");
|
|
||||||
try {
|
|
||||||
InputStream jasperStream = resource.getInputStream();
|
|
||||||
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
|
|
||||||
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, query, dataSource.getConnection());
|
|
||||||
return jasperPrint;
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (JRException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void executeExport(JasperPrint jasperPrint, String openType, String fileName,String fileType) {
|
|
||||||
jasperReportExport.execute(jasperPrint, openType, fileName,fileType);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
package com.ktg.mes.report.utils;
|
|
||||||
|
|
||||||
import net.sf.jasperreports.engine.JRException;
|
|
||||||
import net.sf.jasperreports.engine.JasperPrint;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author: create by libin
|
|
||||||
* @version: v1.0
|
|
||||||
* @description: pl.piomin.jasperreport.handler
|
|
||||||
* @date:2020/4/15
|
|
||||||
*/
|
|
||||||
public abstract class JasperReportExport {
|
|
||||||
@Autowired
|
|
||||||
protected HttpServletResponse response;
|
|
||||||
@Autowired
|
|
||||||
protected HttpServletRequest request;
|
|
||||||
|
|
||||||
public final void execute(JasperPrint jasperPrint, String openType, String fileName,String fileType){
|
|
||||||
OpenTypeEnum openTypeEnum = OpenTypeEnum.getValue(openType);
|
|
||||||
if (openTypeEnum==null) {
|
|
||||||
openTypeEnum=OpenTypeEnum.inline;
|
|
||||||
}
|
|
||||||
initOpenType(openTypeEnum,fileName,fileType);
|
|
||||||
try {
|
|
||||||
executEexport(jasperPrint);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (JRException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public abstract void initOpenType(OpenTypeEnum openType, String fileName,String fileType);
|
|
||||||
/**
|
|
||||||
* 导出
|
|
||||||
*/
|
|
||||||
public abstract void executEexport(JasperPrint jasperPrint) throws IOException, JRException;
|
|
||||||
|
|
||||||
}
|
|
@ -1,57 +0,0 @@
|
|||||||
package com.ktg.mes.report.utils;
|
|
||||||
|
|
||||||
import com.ktg.common.constant.UserConstants;
|
|
||||||
import net.sf.jasperreports.engine.*;
|
|
||||||
import net.sf.jasperreports.engine.base.JRBaseReport;
|
|
||||||
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
|
|
||||||
import net.sf.jasperreports.j2ee.servlets.ImageServlet;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ReportHelper {
|
|
||||||
|
|
||||||
public static void initReport(JasperReport report,String type){
|
|
||||||
|
|
||||||
if(UserConstants.REPORT_EXCEL_TYPE.equals(type))try{
|
|
||||||
Field margin = JRBaseReport.class.getDeclaredField("leftMargin");
|
|
||||||
margin.setAccessible(true);
|
|
||||||
margin.setInt(report, 0);
|
|
||||||
margin = JRBaseReport.class.getDeclaredField("topMargin");
|
|
||||||
margin.setAccessible(true);
|
|
||||||
margin.setInt(report, 0);
|
|
||||||
margin = JRBaseReport.class.getDeclaredField("bottomMargin");
|
|
||||||
margin.setAccessible(true);
|
|
||||||
margin.setInt(report, 0);
|
|
||||||
Field pageHeight = JRBaseReport.class.getDeclaredField("pageHeight");
|
|
||||||
pageHeight.setAccessible(true);
|
|
||||||
pageHeight.setInt(report, 2147483647);
|
|
||||||
}catch (Exception e){
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void showHtml(String fileName, String reportFile, HttpServletRequest request, HttpServletResponse response, Map params, JRDataSource dataSource)throws JRException,IOException {
|
|
||||||
request.setCharacterEncoding("utf-8");
|
|
||||||
response.setCharacterEncoding("utf-8");
|
|
||||||
response.setContentType("text/html");
|
|
||||||
|
|
||||||
JRAbstractExporter exporter = null;//getJRExporter("HTML");
|
|
||||||
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile, params, dataSource);
|
|
||||||
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
|
|
||||||
|
|
||||||
PrintWriter out = response.getWriter();
|
|
||||||
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
|
|
||||||
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
|
|
||||||
//exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
package com.ktg.mes.report.utils.impl;
|
|
||||||
|
|
||||||
|
|
||||||
import com.ktg.mes.report.utils.JasperReportExport;
|
|
||||||
import com.ktg.mes.report.utils.OpenTypeEnum;
|
|
||||||
import net.sf.jasperreports.engine.JRException;
|
|
||||||
import net.sf.jasperreports.engine.JasperPrint;
|
|
||||||
import net.sf.jasperreports.engine.export.JRXlsExporter;
|
|
||||||
import net.sf.jasperreports.export.SimpleExporterInput;
|
|
||||||
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
|
|
||||||
import net.sf.jasperreports.export.SimpleXlsReportConfiguration;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author: create by libin
|
|
||||||
* @version: v1.0
|
|
||||||
* @description: com.yingyinqi.jasperreport.handler
|
|
||||||
* @date:2020/4/15
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class ExportExcel extends JasperReportExport {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void initOpenType(OpenTypeEnum openType, String fileName, String fileType) {
|
|
||||||
response.setContentType("application/vnd_ms-excel;charset=utf-8");
|
|
||||||
response.setHeader("Content-Disposition", "attachment;filename=template.xls");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void executEexport(JasperPrint jasperPrint) throws IOException, JRException {
|
|
||||||
JRXlsExporter exporter = new JRXlsExporter();
|
|
||||||
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
|
||||||
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
|
|
||||||
SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
|
|
||||||
configuration.setOnePagePerSheet(true);
|
|
||||||
configuration.setDetectCellType(true);
|
|
||||||
configuration.setCollapseRowSpan(false);
|
|
||||||
exporter.setConfiguration(configuration);
|
|
||||||
exporter.exportReport();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
package com.ktg.mes.report.utils.impl;
|
|
||||||
|
|
||||||
|
|
||||||
import com.ktg.mes.report.utils.JasperReportExport;
|
|
||||||
import com.ktg.mes.report.utils.OpenTypeEnum;
|
|
||||||
import net.sf.jasperreports.engine.JRException;
|
|
||||||
import net.sf.jasperreports.engine.JasperPrint;
|
|
||||||
import net.sf.jasperreports.engine.export.HtmlExporter;
|
|
||||||
import net.sf.jasperreports.export.SimpleExporterInput;
|
|
||||||
import net.sf.jasperreports.export.SimpleHtmlExporterConfiguration;
|
|
||||||
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author: create by libin
|
|
||||||
* @version: v1.0
|
|
||||||
* @description: com.yingyinqi.jasperreport.handler
|
|
||||||
* @date:2020/4/15
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class ExportHtml extends JasperReportExport {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void initOpenType(OpenTypeEnum openType, String fileName, String fileType) {
|
|
||||||
if (openType.equals(OpenTypeEnum.attachment)) {
|
|
||||||
response.setContentType("text/html; charset=utf-8");
|
|
||||||
response.setHeader("Content-disposition", "attachment; filename=file.html");
|
|
||||||
response.setHeader("Pragma", "No-cache");
|
|
||||||
response.setHeader("Cache-Control", "No-cache");
|
|
||||||
response.setDateHeader("Expires", 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void executEexport(JasperPrint jasperPrint) throws IOException, JRException {
|
|
||||||
HtmlExporter exporter = new HtmlExporter();
|
|
||||||
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
|
||||||
exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getOutputStream()));
|
|
||||||
exporter.setConfiguration(new SimpleHtmlExporterConfiguration());
|
|
||||||
exporter.exportReport();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,54 +0,0 @@
|
|||||||
package com.ktg.mes.report.utils.impl;
|
|
||||||
|
|
||||||
|
|
||||||
import com.ktg.mes.report.utils.JasperReportExport;
|
|
||||||
import com.ktg.mes.report.utils.OpenTypeEnum;
|
|
||||||
import com.ktg.mes.report.utils.ThreadLocalUtil;
|
|
||||||
import net.sf.jasperreports.engine.JRException;
|
|
||||||
import net.sf.jasperreports.engine.JasperPrint;
|
|
||||||
import net.sf.jasperreports.engine.export.JRGraphics2DExporter;
|
|
||||||
import net.sf.jasperreports.export.SimpleExporterInput;
|
|
||||||
import net.sf.jasperreports.export.SimpleGraphics2DExporterOutput;
|
|
||||||
import net.sf.jasperreports.export.SimpleGraphics2DReportConfiguration;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author: create by libin
|
|
||||||
* @version: v1.0
|
|
||||||
* @description: com.yingyinqi.jasperreport.handler
|
|
||||||
* @date:2020/4/15
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class ExportImage extends JasperReportExport {
|
|
||||||
private static String FILE_TYPE="fileType";
|
|
||||||
@Override
|
|
||||||
public void initOpenType(OpenTypeEnum openType, String fileName, String fileType) {
|
|
||||||
response.setContentType("image/png");
|
|
||||||
if (openType.equals(OpenTypeEnum.attachment)) {
|
|
||||||
// response.setHeader("Content-disposition", "attachment; filename="+fileName+"."+fileType);
|
|
||||||
response.setHeader("Content-disposition", "attachment;");
|
|
||||||
}
|
|
||||||
ThreadLocalUtil.set(FILE_TYPE,fileType);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void executEexport(JasperPrint jasperPrint) throws IOException, JRException {
|
|
||||||
JRGraphics2DExporter exporter = new JRGraphics2DExporter();
|
|
||||||
//创建一个影像对象,设置宽高,可自定义
|
|
||||||
BufferedImage bufferedImage = new BufferedImage(jasperPrint.getPageWidth(), jasperPrint.getPageHeight(), BufferedImage.TYPE_INT_RGB);
|
|
||||||
Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
|
|
||||||
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
|
||||||
SimpleGraphics2DExporterOutput graphics2DOutput = new SimpleGraphics2DExporterOutput();
|
|
||||||
graphics2DOutput.setGraphics2D(graphics2D);
|
|
||||||
exporter.setExporterOutput(graphics2DOutput);
|
|
||||||
exporter.setConfiguration(new SimpleGraphics2DReportConfiguration());
|
|
||||||
exporter.exportReport();
|
|
||||||
graphics2D.dispose();// 释放资源信息
|
|
||||||
ImageIO.write(bufferedImage, ThreadLocalUtil.get(FILE_TYPE), response.getOutputStream()); // 生成图片
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
package com.ktg.mes.report.utils.impl;
|
|
||||||
|
|
||||||
|
|
||||||
import com.ktg.mes.report.utils.JasperReportExport;
|
|
||||||
import com.ktg.mes.report.utils.OpenTypeEnum;
|
|
||||||
import net.sf.jasperreports.engine.JRException;
|
|
||||||
import net.sf.jasperreports.engine.JasperExportManager;
|
|
||||||
import net.sf.jasperreports.engine.JasperPrint;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author: create by libin
|
|
||||||
* @version: v1.0
|
|
||||||
* @description: com.yingyinqi.jasperreport.handler
|
|
||||||
* @date:2020/4/15
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class ExportPdf extends JasperReportExport {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void initOpenType(OpenTypeEnum openType, String fileName, String fileType) {
|
|
||||||
response.setContentType("application/pdf");
|
|
||||||
if (openType.equals(OpenTypeEnum.attachment)) {
|
|
||||||
response.setHeader("Content-disposition", "attachment; filename=file.pdf");
|
|
||||||
response.setHeader("Pragma", "No-cache");
|
|
||||||
response.setHeader("Cache-Control", "No-cache");
|
|
||||||
response.setDateHeader("Expires", 0);
|
|
||||||
}else{
|
|
||||||
response.setHeader("Content-Disposition", "inline");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void executEexport(JasperPrint jasperPrint) throws IOException, JRException {
|
|
||||||
final OutputStream outputStream = response.getOutputStream();
|
|
||||||
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
package com.ktg.mes.report.utils.impl;
|
|
||||||
|
|
||||||
import com.ktg.mes.report.utils.JasperReportExport;
|
|
||||||
import com.ktg.mes.report.utils.OpenTypeEnum;
|
|
||||||
import net.sf.jasperreports.engine.DefaultJasperReportsContext;
|
|
||||||
import net.sf.jasperreports.engine.JRException;
|
|
||||||
import net.sf.jasperreports.engine.JasperPrint;
|
|
||||||
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
|
|
||||||
import net.sf.jasperreports.export.SimpleDocxExporterConfiguration;
|
|
||||||
import net.sf.jasperreports.export.SimpleExporterInput;
|
|
||||||
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author: create by libin
|
|
||||||
* @version: v1.0
|
|
||||||
* @description: com.yingyinqi.jasperreport.handler
|
|
||||||
* @date:2020/4/15
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class ExportWord extends JasperReportExport {
|
|
||||||
@Override
|
|
||||||
public void initOpenType(OpenTypeEnum openType, String fileName, String fileType) {
|
|
||||||
response.setContentType("application/msword");
|
|
||||||
response.setHeader("Content-Disposition", "attachment; filename=file.doc");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void executEexport(JasperPrint jasperPrint) throws IOException, JRException {
|
|
||||||
JRDocxExporter exporter = new JRDocxExporter(DefaultJasperReportsContext.getInstance());
|
|
||||||
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
|
|
||||||
exporter.setConfiguration(new SimpleDocxExporterConfiguration());
|
|
||||||
OutputStream outputStream = response.getOutputStream();
|
|
||||||
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
|
|
||||||
exporter.exportReport();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user