首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JasperReport导出到pdf不工作

JasperReport导出到pdf不工作
EN

Stack Overflow用户
提问于 2014-12-01 17:32:14
回答 2查看 3K关注 0票数 0

我使用以下控制器将报告导出为pdf, xls, csv。它的代码如下所示

代码语言:javascript
复制
@Controller
@RequestMapping(value = ServicePath.COMMON_PREFIX + "/test-report")
public class ReportController 
{

@Autowired
private UserService userService;

public static JasperDesign jasperDesign;
public static JasperPrint jasperPrint;
public static JasperReport jasperReport;
public static String reportTemplateUrl = "/lms-internet/src/main/webapp/report.jrxml";

@RequestMapping(method = RequestMethod.GET , value = "/pdf")
public void generatePdf()
{
    try{
    List<User> usersList = userService.findAll();

    JRDataSource JRdataSource = new JRBeanCollectionDataSource(usersList);

    InputStream resourceAsStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream(reportTemplateUrl);
    //get report file and then load into jasperDesign
    jasperDesign = JRXmlLoader.load(resourceAsStream);
    //compile the jasperDesign
    jasperReport = JasperCompileManager.compileReport(jasperDesign);

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    OutputStream outputfile = new FileOutputStream(new File("c:/temp/person.xls"));

    HashMap params = new HashMap();

    //fill the ready report with data and parameter
    jasperPrint = JasperFillManager.fillReport(jasperReport, params, JRdataSource);

    //coding for Excel
    JRXlsExporter exporterXls = new JRXlsExporter();
    exporterXls.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
    exporterXls.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, output);
    exporterXls.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
    exporterXls.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
    exporterXls.exportReport();
    outputfile.write(output.toByteArray());
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

当我运行它时,我得到了以下异常:

代码语言:javascript
复制
net.sf.jasperreports.engine.JRException: java.net.MalformedURLException

有没有人能告诉我我做错了什么,以及如何改正。并告诉我在哪里添加jrxml文件。

EN

回答 2

Stack Overflow用户

发布于 2014-12-01 18:08:36

它看起来像一个springboot项目;尝试将report.jrxml移到src/main/resources中,然后:

代码语言:javascript
复制
InputStream resourceAsStream = this.getClass().getResourceAsStream(reportTemplateUrl);
票数 0
EN

Stack Overflow用户

发布于 2014-12-10 22:26:53

我就是这么做的。希望这能有所帮助

代码语言:javascript
复制
@RequestMapping(value = "/ProduceMyReportPDF", method = RequestMethod.GET, params = { "P1" })
public FileSystemResource AnalisiIncassiGS(
        @RequestParam(value = "P1") String p1,
        HttpServletResponse response) {

    try {
        // START of block "using jasper library": try this if you want to produce a PDF using jasper library
        String reportname = "c:\\TEMPLATE\\myreport.jasper";
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new File(reportname));
        MyData d = new MyData(); // used to store parameters and datasource
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, d.getParametersHashMap(), d.getDataSource);
        JasperExportManager.exportReportToPdfFile(jasperPrint, "c:\\outputpath\\p4.pdf");
        // END of block "using jasper library"

        // START of block "using jasper server": ALTERNATIVELY try this if you want to produce a PDF using jasper server installed somewhere
        String sJASPERSERVER_URL = "http://192.168.1.1:8080/jasperserver";
        String sPathToYourReport = "Path/to/your/report/myreport";
        String j_username = "jasperadmin";
        String j_password = "yourpassword";
        URL website = new URL( sJASPERSERVER_URL+sPathToYourReport + ".pdf"+   "?PARAM1="+ p1 +"&j_username="+j_username+"&j_password="+j_password   );

        ReadableByteChannel rbc = Channels.newChannel(website.openStream());
        FileOutputStream fos = new FileOutputStream("c:\\outputpath\\p4.pdf");
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        // END of block "using jasper server"

        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=p4.pdf");

        return new FileSystemResource( "c:\\outputpath\\p4.pdf" );

    } catch (Exception e) {
        e.printStackTrace();

        return null;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27225337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档