观察:我是巴西人,为了使内容更全面,我只用英文写了这个话题。
我正在为我工作的公司应用程序做一份文档报告,我需要让Jasper解释我将通过Java发送给他的列表。
经过研究,我意识到有两种方法可以做到这一点。第一个,我作为参数发送“数据集(我的列表)”,并在Jasper中解释它。我这样做了,但是我无法遍历列表,也无法从列表对象内部获取信息。另一种方法是提交数据源,然后在Jasper中“玩”它。但是这样,我不知道该怎么办。我已经看到了一些示例,这些示例是工作人员还使用子报表来修改列表。我想知道我可以在Java和Jasper中做什么以及一些示例代码。
@Service
public class ReportService {
@Autowired
private ResourceLoader resourceLoader;
@Autowired
private DataSource dataSource;
private final Logger logger = LoggerFactory.getLogger(ReportService.class);
public ReportStreaming generate(ReportTemplateEnum template, FileTypeEnum type, Map<String, Object> parameters, JRBeanCollectionDataSource dataSourceJasper) throws Exception {
ReportStreaming reportStreaming = new ReportStreaming();
logger.info("BEGIN - REPORT GENERATION");
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
JasperPrint jasperPrint;
//Se não for informado um dataSource, é aplicado a conexão MySQL que está rodando no Spring
if (Objects.isNull(dataSourceJasper)) {
Connection conn = DataSourceUtils.getConnection(dataSource);
jasperPrint = JasperFillManager.fillReport(new ClassPathResource("reports/templates/".concat(template.getTemplateName())).getInputStream(), parameters, conn);
} else {
jasperPrint = JasperFillManager.fillReport(new ClassPathResource("reports/templates/".concat(template.getTemplateName())).getInputStream(), parameters, dataSourceJasper);
}
if (jasperPrint != null) {
switch (type) {
case XLS:
JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setExporterInput(new SimpleExporterInput(jasperPrint));
exporterXLS.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
SimpleXlsReportConfiguration xlsConfig = new SimpleXlsReportConfiguration();
xlsConfig.setOnePagePerSheet(false);
xlsConfig.setDetectCellType(true);
xlsConfig.setWhitePageBackground(false);
exporterXLS.setConfiguration(xlsConfig);
exporterXLS.exportReport();
break;
case XLSX:
JRXlsxExporter exporterXLSX = new JRXlsxExporter();
exporterXLSX.setExporterInput(new SimpleExporterInput(jasperPrint));
exporterXLSX.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
SimpleXlsxReportConfiguration xlsxConfig = new SimpleXlsxReportConfiguration();
xlsxConfig.setOnePagePerSheet(false);
xlsxConfig.setDetectCellType(true);
xlsxConfig.setWhitePageBackground(false);
exporterXLSX.setConfiguration(xlsxConfig);
exporterXLSX.exportReport();
break;
case PDF:
JasperExportManager.exportReportToPdfStream(jasperPrint, out);
break;
default:
logger.info("Unknown report format");
}
reportStreaming.setIn(new ByteArrayInputStream(out.toByteArray()));
reportStreaming.setOut(out);
}
} catch (JRException | IOException e) {
logger.error("", e);
throw e;
} finally {
logger.info("END - REPORT GENERATION");
}
return reportStreaming;
}
}
public enum ReportTemplateEnum {
EMPRESA_CONTRATO("empresa-contrato.jasper", "Documentos por Empresa"),
EMPREGADO_EMPRESA("empregado-empresa.jasper", "Documentos por Empregado");
private String templateName;
private String fileNameDownload;
ReportTemplateEnum(String templateName, String fileNameDownload) {
this.templateName = templateName;
this.fileNameDownload = getName(fileNameDownload);
}
private String getName(String fileNameDownload) {
DateFormat dateFormat = new SimpleDateFormat(" dd-MM-yyyy hhmmss");
String strDate = dateFormat.format(new Date());
return fileNameDownload.concat(strDate);
}
public String getTemplateName() {
return templateName;
}
public String getFileNameDownload(String typeName) {
return fileNameDownload.concat(".").concat(typeName);
}
}
@Getter
public enum FileTypeEnum {
PDF("pdf", "application/pdf"), XLS("xls", "application/vnd.ms-excel"), XLSX("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
private String name;
private String contentType;
FileTypeEnum(String name, String contentType) {
this.name = name;
this.contentType = contentType;
}
}
public class ReportStreaming {
private ByteArrayOutputStream out;
private ByteArrayInputStream in;
public ByteArrayOutputStream getOut() {
return out;
}
public void setOut(ByteArrayOutputStream out) {
this.out = out;
}
public ByteArrayInputStream getIn() {
return in;
}
public void setIn(ByteArrayInputStream in) {
this.in = in;
}
}
我有一些用于组装报表的数据集的对象类,如果需要的话,我可以与您共享。