我有以下控制器将报告导出为pdf, xls, csv
。它的代码如下所示
@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();
}
}
当我运行它时,我得到以下异常:
net.sf.jasperreports.engine.JRException: java.net.MalformedURLException
有人可以告诉我我做错了什么以及如何解决它。并告诉我在哪里添加jrxml文件。
答案 0 :(得分:0)
它看起来像一个springboot项目;尝试将report.jrxml移动到src / main / resources和:
InputStream resourceAsStream = this.getClass().getResourceAsStream(reportTemplateUrl);
答案 1 :(得分:0)
这就是我这样做的方式。希望这有帮助
@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;
}
}