|
|
@@ -1,10 +1,13 @@
|
|
|
package com.uas.report.service.impl;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
import java.io.FileReader;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
import java.sql.Connection;
|
|
|
import java.sql.PreparedStatement;
|
|
|
@@ -16,6 +19,7 @@ import java.util.HashMap;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.Set;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
@@ -24,12 +28,14 @@ import javax.sql.DataSource;
|
|
|
|
|
|
import org.apache.commons.io.output.FileWriterWithEncoding;
|
|
|
import org.apache.commons.lang.ArrayUtils;
|
|
|
+import org.dom4j.Attribute;
|
|
|
import org.dom4j.Document;
|
|
|
import org.dom4j.DocumentException;
|
|
|
import org.dom4j.Element;
|
|
|
import org.dom4j.Node;
|
|
|
import org.dom4j.io.SAXReader;
|
|
|
import org.dom4j.io.XMLWriter;
|
|
|
+import org.dom4j.tree.DefaultElement;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -44,8 +50,8 @@ import com.uas.report.jasperreports.engine.export.CustomJRXlsExporter;
|
|
|
import com.uas.report.model.Master;
|
|
|
import com.uas.report.service.FileService;
|
|
|
import com.uas.report.service.PrintService;
|
|
|
-import com.uas.report.util.MasterManager;
|
|
|
import com.uas.report.util.FileUtils;
|
|
|
+import com.uas.report.util.MasterManager;
|
|
|
import com.uas.report.util.ReportConstants;
|
|
|
import com.uas.report.util.ReportUtils;
|
|
|
|
|
|
@@ -56,6 +62,7 @@ import net.sf.jasperreports.engine.JasperPrint;
|
|
|
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.JRRtfExporter;
|
|
|
import net.sf.jasperreports.engine.export.JRXlsExporter;
|
|
|
import net.sf.jasperreports.engine.xml.JRXmlLoader;
|
|
|
import net.sf.jasperreports.export.ExporterInput;
|
|
|
@@ -63,6 +70,8 @@ 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.SimpleWriterExporterOutput;
|
|
|
+import net.sf.jasperreports.export.WriterExporterOutput;
|
|
|
|
|
|
/**
|
|
|
* 报表打印
|
|
|
@@ -160,22 +169,30 @@ public class PrintServiceImpl implements PrintService {
|
|
|
logger.info("export fillReport...");
|
|
|
boolean customCellStyle = false;
|
|
|
// 只导出数据
|
|
|
- if (exportFileType.equals("xls_with_only_data")) {
|
|
|
+ if (exportFileType.equals(ReportConstants.EXCEL_WITH_ONLY_DATA_FILE_TYPE)) {
|
|
|
// 需自定义单元格格式
|
|
|
customCellStyle = true;
|
|
|
JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFilePath);
|
|
|
// 移除模板中多余元素
|
|
|
removeUnusedElements(jasperDesign);
|
|
|
- exportFileType = "xls";
|
|
|
+ exportFileType = ReportConstants.EXCEL_FILE_TYPE;
|
|
|
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
|
|
|
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
|
|
|
|
|
|
+ } else if (exportFileType.equals(ReportConstants.WORD_FILE_TYPE)) {
|
|
|
+ // 导出word时需要对字体、行距等进行调整
|
|
|
+ InputStream inputStream = new ByteArrayInputStream(replaceFont(jrxmlFilePath).getBytes());
|
|
|
+ JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
|
|
|
+ jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
|
|
|
+ inputStream.close();
|
|
|
} else {
|
|
|
jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
|
|
|
}
|
|
|
|
|
|
- if (exportFileType.equals("xls")) {
|
|
|
+ if (exportFileType.equals(ReportConstants.EXCEL_FILE_TYPE)) {
|
|
|
exportReportToXls(jasperPrint, outputStream, customCellStyle);
|
|
|
+ } else if (exportFileType.equals(ReportConstants.WORD_FILE_TYPE)) {
|
|
|
+ exportReportToDoc(jasperPrint, outputStream);
|
|
|
} else {
|
|
|
exportReportToPdf(jasperPrint, outputStream, pageIndex);
|
|
|
}
|
|
|
@@ -408,6 +425,48 @@ public class PrintServiceImpl implements PrintService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 替换jrxml中的字体等信息
|
|
|
+ *
|
|
|
+ * @param jrxmlFilePath
|
|
|
+ * @return
|
|
|
+ * @throws FileNotFoundException
|
|
|
+ * @throws IOException
|
|
|
+ * @throws DocumentException
|
|
|
+ */
|
|
|
+ private String replaceFont(String jrxmlFilePath) throws FileNotFoundException, IOException, DocumentException {
|
|
|
+ File jrxmlFile = new File(jrxmlFilePath);
|
|
|
+ SAXReader saxReader = new SAXReader();
|
|
|
+ Document document = saxReader.read(jrxmlFile);
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ // 获取所有textElement节点(jrxml的XPath格式为:/*[name()='jasperReport']/*[name()='columnHeader']/*[name()='band']/*[name()='textField'])
|
|
|
+ List<Element> textElements = document.selectNodes("//*[name()='textElement']");
|
|
|
+ for (Element textElement : textElements) {
|
|
|
+ // 获取font节点
|
|
|
+ Element fontElement = (Element) textElement.selectSingleNode("*[name()='font']");
|
|
|
+ // 将字体MSYahei改为Microsoft YaHei UI
|
|
|
+ Attribute fontName = fontElement.attribute("fontName");
|
|
|
+ if (Objects.equals(fontName.getText(), "MSYahei")) {
|
|
|
+ fontName.setText("Microsoft YaHei UI");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更改可换行的textField的行距
|
|
|
+ Element parent = textElement.getParent();
|
|
|
+ if (parent != null && Objects.equals(parent.getName(), "textField")) {
|
|
|
+ // 如果textElement节点指定isStretchWithOverflow为true(可换行),则调整行距
|
|
|
+ Attribute isStretchWithOverflow = parent.attribute("isStretchWithOverflow");
|
|
|
+ if (isStretchWithOverflow != null && Objects.equals(isStretchWithOverflow.getText(), "true")) {
|
|
|
+ // 添加节点paragraph,调整行距为固定值12磅
|
|
|
+ Element paragraphElement = new DefaultElement("paragraph", textElement.getNamespace());
|
|
|
+ paragraphElement.addAttribute("lineSpacing", "Fixed");
|
|
|
+ paragraphElement.addAttribute("lineSpacingSize", "12.0");
|
|
|
+ textElement.add(paragraphElement);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return document.asXML();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 以xls的格式导出报表
|
|
|
*
|
|
|
@@ -425,6 +484,35 @@ public class PrintServiceImpl implements PrintService {
|
|
|
exporter.exportReport();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 以doc的格式导出报表
|
|
|
+ *
|
|
|
+ * @param jasperPrint
|
|
|
+ * @param outputStream
|
|
|
+ * @throws JRException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private void exportReportToDoc(JasperPrint jasperPrint, OutputStream outputStream) throws JRException, IOException {
|
|
|
+ exportReportToRtf(jasperPrint, outputStream);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 以rtf的格式导出报表
|
|
|
+ *
|
|
|
+ * @param jasperPrint
|
|
|
+ * @param outputStream
|
|
|
+ * @throws JRException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private void exportReportToRtf(JasperPrint jasperPrint, OutputStream outputStream) throws JRException, IOException {
|
|
|
+ JRRtfExporter exporter = new JRRtfExporter();
|
|
|
+ ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
|
|
|
+ exporter.setExporterInput(exporterInput);
|
|
|
+ WriterExporterOutput exporterOutput = new SimpleWriterExporterOutput(outputStream);
|
|
|
+ exporter.setExporterOutput(exporterOutput);
|
|
|
+ exporter.exportReport();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 以pdf的格式导出报表
|
|
|
*
|