|
|
@@ -232,9 +232,10 @@ public class CrystalToJasper {
|
|
|
map(rptXmlFile, subReportFile, styleFile);
|
|
|
// 修改报表名称
|
|
|
modifyReportName(subReportFile, subReportName);
|
|
|
- // 修改查询语句
|
|
|
- String queryString = parseQueryString(rptXmlFile);
|
|
|
- modifyQueryString(subReportFile, queryString);
|
|
|
+ // 处理查询语句
|
|
|
+ processQueryString(rptXmlFile, subReportFile);
|
|
|
+ // 移除模板中多余field
|
|
|
+ removeUnusedFields(subReportFile);
|
|
|
// 移除模板中多余变量
|
|
|
removeUnusedVariables(subReportFile);
|
|
|
}
|
|
|
@@ -245,9 +246,10 @@ public class CrystalToJasper {
|
|
|
map(rptXmlFile, reportFile, styleFile);
|
|
|
// 修改报表名称
|
|
|
modifyReportName(reportFile, reportName);
|
|
|
- // 修改查询语句
|
|
|
- String queryString = parseQueryString(rptXmlFile);
|
|
|
- modifyQueryString(reportFile, queryString);
|
|
|
+ // 处理查询语句
|
|
|
+ processQueryString(rptXmlFile, reportFile);
|
|
|
+ // 移除模板中多余field
|
|
|
+ removeUnusedFields(reportFile);
|
|
|
// 移除模板中多余变量
|
|
|
removeUnusedVariables(reportFile);
|
|
|
// 替换子报表路径
|
|
|
@@ -322,6 +324,54 @@ public class CrystalToJasper {
|
|
|
saveXmlFile(document, jrxmlFile);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 移除模板中多余field
|
|
|
+ *
|
|
|
+ * @param jrxmlFile
|
|
|
+ * 报表
|
|
|
+ * @throws DocumentException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private void removeUnusedFields(File jrxmlFile) throws DocumentException, IOException {
|
|
|
+ Set<String> usedFields = getUsedFields(jrxmlFile);
|
|
|
+ if (!CollectionUtils.isEmpty(usedFields)) {
|
|
|
+ Document document = new SAXReader().read(jrxmlFile);
|
|
|
+ List<Element> elements = document.selectNodes("//*[name()='field']");
|
|
|
+ for (Element element : elements) {
|
|
|
+ String columnName = element.attributeValue("name");
|
|
|
+ // field未实际使用,移除元素
|
|
|
+ if (!usedFields.contains(columnName.toUpperCase())) {
|
|
|
+ element.getParent().remove(element);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ saveXmlFile(document, jrxmlFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取模板中实际使用的Field
|
|
|
+ *
|
|
|
+ * @param jrxmlFilePath
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private Set<String> getUsedFields(File jrxmlFile) throws IOException {
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(new FileReader(jrxmlFile));
|
|
|
+ String line = null;
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ bufferedReader.close();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 移除模板中多余变量
|
|
|
*
|
|
|
@@ -408,11 +458,15 @@ public class CrystalToJasper {
|
|
|
*
|
|
|
* @param rptXmlFile
|
|
|
* rpt的xml文件
|
|
|
+ * @param jrxmlFile
|
|
|
+ * 报表
|
|
|
* @return sql查询语句
|
|
|
* @throws DocumentException
|
|
|
+ * @throws IOException
|
|
|
*/
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- private String parseQueryString(File rptXmlFile) throws DocumentException {
|
|
|
+ private String parseQueryString(File rptXmlFile, File jrxmlFile) throws DocumentException, IOException {
|
|
|
+ Set<String> usedFields = getUsedFields(jrxmlFile);
|
|
|
Document document = new SAXReader().read(rptXmlFile);
|
|
|
|
|
|
// 字段
|
|
|
@@ -436,7 +490,10 @@ public class CrystalToJasper {
|
|
|
List<String> columns = new ArrayList<>();
|
|
|
for (Element columnElement : columnElements) {
|
|
|
String columnName = columnElement.attribute("name").getText();
|
|
|
- columns.add(columnName);
|
|
|
+ // 实际使用过的列才拼接到sql中
|
|
|
+ if (usedFields.contains((alias + "." + columnName).toUpperCase())) {
|
|
|
+ columns.add(columnName);
|
|
|
+ }
|
|
|
}
|
|
|
columnNames.put(alias, columns);
|
|
|
}
|
|
|
@@ -637,16 +694,17 @@ public class CrystalToJasper {
|
|
|
/**
|
|
|
* 修改queryString节点
|
|
|
*
|
|
|
+ * @param rptXmlFile
|
|
|
+ * rpt的xml文件
|
|
|
* @param jrxmlFile
|
|
|
* 报表
|
|
|
- * @param queryString
|
|
|
- * queryString的值
|
|
|
* @throws DocumentException
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- private void modifyQueryString(File jrxmlFile, String queryString) throws DocumentException, IOException {
|
|
|
+ private void processQueryString(File rptXmlFile, File jrxmlFile) throws DocumentException, IOException {
|
|
|
Document document = new SAXReader().read(jrxmlFile);
|
|
|
Node node = document.selectSingleNode("//*[name()='queryString']");
|
|
|
+ String queryString = parseQueryString(rptXmlFile, jrxmlFile);
|
|
|
node.setText(queryString);
|
|
|
saveXmlFile(document, jrxmlFile);
|
|
|
}
|