springboot导入jasper
时间: 2023-07-09 12:47:07 浏览: 139
要在Spring Boot中使用Jasper报表工具,可以按照以下步骤进行操作:
1. 在pom.xml文件中加入以下依赖:
```
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.17.0</version>
</dependency>
```
2. 在application.properties文件中配置Jasper报表文件的存放路径:
```
jasper.report.path=/report
```
3. 创建一个JasperReportService,用于加载Jasper报表文件:
```
@Service
public class JasperReportService {
@Value("${jasper.report.path}")
private String reportPath;
public JasperPrint getJasperPrint(String reportName, Map<String, Object> params) throws JRException {
String filePath = reportPath + "/" + reportName + ".jrxml";
InputStream inputStream = this.getClass().getResourceAsStream(filePath);
JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());
return jasperPrint;
}
}
```
4. 在Controller中调用JasperReportService生成JasperPrint对象,并将其转换成PDF或其他格式:
```
@RestController
@RequestMapping("/report")
public class ReportController {
@Autowired
private JasperReportService jasperReportService;
@GetMapping(value = "/{reportName}", produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<byte[]> getReport(@PathVariable String reportName) throws Exception {
Map<String, Object> params = new HashMap<>();
JasperPrint jasperPrint = jasperReportService.getJasperPrint(reportName, params);
byte[] pdfBytes = JasperExportManager.exportReportToPdf(jasperPrint);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_PDF);
headers.setContentDisposition(ContentDisposition.builder("inline").filename(reportName + ".pdf").build());
headers.setContentLength(pdfBytes.length);
return new ResponseEntity<>(pdfBytes, headers, HttpStatus.OK);
}
}
```
这样就可以在Spring Boot中使用Jasper报表工具了。
阅读全文