|
|
@@ -1,7 +1,9 @@
|
|
|
package com.uas.report.service.impl;
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
|
+import java.io.FileReader;
|
|
|
import java.io.FileWriter;
|
|
|
import java.io.IOException;
|
|
|
import java.io.OutputStream;
|
|
|
@@ -12,8 +14,12 @@ import java.sql.ResultSetMetaData;
|
|
|
import java.sql.SQLException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
|
|
@@ -313,25 +319,25 @@ public class PrintServiceImpl implements PrintService {
|
|
|
// 如果查询语句中含有WHERE_CONDITION参数,需将其替换掉
|
|
|
if (queryString.contains("$P!{WHERE_CONDITION}")) {
|
|
|
queryString = queryString.substring(0, queryString.indexOf("$P!{WHERE_CONDITION}"))
|
|
|
- + "where rownum = 1";
|
|
|
+ + "where rownum = 0";
|
|
|
}
|
|
|
// 如果查询语句中含有where条件,则可能有用到一些参数,需将其替换掉
|
|
|
else if (queryString.toUpperCase().contains("WHERE")) {
|
|
|
- queryString = queryString.substring(0, queryString.indexOf("WHERE")) + "where rownum = 1";
|
|
|
+ queryString = queryString.substring(0, queryString.indexOf("WHERE")) + "where rownum = 0";
|
|
|
}
|
|
|
List<String> columnNames = getColumnNames(connection, queryString);
|
|
|
if (CollectionUtils.isEmpty(columnNames)) {
|
|
|
throw new ReportException("未查询到任何列:" + queryString);
|
|
|
}
|
|
|
+ Set<String> usedFields = getUsedFields(jrxmlFilePath);
|
|
|
// 指定的field
|
|
|
- @SuppressWarnings("rawtypes")
|
|
|
- List fieldElements = rootElement.elements("field");
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ List<Element> elements = rootElement.elements("field");
|
|
|
boolean needModifying = false;
|
|
|
- for (int i = 0; i < fieldElements.size(); i++) {
|
|
|
- Element element = (Element) fieldElements.get(i);
|
|
|
+ for (Element element : elements) {
|
|
|
String columnName = element.attribute("name").getText();
|
|
|
- // 如果指定的Field在当前帐套的表中不存在,则删除该Field
|
|
|
- if (!columnNames.contains(columnName.toUpperCase())) {
|
|
|
+ // 如果指定的Field在当前帐套的表中不存在,或者在模板中并未实际使用,则删除该Field
|
|
|
+ if (!columnNames.contains(columnName.toUpperCase()) || !usedFields.contains(columnName.toUpperCase())) {
|
|
|
needModifying = true;
|
|
|
rootElement.remove(element);
|
|
|
logger.info("Removed... " + columnName);
|
|
|
@@ -375,6 +381,29 @@ public class PrintServiceImpl implements PrintService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取模板中实际使用的Field
|
|
|
+ *
|
|
|
+ * @param jrxmlFilePath
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private Set<String> getUsedFields(String jrxmlFilePath) throws IOException {
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(new FileReader(jrxmlFilePath));
|
|
|
+ String line = null;
|
|
|
+ Set<String> result = new HashSet<>();
|
|
|
+ while (!StringUtils.isEmpty((line = bufferedReader.readLine()))) {
|
|
|
+ // 格式为$F{PD_TOTAL}
|
|
|
+ Pattern pattern = Pattern.compile("\\$F\\{([^\\$]*)\\}");
|
|
|
+ Matcher matcher = pattern.matcher(line);
|
|
|
+ while (matcher.find()) {
|
|
|
+ result.add(matcher.group(1));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ bufferedReader.close();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 以xls的格式导出报表
|
|
|
*
|