Browse Source

报表支持导出为 txt

sunyj 8 years ago
parent
commit
6a94f7d0a5

+ 5 - 0
src/main/java/com/uas/report/controller/PrintController.java

@@ -105,6 +105,11 @@ public class PrintController {
 		else if (printType.equals(ReportConstants.PRINT_TYPE_WORD)) {
 			export(userName, profile, reportName, whereCondition, otherParameters, ReportConstants.FILE_TYPE_WORD, true,
 					title, request, response);
+		}
+		// 下载text
+		else if (printType.equals(ReportConstants.PRINT_TYPE_TEXT)) {
+			export(userName, profile, reportName, whereCondition, otherParameters, ReportConstants.FILE_TYPE_TEXT, true,
+					title, request, response);
 		} else {
 			throw new IllegalArgumentException("printType不合法");
 		}

+ 19 - 0
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -62,6 +62,7 @@ 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.JRTextExporter;
 import net.sf.jasperreports.engine.export.JRXlsExporter;
 import net.sf.jasperreports.engine.xml.JRXmlLoader;
 import net.sf.jasperreports.export.ExporterInput;
@@ -203,6 +204,8 @@ public class PrintServiceImpl implements PrintService {
 					exportReportToXls(jasperPrint, outputStream, customCellStyle);
 				} else if (exportFileType.equals(ReportConstants.FILE_TYPE_WORD)) {
 					exportReportToDoc(jasperPrint, outputStream);
+				} else if (exportFileType.equals(ReportConstants.FILE_TYPE_TEXT)) {
+					exportReportToText(jasperPrint, outputStream);
 				} else {
 					exportReportToPdf(jasperPrint, outputStream, pageIndex);
 				}
@@ -567,6 +570,22 @@ public class PrintServiceImpl implements PrintService {
 		exporter.exportReport();
 	}
 
+	/**
+	 * 以text的格式导出报表
+	 * 
+	 * @param jasperPrint
+	 * @param outputStream
+	 * @throws JRException
+	 */
+	private void exportReportToText(JasperPrint jasperPrint, OutputStream outputStream) throws JRException {
+		JRTextExporter exporter = new JRTextExporter();
+		ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
+		exporter.setExporterInput(exporterInput);
+		WriterExporterOutput exporterOutput = new SimpleWriterExporterOutput(outputStream);
+		exporter.setExporterOutput(exporterOutput);
+		exporter.exportReport();
+	}
+
 	/**
 	 * 移除除了Column Header和Detail之外的元素
 	 * 

+ 10 - 0
src/main/java/com/uas/report/util/ReportConstants.java

@@ -43,6 +43,11 @@ public class ReportConstants {
 	 */
 	public static final String PRINT_TYPE_WORD = "WORD";
 
+	/**
+	 * 请求类型:下载text
+	 */
+	public static final String PRINT_TYPE_TEXT = "TEXT";
+
 	/**
 	 * 导出文件的格式:pdf
 	 */
@@ -63,6 +68,11 @@ public class ReportConstants {
 	 */
 	public static final String FILE_TYPE_WORD = "doc";
 
+	/**
+	 * 导出文件的格式:text
+	 */
+	public static final String FILE_TYPE_TEXT = "txt";
+
 	/**
 	 * 生成的pdf、excel等文件的相对路径
 	 */

+ 4 - 0
src/main/webapp/WEB-INF/views/preview.html

@@ -62,6 +62,10 @@
 				title="下载Excel(仅数据)">
 				<i class="fa fa-file-excel-o fa-lg" aria-hidden="true"></i>
 			</button>
+			<button id="downloadText" class="toolbarButton"
+				title="下载Text">
+				<i class="fa fa-file-text-o fa-lg" aria-hidden="true"></i>
+			</button>
 		</div>
 	</div>
 

+ 8 - 0
src/main/webapp/resources/js/preview/app.js

@@ -134,6 +134,14 @@ $("#downloadExcelData").click(function() {
 	window.open(downloadUrl("xls_data"));
 });
 
+//下载text
+$("#downloadText").click(function() {
+	if (!pdfDoc) {
+		return;
+	}
+	window.open(downloadUrl("txt"));
+});
+
 // 键盘左右键进行翻页
 $("body").keydown(function(event) {
 	// 如果在选中input输入框或select下拉列表时按左右键,不进行翻页