JasperController.java 3.38 KB
package com.huaheng.pc.report.jasper;

import com.huaheng.common.jasper.DocType;
import com.huaheng.common.jasper.DocTypeUtil;
import com.huaheng.common.jasper.JasperreportUtils;
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;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/jasper")
public class JasperController {

    @Resource
    private DataSource dataSource;




    /**
     * 转换为pdf在浏览器上展示
     *
     * @param reportName
     * @param parameters
     * @param response
     * @throws SQLException
     * @throws ClassNotFoundException
     * @throws JRException
     * @throws IOException
     */
    @GetMapping("/{reportName}")
//    @GetMapping("/{reportName}")
    public void getReportByParam(
            @PathVariable("reportName") final String reportName,
            @RequestParam(required = false) Map<String, Object> parameters,
            HttpServletResponse response) throws Exception {

        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);
        Connection connection =dataSource.getConnection();
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline;");
        final OutputStream outputStream = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
    }




    /**
     * 转换为多种格式导出
     *
     * @param reportName
     * @param parameters
     * @param response
     * @throws SQLException
     * @throws ClassNotFoundException
     * @throws JRException
     * @throws IOException
     */
    @PostMapping("/{reportName}")
    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";
        String docType="pdf";
        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);

    }


}