Blame view

src/main/java/com/huaheng/pc/report/jasper/JasperController.java 3.38 KB
1
package com.huaheng.pc.report.jasper;
2
pengcheng authored
3
4
5
import com.huaheng.common.jasper.DocType;
import com.huaheng.common.jasper.DocTypeUtil;
import com.huaheng.common.jasper.JasperreportUtils;
6
7
8
9
10
11
12
13
14
import net.sf.jasperreports.engine.*;
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.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
pengcheng authored
15
16
17
18
19
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
20
import java.sql.SQLException;
pengcheng authored
21
import java.util.ArrayList;
22
import java.util.HashMap;
pengcheng authored
23
import java.util.List;
24
import java.util.Map;
pengcheng authored
25
26
27
@RestController
@RequestMapping("/api/jasper")
pengcheng authored
28
public class JasperController {
29
30
31
32
33

    @Resource
    private DataSource dataSource;
pengcheng authored
34
35
36
    /**
pengcheng authored
37
     * 转换为pdf在浏览器上展示
38
39
40
41
42
43
44
45
46
     *
     * @param reportName
     * @param parameters
     * @param response
     * @throws SQLException
     * @throws ClassNotFoundException
     * @throws JRException
     * @throws IOException
     */
47
    @GetMapping("/{reportName}")
pengcheng authored
48
//    @GetMapping("/{reportName}")
49
50
51
52
53
    public void getReportByParam(
            @PathVariable("reportName") final String reportName,
            @RequestParam(required = false) Map<String, Object> parameters,
            HttpServletResponse response) throws Exception {
pengcheng authored
54
55
56
57
58
59
        parameters = parameters == null ? new HashMap<>() : parameters;
        //获取文件流
        ClassPathResource resource = new ClassPathResource("jaspers" + File.separator + reportName + ".jasper");
        InputStream jasperStream = resource.getInputStream();

        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
pengcheng authored
60
61
        Connection connection =dataSource.getConnection();
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
pengcheng authored
62
63
64
65
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline;");
        final OutputStream outputStream = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
pengcheng authored
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    }




    /**
     * 转换为多种格式导出
     *
     * @param reportName
     * @param parameters
     * @param response
     * @throws SQLException
     * @throws ClassNotFoundException
     * @throws JRException
     * @throws IOException
     */
82
    @PostMapping("/{reportName}")
pengcheng authored
83
84
85
86
87
88
89
90
    public void getReportByExcel(
            @PathVariable("reportName") final String reportName,
            @RequestParam(required = false) Map<String, Object> parameters,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        parameters = parameters == null ? new HashMap<>() : parameters;
        String jasperPath ="C:/Users/Administrator/JaspersoftWorkspace/MyReports/" + reportName + ".jasper";
pengcheng authored
91
        String docType="pdf";
pengcheng authored
92
93
94
95
96
97
        DocType type = DocTypeUtil.getEnumDocType(docType);
        String fileName =reportName;
        List list =new ArrayList();
        Connection connection =dataSource.getConnection();
        JasperreportUtils jasperreportUtils =new JasperreportUtils(request,response,request.getSession());
        jasperreportUtils.createExportDocument(type,jasperPath,parameters,reportName,connection);
pengcheng authored
98
99
    }
pengcheng authored
100
101
102
}