|
|
@@ -63,43 +63,8 @@ public class PrintServiceImpl implements PrintService {
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
@Override
|
|
|
- public byte[] export(String userName, String profile, String reportName, String whereCondition,
|
|
|
- String otherParameters, ExportType exportType)
|
|
|
- throws JRException, IOException, DocumentException, SQLException {
|
|
|
- Map<String, Object> result = print(userName, profile, reportName, whereCondition, otherParameters, exportType,
|
|
|
- null, true);
|
|
|
- if (!CollectionUtils.isEmpty(result)) {
|
|
|
- return (byte[]) result.get("data");
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Map<String, Object> preview(String userName, String profile, String reportName, String whereCondition,
|
|
|
- String otherParameters, Integer pageIndex)
|
|
|
- throws JRException, IOException, DocumentException, SQLException {
|
|
|
- return print(userName, profile, reportName, whereCondition, otherParameters, null, pageIndex, true);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 输出报表的数据
|
|
|
- *
|
|
|
- * @param userName
|
|
|
- * @param profile
|
|
|
- * @param reportName
|
|
|
- * @param whereCondition
|
|
|
- * @param otherParameters
|
|
|
- * @param exportType
|
|
|
- * @param pageIndex
|
|
|
- * @param useFileVirtualizer 是否使用文件虚拟化技术
|
|
|
- * @return 页码pageSize:int,数据data:byte[]
|
|
|
- * @throws JRException
|
|
|
- * @throws IOException
|
|
|
- * @throws DocumentException
|
|
|
- * @throws SQLException
|
|
|
- */
|
|
|
- private Map<String, Object> print(String userName, String profile, String reportName, String whereCondition,
|
|
|
- String otherParameters, ExportType exportType, Integer pageIndex, boolean useFileVirtualizer)
|
|
|
+ public int export(String userName, String profile, String reportName, String whereCondition,
|
|
|
+ String otherParameters, ExportType exportType, File exportFile, Integer pageIndex, boolean useFileVirtualizer)
|
|
|
throws JRException, IOException, DocumentException, SQLException {
|
|
|
// TODO 重新实现jasperserver接口
|
|
|
// try {
|
|
|
@@ -113,6 +78,10 @@ public class PrintServiceImpl implements PrintService {
|
|
|
throw new SQLException("获取数据源失败");
|
|
|
}
|
|
|
|
|
|
+ if(!exportFile.getParentFile().exists()){
|
|
|
+ exportFile.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
String masterOfJrxml = getMasterOfJrxml(userName, reportName);
|
|
|
String jrxmlFilePath = fileService.getJrxmlFilePath(masterOfJrxml, reportName);
|
|
|
|
|
|
@@ -143,68 +112,48 @@ public class PrintServiceImpl implements PrintService {
|
|
|
throw new IllegalStateException("编译报表模板失败");
|
|
|
}
|
|
|
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- byte[] data;
|
|
|
+ logger.info("export fillReport...");
|
|
|
// 从数据库获取数据填充报表
|
|
|
- JasperPrint jasperPrint = null;
|
|
|
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
- // exportType导出文件的格式不为空,表示是导出,并非预览
|
|
|
- if (exportType != null) {
|
|
|
- logger.info("export fillReport...");
|
|
|
- boolean customCellStyle = false;
|
|
|
- // 只导出数据
|
|
|
- if (exportType == ExportType.XLS_DATA || exportType == ExportType.XLSX_DATA) {
|
|
|
- // 需自定义单元格格式
|
|
|
- customCellStyle = true;
|
|
|
- JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFilePath);
|
|
|
- // 移除模板中多余元素
|
|
|
- removeUnusedElements(jasperDesign);
|
|
|
- JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
|
|
|
- jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
|
|
|
- } else if (exportType == ExportType.DOC) {
|
|
|
- // 导出word时需要对行距等进行调整
|
|
|
- InputStream inputStream = new ByteArrayInputStream(
|
|
|
- modifyLineSpacing(jrxmlFilePath).getBytes("UTF-8"));
|
|
|
- JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
|
|
|
- jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
|
|
|
- inputStream.close();
|
|
|
- } else {
|
|
|
- jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
|
|
|
- }
|
|
|
-
|
|
|
- if (exportType == ExportType.XLS || exportType == ExportType.XLS_DATA) {
|
|
|
- exportReportToXls(jasperPrint, outputStream, customCellStyle);
|
|
|
- } else if (exportType == ExportType.XLSX || exportType == ExportType.XLSX_DATA) {
|
|
|
- exportReportToXlsx(jasperPrint, outputStream, customCellStyle);
|
|
|
- } else if (exportType == ExportType.DOC) {
|
|
|
- exportReportToDoc(jasperPrint, outputStream);
|
|
|
- } else if (exportType == ExportType.TXT) {
|
|
|
- exportReportToText(jasperPrint, outputStream);
|
|
|
- } else {
|
|
|
- exportReportToPdf(jasperPrint, outputStream, pageIndex);
|
|
|
- }
|
|
|
- logger.info("export fillReport done...");
|
|
|
- data = outputStream.toByteArray();
|
|
|
- outputStream.close();
|
|
|
- }
|
|
|
- // 报表预览,则直接输出pdf,并且也许需要分页
|
|
|
- else {
|
|
|
- logger.info("preview fillReport...");
|
|
|
+ JasperPrint jasperPrint;
|
|
|
+ boolean customCellStyle = false;
|
|
|
+ // 只导出数据
|
|
|
+ if (exportType == ExportType.XLS_DATA || exportType == ExportType.XLSX_DATA) {
|
|
|
+ // 需自定义单元格格式
|
|
|
+ customCellStyle = true;
|
|
|
+ JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFilePath);
|
|
|
+ // 移除模板中多余元素
|
|
|
+ removeUnusedElements(jasperDesign);
|
|
|
+ JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
|
|
|
+ jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
|
|
|
+ } else if (exportType == ExportType.DOC) {
|
|
|
+ // 导出word时需要对行距等进行调整
|
|
|
+ InputStream inputStream = new ByteArrayInputStream(
|
|
|
+ modifyLineSpacing(jrxmlFilePath).getBytes("UTF-8"));
|
|
|
+ JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
|
|
|
+ jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
|
|
|
+ inputStream.close();
|
|
|
+ } else {
|
|
|
jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
|
|
|
- exportReportToPdf(jasperPrint, outputStream, pageIndex);
|
|
|
- logger.info("preview fillReport done...");
|
|
|
- data = outputStream.toByteArray();
|
|
|
- outputStream.close();
|
|
|
- result.put("pageSize", jasperPrint.getPages().size());
|
|
|
}
|
|
|
- // TODO 先写入文件,用时再读取,防止数据量太大,内存不足
|
|
|
- result.put("data", data);
|
|
|
- return result;
|
|
|
- } catch (JRRuntimeException e) {
|
|
|
+
|
|
|
+ if (exportType == ExportType.XLS || exportType == ExportType.XLS_DATA) {
|
|
|
+ exportReportToXls(jasperPrint, exportFile, customCellStyle, pageIndex);
|
|
|
+ } else if (exportType == ExportType.XLSX || exportType == ExportType.XLSX_DATA) {
|
|
|
+ exportReportToXlsx(jasperPrint, exportFile, customCellStyle, pageIndex);
|
|
|
+ } else if (exportType == ExportType.DOC) {
|
|
|
+ exportReportToDoc(jasperPrint, exportFile, pageIndex);
|
|
|
+ } else if (exportType == ExportType.TXT) {
|
|
|
+ exportReportToText(jasperPrint, exportFile, pageIndex);
|
|
|
+ } else {
|
|
|
+ exportReportToPdf(jasperPrint, exportFile, pageIndex);
|
|
|
+ }
|
|
|
+ logger.info("export fillReport done...");
|
|
|
+ return jasperPrint.getPages().size();
|
|
|
+ } catch (JRRuntimeException e) {
|
|
|
// 如果因为某个类未实现 Serializable 而无法反序列化,则不使用 REPORT_VIRTUALIZER
|
|
|
if (e.getCause() instanceof NotSerializableException) {
|
|
|
logger.error(e.getMessage() + " 取消 REPORT_VIRTUALIZER ,重新打印");
|
|
|
- return print(userName, profile, reportName, whereCondition, otherParameters, exportType, pageIndex, false);
|
|
|
+ return export(userName, profile, reportName, whereCondition, otherParameters, exportType, exportFile, pageIndex, false);
|
|
|
}
|
|
|
throw e;
|
|
|
} finally {
|
|
|
@@ -497,15 +446,17 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* 以xls的格式导出报表
|
|
|
*
|
|
|
* @param jasperPrint
|
|
|
- * @param outputStream
|
|
|
+ * @param outFile
|
|
|
+ * @param pageIndex
|
|
|
* @throws JRException
|
|
|
*/
|
|
|
- private void exportReportToXls(JasperPrint jasperPrint, OutputStream outputStream, boolean customCellStyle)
|
|
|
+ private void exportReportToXls(JasperPrint jasperPrint, File outFile, boolean customCellStyle, Integer pageIndex)
|
|
|
throws JRException {
|
|
|
JRXlsExporter exporter = new CustomJRXlsExporter(customCellStyle);
|
|
|
+ setPageConfiguration(exporter, jasperPrint, pageIndex);
|
|
|
ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
|
|
|
exporter.setExporterInput(exporterInput);
|
|
|
- OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
|
|
|
+ OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outFile);
|
|
|
exporter.setExporterOutput(exporterOutput);
|
|
|
exporter.exportReport();
|
|
|
}
|
|
|
@@ -514,15 +465,17 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* 以xlsx的格式导出报表
|
|
|
*
|
|
|
* @param jasperPrint
|
|
|
- * @param outputStream
|
|
|
+ * @param outFile
|
|
|
+ * @param pageIndex
|
|
|
* @throws JRException
|
|
|
*/
|
|
|
- private void exportReportToXlsx(JasperPrint jasperPrint, OutputStream outputStream, boolean customCellStyle)
|
|
|
+ private void exportReportToXlsx(JasperPrint jasperPrint, File outFile, boolean customCellStyle, Integer pageIndex)
|
|
|
throws JRException {
|
|
|
JRXlsxExporter exporter = new CustomJRXlsxExporter(customCellStyle);
|
|
|
+ setPageConfiguration(exporter, jasperPrint, pageIndex);
|
|
|
ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
|
|
|
exporter.setExporterInput(exporterInput);
|
|
|
- OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
|
|
|
+ OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outFile);
|
|
|
exporter.setExporterOutput(exporterOutput);
|
|
|
exporter.exportReport();
|
|
|
}
|
|
|
@@ -531,27 +484,30 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* 以doc的格式导出报表
|
|
|
*
|
|
|
* @param jasperPrint
|
|
|
- * @param outputStream
|
|
|
+ * @param outFile
|
|
|
+ * @param pageIndex
|
|
|
* @throws JRException
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- private void exportReportToDoc(JasperPrint jasperPrint, OutputStream outputStream) throws JRException, IOException {
|
|
|
- exportReportToRtf(jasperPrint, outputStream);
|
|
|
+ private void exportReportToDoc(JasperPrint jasperPrint, File outFile, Integer pageIndex) throws JRException, IOException {
|
|
|
+ exportReportToRtf(jasperPrint, outFile, pageIndex);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 以rtf的格式导出报表
|
|
|
*
|
|
|
* @param jasperPrint
|
|
|
- * @param outputStream
|
|
|
+ * @param outFile
|
|
|
+ * @param pageIndex
|
|
|
* @throws JRException
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- private void exportReportToRtf(JasperPrint jasperPrint, OutputStream outputStream) throws JRException, IOException {
|
|
|
+ private void exportReportToRtf(JasperPrint jasperPrint, File outFile, Integer pageIndex) throws JRException, IOException {
|
|
|
JRRtfExporter exporter = new JRRtfExporter();
|
|
|
+ setPageConfiguration(exporter, jasperPrint, pageIndex);
|
|
|
ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
|
|
|
exporter.setExporterInput(exporterInput);
|
|
|
- WriterExporterOutput exporterOutput = new SimpleWriterExporterOutput(outputStream);
|
|
|
+ WriterExporterOutput exporterOutput = new SimpleWriterExporterOutput(outFile);
|
|
|
exporter.setExporterOutput(exporterOutput);
|
|
|
exporter.exportReport();
|
|
|
}
|
|
|
@@ -560,27 +516,17 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* 以pdf的格式导出报表
|
|
|
*
|
|
|
* @param jasperPrint
|
|
|
- * @param outputStream
|
|
|
+ * @param outFile
|
|
|
* @param pageIndex
|
|
|
* @throws JRException
|
|
|
*/
|
|
|
- private void exportReportToPdf(JasperPrint jasperPrint, OutputStream outputStream, Integer pageIndex)
|
|
|
+ private void exportReportToPdf(JasperPrint jasperPrint, File outFile, Integer pageIndex)
|
|
|
throws JRException {
|
|
|
JRPdfExporter exporter = new JRPdfExporter();
|
|
|
- if (pageIndex != null) {
|
|
|
- // 前端显示时页码从1开始,生成报表时页码从0开始
|
|
|
- pageIndex -= 1;
|
|
|
- // 页码并非有效数值,重置为第一页
|
|
|
- if (pageIndex < 0 || pageIndex >= jasperPrint.getPages().size()) {
|
|
|
- pageIndex = 0;
|
|
|
- }
|
|
|
- SimplePdfReportConfiguration configuration = new SimplePdfReportConfiguration();
|
|
|
- configuration.setPageIndex(pageIndex);
|
|
|
- exporter.setConfiguration(configuration);
|
|
|
- }
|
|
|
+ setPageConfiguration(exporter, jasperPrint, pageIndex);
|
|
|
ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
|
|
|
exporter.setExporterInput(exporterInput);
|
|
|
- OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
|
|
|
+ OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outFile);
|
|
|
exporter.setExporterOutput(exporterOutput);
|
|
|
exporter.exportReport();
|
|
|
}
|
|
|
@@ -589,18 +535,41 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* 以text的格式导出报表
|
|
|
*
|
|
|
* @param jasperPrint
|
|
|
- * @param outputStream
|
|
|
+ * @param outFile
|
|
|
+ * @param pageIndex
|
|
|
* @throws JRException
|
|
|
*/
|
|
|
- private void exportReportToText(JasperPrint jasperPrint, OutputStream outputStream) throws JRException {
|
|
|
+ private void exportReportToText(JasperPrint jasperPrint, File outFile, Integer pageIndex) throws JRException {
|
|
|
JRTextExporter exporter = new JRTextExporter();
|
|
|
+ setPageConfiguration(exporter, jasperPrint, pageIndex);
|
|
|
ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
|
|
|
exporter.setExporterInput(exporterInput);
|
|
|
- WriterExporterOutput exporterOutput = new SimpleWriterExporterOutput(outputStream);
|
|
|
+ WriterExporterOutput exporterOutput = new SimpleWriterExporterOutput(outFile);
|
|
|
exporter.setExporterOutput(exporterOutput);
|
|
|
exporter.exportReport();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 设置分页配置
|
|
|
+ *
|
|
|
+ * @param exporter
|
|
|
+ * @param jasperPrint
|
|
|
+ * @param pageIndex
|
|
|
+ */
|
|
|
+ private void setPageConfiguration(JRAbstractExporter exporter, JasperPrint jasperPrint, Integer pageIndex) {
|
|
|
+ if (pageIndex != null) {
|
|
|
+ // 前端显示时页码从1开始,生成报表时页码从0开始
|
|
|
+ pageIndex -= 1;
|
|
|
+ // 页码并非有效数值,重置为第一页
|
|
|
+ if (pageIndex < 0 || pageIndex >= jasperPrint.getPages().size()) {
|
|
|
+ pageIndex = 0;
|
|
|
+ }
|
|
|
+ SimpleReportExportConfiguration configuration = new SimpleReportExportConfiguration();
|
|
|
+ configuration.setPageIndex(pageIndex);
|
|
|
+ exporter.setConfiguration(configuration);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 移除除了Column Header和Detail之外的元素
|
|
|
*
|