|
|
@@ -1,11 +1,9 @@
|
|
|
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;
|
|
|
@@ -48,6 +46,7 @@ import com.uas.report.model.Master;
|
|
|
import com.uas.report.service.FileService;
|
|
|
import com.uas.report.service.PrintService;
|
|
|
import com.uas.report.util.CollectionUtils;
|
|
|
+import com.uas.report.util.FileUtils;
|
|
|
import com.uas.report.util.MasterManager;
|
|
|
import com.uas.report.util.Platform;
|
|
|
import com.uas.report.util.ReportConstants;
|
|
|
@@ -179,8 +178,8 @@ public class PrintServiceImpl implements PrintService {
|
|
|
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
|
|
|
|
|
|
} else if (exportFileType.equals(ReportConstants.FILE_TYPE_WORD)) {
|
|
|
- // 导出word时需要对字体、行距等进行调整
|
|
|
- InputStream inputStream = new ByteArrayInputStream(replaceFont(jrxmlFilePath).getBytes());
|
|
|
+ // 导出word时需要对行距等进行调整
|
|
|
+ InputStream inputStream = new ByteArrayInputStream(modifyLineSpacing(jrxmlFilePath).getBytes());
|
|
|
JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
|
|
|
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
|
|
|
inputStream.close();
|
|
|
@@ -232,8 +231,11 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @param otherParameters
|
|
|
* @param connection
|
|
|
* @return 编译后的jasper文件路径
|
|
|
+ * @throws IOException
|
|
|
+ * @throws FileNotFoundException
|
|
|
*/
|
|
|
- private String maybeCompileJrxmlFile(String jrxmlFilePath, String otherParameters, Connection connection) {
|
|
|
+ private String maybeCompileJrxmlFile(String jrxmlFilePath, String otherParameters, Connection connection)
|
|
|
+ throws FileNotFoundException, IOException {
|
|
|
if (StringUtils.isEmpty(jrxmlFilePath)) {
|
|
|
logger.error("参数不能为空:" + jrxmlFilePath);
|
|
|
return null;
|
|
|
@@ -263,12 +265,11 @@ public class PrintServiceImpl implements PrintService {
|
|
|
if (!jasperFile.exists()) {
|
|
|
logger.info("正在编译报表模板... " + jrxmlFilePath);
|
|
|
compileReportToFile(jrxmlFilePath, jasperFilePath, otherParameters, connection);
|
|
|
- } else {
|
|
|
- // 如果在编译之后,报表模板有更改过 ,重新编译
|
|
|
- if (jrxmlFile.lastModified() > jasperFile.lastModified()) {
|
|
|
- logger.info("正在重新编译报表模板... " + jrxmlFilePath);
|
|
|
- compileReportToFile(jrxmlFilePath, jasperFilePath, otherParameters, connection);
|
|
|
- }
|
|
|
+ }
|
|
|
+ // 如果在编译之后,报表模板有更改过 ,重新编译
|
|
|
+ else if (jrxmlFile.lastModified() > jasperFile.lastModified()) {
|
|
|
+ logger.info("正在重新编译报表模板... " + jrxmlFilePath);
|
|
|
+ compileReportToFile(jrxmlFilePath, jasperFilePath, otherParameters, connection);
|
|
|
}
|
|
|
} catch (JRException e) {
|
|
|
logger.error("", e);
|
|
|
@@ -317,9 +318,12 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @param connection
|
|
|
* 数据库连接
|
|
|
* @throws JRException
|
|
|
+ * @throws IOException
|
|
|
+ * @throws FileNotFoundException
|
|
|
*/
|
|
|
private void compileReportToFile(String jrxmlFilePath, String jasperFilePath, String otherParameters,
|
|
|
- Connection connection) throws JRException {
|
|
|
+ Connection connection) throws JRException, FileNotFoundException, IOException {
|
|
|
+ replaceFont(jrxmlFilePath);
|
|
|
processFields(jrxmlFilePath, otherParameters, connection);
|
|
|
JasperCompileManager.compileReportToFile(jrxmlFilePath, jasperFilePath);
|
|
|
}
|
|
|
@@ -413,31 +417,42 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
private Set<String> getUsedFields(String jrxmlFilePath) throws IOException {
|
|
|
- BufferedReader bufferedReader = new BufferedReader(new FileReader(jrxmlFilePath));
|
|
|
- String line = null;
|
|
|
+ String xml = FileUtils.readAsString(new File(jrxmlFilePath));
|
|
|
Set<String> result = new HashSet<>();
|
|
|
- while ((line = bufferedReader.readLine()) != null) {
|
|
|
- // 格式为$F{PD_TOTAL}
|
|
|
- Pattern pattern = Pattern.compile("\\$F\\{([^\\$]*)\\}");
|
|
|
- Matcher matcher = pattern.matcher(line);
|
|
|
- while (matcher.find()) {
|
|
|
- result.add(matcher.group(1).toUpperCase());
|
|
|
- }
|
|
|
+ // 格式为$F{PD_TOTAL}
|
|
|
+ Pattern pattern = Pattern.compile("\\$F\\{([^\\$]*)\\}");
|
|
|
+ Matcher matcher = pattern.matcher(xml);
|
|
|
+ while (matcher.find()) {
|
|
|
+ result.add(matcher.group(1).toUpperCase());
|
|
|
}
|
|
|
- bufferedReader.close();
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 替换jrxml中的字体等信息
|
|
|
+ * 替换字体
|
|
|
*
|
|
|
* @param jrxmlFilePath
|
|
|
- * @return
|
|
|
+ * @throws IOException
|
|
|
+ * @throws FileNotFoundException
|
|
|
+ */
|
|
|
+ private void replaceFont(String jrxmlFilePath) throws FileNotFoundException, IOException {
|
|
|
+ File jrxmlFile = new File(jrxmlFilePath);
|
|
|
+ String xml = FileUtils.readAsString(jrxmlFile);
|
|
|
+ xml = xml.replace("MSYahei", ReportConstants.MICROSOFT_YAHEI_UI_FONT_NAME);
|
|
|
+ FileUtils.write(jrxmlFile, xml);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更改可换行的textField的行距
|
|
|
+ *
|
|
|
+ * @param jrxmlFilePath
|
|
|
+ * @return 修改后的jrxml源码
|
|
|
* @throws FileNotFoundException
|
|
|
* @throws IOException
|
|
|
* @throws DocumentException
|
|
|
*/
|
|
|
- private String replaceFont(String jrxmlFilePath) throws FileNotFoundException, IOException, DocumentException {
|
|
|
+ private String modifyLineSpacing(String jrxmlFilePath)
|
|
|
+ throws FileNotFoundException, IOException, DocumentException {
|
|
|
File jrxmlFile = new File(jrxmlFilePath);
|
|
|
SAXReader saxReader = new SAXReader();
|
|
|
Document document = saxReader.read(jrxmlFile);
|
|
|
@@ -445,17 +460,6 @@ public class PrintServiceImpl implements PrintService {
|
|
|
// 获取所有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']");
|
|
|
- if (fontElement == null) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- // 将字体MSYahei改为Microsoft YaHei UI
|
|
|
- Attribute fontName = fontElement.attribute("fontName");
|
|
|
- if (Objects.equals(fontName.getText(), "MSYahei")) {
|
|
|
- fontName.setText(ReportConstants.MICROSOFT_YAHEI_UI_FONT_NAME);
|
|
|
- }
|
|
|
-
|
|
|
// 更改可换行的textField的行距
|
|
|
Element parent = textElement.getParent();
|
|
|
if (parent != null && Objects.equals(parent.getName(), "textField")) {
|