Browse Source

支持导出为只包含数据的Excel表格

sunyj 9 years ago
parent
commit
2253e3794f

+ 7 - 1
src/main/java/com/uas/report/controller/PrintController.java

@@ -81,7 +81,13 @@ public class PrintController {
 		}
 
 		try {
-			response.setHeader("Content-Disposition", "attachment;filename=" + reportName + "." + exportFileType);
+			String exportFileName = reportName;
+			if (exportFileType.equals("xls_with_only_data")) {
+				exportFileName += ".xls";
+			} else {
+				exportFileName += "." + exportFileType;
+			}
+			response.setHeader("Content-Disposition", "attachment;filename=" + exportFileName);
 			OutputStream outputStream = response.getOutputStream();
 			outputStream.write(data);
 			outputStream.flush();

+ 53 - 13
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -14,11 +14,24 @@ import java.util.Properties;
 
 import javax.sql.DataSource;
 
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
+
+import com.alibaba.druid.pool.DruidDataSource;
+import com.alibaba.druid.pool.DruidDataSourceFactory;
+import com.uas.report.core.exception.SystemError;
+import com.uas.report.service.PrintService;
+import com.uas.report.util.ContextUtils;
+import com.uas.report.util.ReportConstants;
+
 import net.sf.jasperreports.engine.JRException;
 import net.sf.jasperreports.engine.JasperCompileManager;
 import net.sf.jasperreports.engine.JasperExportManager;
 import net.sf.jasperreports.engine.JasperFillManager;
 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.JRXlsExporter;
@@ -29,18 +42,6 @@ import net.sf.jasperreports.export.SimpleExporterInput;
 import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
 import net.sf.jasperreports.export.SimplePdfReportConfiguration;
 
-import org.apache.log4j.Logger;
-import org.springframework.stereotype.Service;
-import org.springframework.util.CollectionUtils;
-import org.springframework.util.StringUtils;
-
-import com.alibaba.druid.pool.DruidDataSource;
-import com.alibaba.druid.pool.DruidDataSourceFactory;
-import com.uas.report.core.exception.SystemError;
-import com.uas.report.service.PrintService;
-import com.uas.report.util.ContextUtils;
-import com.uas.report.util.ReportConstants;
-
 /**
  * 报表打印
  * 
@@ -148,11 +149,24 @@ public class PrintServiceImpl implements PrintService {
 			Map<String, Object> result = new HashMap<>();
 
 			// 从数据库获取数据填充报表
-			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
+			JasperPrint jasperPrint = null;
 			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 
 			// exportFileType导出文件的格式不为空,表示是导出,并非预览
 			if (!StringUtils.isEmpty(exportFileType)) {
+				// 只导出数据
+				if (exportFileType.equals("xls_with_only_data")) {
+					JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFile);
+					// 移除多余元素
+					removeUnusedElements(jasperDesign);
+					exportFileType = "xls";
+					JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
+					jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
+
+				} else {
+					jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
+				}
+
 				boolean exportSucceeded = exportReport(jasperPrint, exportFileType, outputStream);
 				if (exportSucceeded) {
 					byte[] data = outputStream.toByteArray();
@@ -164,6 +178,7 @@ public class PrintServiceImpl implements PrintService {
 			// 报表预览,则直接输出pdf,并且需要进行分页
 			else if (pageIndex != null) {
 				// 页码并非有效数值,重置为第一页
+				jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
 				if (pageIndex < 0 || pageIndex >= jasperPrint.getPages().size()) {
 					pageIndex = 0;
 				}
@@ -328,4 +343,29 @@ public class PrintServiceImpl implements PrintService {
 		exporter.setExporterOutput(exporterOutput);
 		exporter.exportReport();
 	}
+
+	/**
+	 * 移除除Column Header和Detail之外的元素
+	 * 
+	 * @param jasperDesign
+	 */
+	private void removeUnusedElements(JasperDesign jasperDesign) {
+		if (jasperDesign == null) {
+			return;
+		}
+		jasperDesign.setTitle(null);
+		jasperDesign.setPageHeader(null);
+		jasperDesign.setColumnFooter(null);
+		jasperDesign.setPageFooter(null);
+		jasperDesign.setLastPageFooter(null);
+		jasperDesign.setSummary(null);
+		jasperDesign.setNoData(null);
+		jasperDesign.setBackground(null);
+		// 忽略分页,以使列名只打印一次
+		jasperDesign.setIgnorePagination(true);
+		jasperDesign.setTopMargin(0);
+		jasperDesign.setLeftMargin(0);
+		jasperDesign.setBottomMargin(0);
+		jasperDesign.setRightMargin(0);
+	}
 }