我在jasper-view.xml中声明了一个bean,如下所示
df.withColumn("college_name", explode(split(df("college_name"), ",")))
控制器中的方法如下
<bean id="resultsPdf"
class="org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView"
p:url="classpath:reports/resultsPdfFormat.jrxml"
p:reportDataKey="datasource">
<property name="headers">
<props>
<prop key="Content-Disposition">
attachment; filename=results.pdf
</prop>
</props>
</property>
</bean>
目前工作正常。下载文件的名称是
results.pdf
现在我想将year参数追加到文件名中,以便它可以读作
results_YEAR.pdf
有什么想法吗?提前谢谢。
答案 0 :(得分:1)
我找到了解决这个问题的方法。我们可以通过更改 generateResultsPdf 方法来实现任务,而不是声明bean
@RequestMapping(value = "/results-pdf", method = RequestMethod.GET)
@ResponseBody
public void generateResultsPdf(@RequestParam("year") String year, HttpServletResponse response) {
List<Student> studentList = studentService.getResultsByYear(year);
JRDataSource jrDataSource = new JRBeanCollectionDataSource(studentList);
Map<String, Object> parameterMap = new HashMap<>();
parameterMap.put("title", "Results table"));
parameterMap.put("datasource", jrDataSource);
try {
JasperReport jasperReport = JasperCompileManager.compileReport(RESULTS_PDF_REPORT_PATH);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameterMap, jrDataSource);
String filename = "new-filename.pdf";
response.setContentType("application/pdf");
response.addHeader("Content-disposition", "attachment; filename=" +filename);
OutputStream outputStream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
} catch (JRException | IOException e) {
logger.error("Error in generating pdf : {}", e);
}
}