|
|
@@ -24,13 +24,19 @@ import org.springframework.util.StringUtils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.uas.report.core.exception.ReportException;
|
|
|
+import com.uas.report.jasperreports.engine.export.CustomJRXlsExporter;
|
|
|
import com.uas.report.service.FileService;
|
|
|
import com.uas.report.service.PrintService;
|
|
|
import com.uas.report.util.DataSourceUtils;
|
|
|
import com.uas.report.util.FileUtils;
|
|
|
import com.uas.report.util.ReportConstants;
|
|
|
|
|
|
+import net.sf.jasperreports.engine.JRBand;
|
|
|
+import net.sf.jasperreports.engine.JRElement;
|
|
|
import net.sf.jasperreports.engine.JRException;
|
|
|
+import net.sf.jasperreports.engine.JRExporterParameter;
|
|
|
+import net.sf.jasperreports.engine.JRSection;
|
|
|
+import net.sf.jasperreports.engine.JRTextField;
|
|
|
import net.sf.jasperreports.engine.JasperCompileManager;
|
|
|
import net.sf.jasperreports.engine.JasperExportManager;
|
|
|
import net.sf.jasperreports.engine.JasperFillManager;
|
|
|
@@ -39,12 +45,15 @@ import net.sf.jasperreports.engine.JasperReport;
|
|
|
import net.sf.jasperreports.engine.design.JasperDesign;
|
|
|
import net.sf.jasperreports.engine.export.JRPdfExporter;
|
|
|
import net.sf.jasperreports.engine.export.JRXlsExporter;
|
|
|
+import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
|
|
|
import net.sf.jasperreports.engine.xml.JRXmlLoader;
|
|
|
import net.sf.jasperreports.export.ExporterInput;
|
|
|
import net.sf.jasperreports.export.OutputStreamExporterOutput;
|
|
|
import net.sf.jasperreports.export.SimpleExporterInput;
|
|
|
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
|
|
|
import net.sf.jasperreports.export.SimplePdfReportConfiguration;
|
|
|
+import net.sf.jasperreports.export.SimpleXlsExporterConfiguration;
|
|
|
+import net.sf.jasperreports.export.XlsExporterConfiguration;
|
|
|
|
|
|
/**
|
|
|
* 报表打印
|
|
|
@@ -138,8 +147,11 @@ public class PrintServiceImpl implements PrintService {
|
|
|
// exportFileType导出文件的格式不为空,表示是导出,并非预览
|
|
|
if (!StringUtils.isEmpty(exportFileType)) {
|
|
|
logger.info("export fillReport...");
|
|
|
+ boolean customCellStyle = false;
|
|
|
// 只导出数据
|
|
|
if (exportFileType.equals("xls_with_only_data")) {
|
|
|
+ // 需自定义单元格格式
|
|
|
+ customCellStyle = true;
|
|
|
JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFilePath);
|
|
|
// 移除模板中多余元素
|
|
|
removeUnusedElements(jasperDesign);
|
|
|
@@ -151,7 +163,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
|
|
|
}
|
|
|
|
|
|
- exportReport(jasperPrint, exportFileType, outputStream);
|
|
|
+ exportReportToXls(jasperPrint, outputStream, customCellStyle);
|
|
|
logger.info("export fillReport done...");
|
|
|
data = outputStream.toByteArray();
|
|
|
outputStream.close();
|
|
|
@@ -285,19 +297,20 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @param exportFileType
|
|
|
* @param outputStream
|
|
|
*/
|
|
|
- private void exportReport(JasperPrint jasperPrint, String exportFileType, OutputStream outputStream) {
|
|
|
- try {
|
|
|
- if (exportFileType.equals("pdf")) {
|
|
|
- exportReportToPdf(jasperPrint, outputStream);
|
|
|
- } else if (exportFileType.equals("xls")) {
|
|
|
- exportReportToXls(jasperPrint, outputStream);
|
|
|
- } else {
|
|
|
- throw new ReportException("不支持导出为 " + exportFileType + "格式!");
|
|
|
- }
|
|
|
- } catch (JRException e) {
|
|
|
- throw new ReportException(e).setDetailedMessage(e);
|
|
|
- }
|
|
|
- }
|
|
|
+ // private void exportReport(JasperPrint jasperPrint, String exportFileType,
|
|
|
+ // OutputStream outputStream) {
|
|
|
+ // try {
|
|
|
+ // if (exportFileType.equals("pdf")) {
|
|
|
+ // exportReportToPdf(jasperPrint, outputStream);
|
|
|
+ // } else if (exportFileType.equals("xls")) {
|
|
|
+ // exportReportToXls(jasperPrint, outputStream);
|
|
|
+ // } else {
|
|
|
+ // throw new ReportException("不支持导出为 " + exportFileType + "格式!");
|
|
|
+ // }
|
|
|
+ // } catch (JRException e) {
|
|
|
+ // throw new ReportException(e).setDetailedMessage(e);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
|
|
|
/**
|
|
|
* 以pdf的格式导出报表
|
|
|
@@ -317,8 +330,15 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @param outputStream
|
|
|
* @throws JRException
|
|
|
*/
|
|
|
- private void exportReportToXls(JasperPrint jasperPrint, OutputStream outputStream) throws JRException {
|
|
|
- JRXlsExporter exporter = new JRXlsExporter();
|
|
|
+ private void exportReportToXls(JasperPrint jasperPrint, OutputStream outputStream, boolean customCellStyle)
|
|
|
+ throws JRException {
|
|
|
+ JRXlsExporter exporter = new CustomJRXlsExporter(customCellStyle);
|
|
|
+ // SimpleXlsExporterConfiguration configuration = new
|
|
|
+ // SimpleXlsExporterConfiguration();
|
|
|
+ // configuration.setCreateCustomPalette(true);
|
|
|
+ // configuration.setKeepWorkbookTemplateSheets(true);
|
|
|
+ // configuration.setOverrideHints(true);
|
|
|
+ // exporter.setConfiguration(configuration);
|
|
|
ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
|
|
|
exporter.setExporterInput(exporterInput);
|
|
|
OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
|