Bläddra i källkod

when export, write to file rather than return data

sunyj 8 år sedan
förälder
incheckning
9430855340

+ 12 - 11
report/src/main/java/com/uas/report/controller/PdfController.java

@@ -4,7 +4,8 @@ import com.uas.report.model.ExportType;
 import com.uas.report.model.Platform;
 import com.uas.report.service.FileService;
 import com.uas.report.service.PrintService;
-import com.uas.report.util.*;
+import com.uas.report.util.FileUtils;
+import com.uas.report.util.ReportUtils;
 import net.sf.jasperreports.engine.JRException;
 import org.dom4j.DocumentException;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -16,6 +17,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.sql.SQLException;
 import java.util.HashMap;
@@ -93,18 +95,14 @@ public class PdfController {
 			result.put("pageSize", 0);
 			result.put("overload", true);
 		} else {
-			result = printService.preview(u, pr, r, w, o, null);
-			if (CollectionUtils.isEmpty(result) || ArrayUtils.isEmpty((byte[]) result.get("data"))) {
-				throw new IllegalStateException("pdf生成失败:" + u + "/" + r);
-			}
-			byte[] data = (byte[]) result.remove("data");
 			// 相对路径
 			String pdfPath = r + "/"
 					+ fileService.generateFileName(u, pr, w, o, ExportType.PDF.getQualifier())
                     + "." + ExportType.PDF.getQualifier();
 			File file = new File(ReportUtils.getDocumentsDir(), pdfPath);
-			FileUtils.write(file.getPath(), data);
+			int pageSize = printService.export(u, pr, r, w, o, ExportType.PDF, file, null, true);
 			result.put("path", "pdf/preview?p=" + pdfPath);
+			result.put("pageSize", pageSize);
 			result.put("overload", false);
 		}
 		return result;
@@ -166,10 +164,13 @@ public class PdfController {
 			result.put("pageSize", 0);
 			result.put("overload", true);
 		} else {
-			result = printService.preview(u, pr, r, w, o, null);
-			if (CollectionUtils.isEmpty(result) || ArrayUtils.isEmpty((byte[]) result.get("data"))) {
-				throw new IllegalStateException("pdf生成失败:" + u + "/" + r);
-			}
+			String pdfPath = r + "/"
+					+ fileService.generateFileName(u, pr, w, o, ExportType.PDF.getQualifier())
+					+ "." + ExportType.PDF.getQualifier();
+			File file = new File(ReportUtils.getDocumentsDir(), pdfPath);
+			int pageSize = printService.export(u, pr, r, w, o, ExportType.PDF, file, null, true);
+			result.put("data", FileUtils.readData(new FileInputStream(file)));
+			result.put("pageSize", pageSize);
 			result.put("overload", false);
 		}
 		return result;

+ 10 - 15
report/src/main/java/com/uas/report/controller/PrintController.java

@@ -18,6 +18,7 @@ import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.sql.SQLException;
 import java.util.HashMap;
@@ -159,12 +160,7 @@ public class PrintController {
 			if (printService.overload(userName, profile, reportName, whereCondition, otherParameters, Platform.PC)) {
 				throw new IllegalStateException("数据量过大,无法提供服务");
 			}
-			byte[] data = printService.export(userName, profile, reportName, whereCondition, otherParameters,
-					exportType);
-			if (ArrayUtils.isEmpty(data)) {
-				throw new IllegalStateException("报表导出失败:" + userName + "/" + reportName + "\n");
-			}
-			FileUtils.write(file.getPath(), data);
+			printService.export(userName, profile, reportName, whereCondition, otherParameters, exportType, file, null, true);
 		}
 
 		String exportFileName = (!StringUtils.isEmpty(title) ? title : reportName) + "." + exportType.getQualifier();
@@ -204,7 +200,6 @@ public class PrintController {
 		ReportUtils.checkParameters(userName, reportName);
 		String masterOfJrxml = printService.getMasterOfJrxml(userName, reportName);
 
-        // 相对路径,返回给前端
         String pdfPath = reportName + "/"
                 + fileService.generateFileName(userName, profile, whereCondition, otherParameters, ExportType.PDF.getQualifier())
                 + "." + ExportType.PDF.getQualifier();
@@ -216,13 +211,7 @@ public class PrintController {
 			if (printService.overload(userName, profile, reportName, whereCondition, otherParameters, Platform.PC)) {
 				throw new IllegalStateException("数据量过大,无法提供服务");
 			}
-			Map<String, Object> result = printService.preview(userName, profile, reportName, whereCondition,
-					otherParameters, null);
-			if (CollectionUtils.isEmpty(result) || ArrayUtils.isEmpty((byte[]) result.get("data"))) {
-				throw new IllegalStateException("报表预览失败:" + userName + "/" + reportName);
-			}
-			byte[] data = (byte[]) result.remove("data");
-			FileUtils.write(file.getPath(), data);
+			printService.export(userName, profile, reportName, whereCondition, otherParameters, ExportType.PDF, file, null, true);
 		}
 		return "pdf/preview?p=" + pdfPath;
 	}
@@ -263,7 +252,13 @@ public class PrintController {
 			result.put("pageSize", 0);
 			result.put("overload", true);
 		} else {
-			result = printService.preview(userName, profile, reportName, whereCondition, otherParameters, null);
+			String pdfPath = reportName + "/"
+					+ fileService.generateFileName(userName, profile, whereCondition, otherParameters, ExportType.PDF.getQualifier())
+					+ "." + ExportType.PDF.getQualifier();
+			File file = new File(ReportUtils.getDocumentsDir(), pdfPath);
+			int pageSize = printService.export(userName, profile, reportName, whereCondition, otherParameters, ExportType.PDF, file, null, true);
+			result.put("data", FileUtils.readData(new FileInputStream(file)));
+			result.put("pageSize", pageSize);
 			result.put("overload", false);
 		}
 		return result;

+ 16 - 44
report/src/main/java/com/uas/report/service/PrintService.java

@@ -5,9 +5,9 @@ import com.uas.report.model.Platform;
 import net.sf.jasperreports.engine.JRException;
 import org.dom4j.DocumentException;
 
+import java.io.File;
 import java.io.IOException;
 import java.sql.SQLException;
-import java.util.Map;
 
 /**
  * 报表打印
@@ -18,56 +18,28 @@ import java.util.Map;
 public interface PrintService {
 
 	/**
-	 * 导出报表
-	 * 
-	 * @param userName
-	 *            不为null;当前账套用户名
-	 * @param profile
-	 *            用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
-	 * @param reportName
-	 *            不为null;需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase")
-	 * @param whereCondition
-	 *            可为null;where之后的条件(包括where)
-	 * @param otherParameters
-	 *            若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
-	 *            JSON格式,数据为键值对
-	 * @param exportType
-	 *            报表导出的格式,默认为pdf
-	 * @return 导出的文件的字节数组
+	 * 导出报表到指定文件
+	 *
+	 * @param userName           不为null;当前账套用户名
+	 * @param profile            用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
+	 * @param reportName         不为null;需要导出的报表的名称,不带任何后缀(如导出采购单,即为"Purchase")
+	 * @param whereCondition     可为null;where之后的条件(包括where)
+	 * @param otherParameters    若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
+	 *                           JSON格式,数据为键值对
+	 * @param exportType         不为null;报表导出的格式Z
+	 * @param exportFile         不为null;导出的文件
+	 * @param pageIndex          可为null;分页展示,当前页码,从0开始
+	 * @param useFileVirtualizer 是否使用文件虚拟化技术
+	 * @return 总页数
 	 * @throws SQLException
 	 * @throws DocumentException
 	 * @throws IOException
 	 * @throws JRException
 	 */
-	public byte[] export(String userName, String profile, String reportName, String whereCondition,
-			String otherParameters, ExportType exportType)
+	int export(String userName, String profile, String reportName, String whereCondition,
+			   String otherParameters, ExportType exportType, File exportFile, Integer pageIndex, boolean useFileVirtualizer)
 			throws JRException, IOException, DocumentException, SQLException;
 
-	/**
-	 * 预览报表
-	 * 
-	 * @param userName
-	 *            不为null;当前账套用户名
-	 * @param profile
-	 *            用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
-	 * @param reportName
-	 *            不为null;需要预览的报表的名称,不带任何后缀(如预览采购单,即为"Purchase")
-	 * @param whereCondition
-	 *            可为null;where之后的条件(包括where)
-	 * @param otherParameters
-	 *            若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
-	 *            JSON格式,数据为键值对
-	 * @param pageIndex
-	 *            分页展示,当前页码,从0开始
-	 * @return 报表数据"data": byte[];总页数"pageSize": Integer
-	 * @throws SQLException
-	 * @throws DocumentException
-	 * @throws IOException
-	 * @throws JRException
-	 */
-	public Map<String, Object> preview(String userName, String profile, String reportName, String whereCondition,
-			String otherParameters, Integer pageIndex) throws JRException, IOException, DocumentException, SQLException;
-
 	/**
 	 * 获取模板对应的账套
 	 * 

+ 91 - 122
report/src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -63,43 +63,8 @@ public class PrintServiceImpl implements PrintService {
 	private Logger logger = LoggerFactory.getLogger(getClass());
 
 	@Override
-	public byte[] export(String userName, String profile, String reportName, String whereCondition,
-			String otherParameters, ExportType exportType)
-			throws JRException, IOException, DocumentException, SQLException {
-		Map<String, Object> result = print(userName, profile, reportName, whereCondition, otherParameters, exportType,
-				null, true);
-		if (!CollectionUtils.isEmpty(result)) {
-			return (byte[]) result.get("data");
-		}
-		return null;
-	}
-
-	@Override
-	public Map<String, Object> preview(String userName, String profile, String reportName, String whereCondition,
-			String otherParameters, Integer pageIndex)
-			throws JRException, IOException, DocumentException, SQLException {
-		return print(userName, profile, reportName, whereCondition, otherParameters, null, pageIndex, true);
-	}
-
-	/**
-	 * 输出报表的数据
-	 *
-	 * @param userName
-	 * @param profile
-	 * @param reportName
-	 * @param whereCondition
-	 * @param otherParameters
-	 * @param exportType
-	 * @param pageIndex
-     * @param useFileVirtualizer 是否使用文件虚拟化技术
-	 * @return 页码pageSize:int,数据data:byte[]
-	 * @throws JRException
-	 * @throws IOException
-	 * @throws DocumentException
-	 * @throws SQLException
-	 */
-	private Map<String, Object> print(String userName, String profile, String reportName, String whereCondition,
-			String otherParameters, ExportType exportType, Integer pageIndex, boolean useFileVirtualizer)
+	public int export(String userName, String profile, String reportName, String whereCondition,
+			String otherParameters, ExportType exportType, File exportFile, Integer pageIndex, boolean useFileVirtualizer)
 			throws JRException, IOException, DocumentException, SQLException {
 		// TODO 重新实现jasperserver接口
 		// try {
@@ -113,6 +78,10 @@ public class PrintServiceImpl implements PrintService {
 			throw new SQLException("获取数据源失败");
 		}
 
+		if(!exportFile.getParentFile().exists()){
+            exportFile.getParentFile().mkdirs();
+        }
+
 		String masterOfJrxml = getMasterOfJrxml(userName, reportName);
 		String jrxmlFilePath = fileService.getJrxmlFilePath(masterOfJrxml, reportName);
 
@@ -143,68 +112,48 @@ public class PrintServiceImpl implements PrintService {
 				throw new IllegalStateException("编译报表模板失败");
 			}
 
-			Map<String, Object> result = new HashMap<>();
-			byte[] data;
+			logger.info("export fillReport...");
 			// 从数据库获取数据填充报表
-			JasperPrint jasperPrint = null;
-			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-			// exportType导出文件的格式不为空,表示是导出,并非预览
-			if (exportType != null) {
-				logger.info("export fillReport...");
-				boolean customCellStyle = false;
-				// 只导出数据
-				if (exportType == ExportType.XLS_DATA || exportType == ExportType.XLSX_DATA) {
-					// 需自定义单元格格式
-					customCellStyle = true;
-					JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFilePath);
-					// 移除模板中多余元素
-					removeUnusedElements(jasperDesign);
-					JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
-					jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
-				} else if (exportType == ExportType.DOC) {
-					// 导出word时需要对行距等进行调整
-					InputStream inputStream = new ByteArrayInputStream(
-							modifyLineSpacing(jrxmlFilePath).getBytes("UTF-8"));
-					JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
-					jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
-					inputStream.close();
-				} else {
-					jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
-				}
-
-				if (exportType == ExportType.XLS || exportType == ExportType.XLS_DATA) {
-					exportReportToXls(jasperPrint, outputStream, customCellStyle);
-				} else if (exportType == ExportType.XLSX || exportType == ExportType.XLSX_DATA) {
-					exportReportToXlsx(jasperPrint, outputStream, customCellStyle);
-                } else if (exportType == ExportType.DOC) {
-					exportReportToDoc(jasperPrint, outputStream);
-				} else if (exportType == ExportType.TXT) {
-					exportReportToText(jasperPrint, outputStream);
-				} else {
-					exportReportToPdf(jasperPrint, outputStream, pageIndex);
-				}
-				logger.info("export fillReport done...");
-				data = outputStream.toByteArray();
-				outputStream.close();
-			}
-			// 报表预览,则直接输出pdf,并且也许需要分页
-			else {
-				logger.info("preview fillReport...");
+			JasperPrint jasperPrint;
+			boolean customCellStyle = false;
+			// 只导出数据
+			if (exportType == ExportType.XLS_DATA || exportType == ExportType.XLSX_DATA) {
+				// 需自定义单元格格式
+				customCellStyle = true;
+				JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFilePath);
+				// 移除模板中多余元素
+				removeUnusedElements(jasperDesign);
+				JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
+				jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
+			} else if (exportType == ExportType.DOC) {
+				// 导出word时需要对行距等进行调整
+				InputStream inputStream = new ByteArrayInputStream(
+						modifyLineSpacing(jrxmlFilePath).getBytes("UTF-8"));
+				JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
+				jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
+				inputStream.close();
+			} else {
 				jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
-				exportReportToPdf(jasperPrint, outputStream, pageIndex);
-				logger.info("preview fillReport done...");
-				data = outputStream.toByteArray();
-				outputStream.close();
-				result.put("pageSize", jasperPrint.getPages().size());
 			}
-			// TODO 先写入文件,用时再读取,防止数据量太大,内存不足
-			result.put("data", data);
-			return result;
-        } catch (JRRuntimeException e) {
+
+			if (exportType == ExportType.XLS || exportType == ExportType.XLS_DATA) {
+				exportReportToXls(jasperPrint, exportFile, customCellStyle, pageIndex);
+			} else if (exportType == ExportType.XLSX || exportType == ExportType.XLSX_DATA) {
+				exportReportToXlsx(jasperPrint, exportFile, customCellStyle, pageIndex);
+			} else if (exportType == ExportType.DOC) {
+				exportReportToDoc(jasperPrint, exportFile, pageIndex);
+			} else if (exportType == ExportType.TXT) {
+				exportReportToText(jasperPrint, exportFile, pageIndex);
+			} else {
+				exportReportToPdf(jasperPrint, exportFile, pageIndex);
+			}
+			logger.info("export fillReport done...");
+			return jasperPrint.getPages().size();
+		} catch (JRRuntimeException e) {
             // 如果因为某个类未实现 Serializable 而无法反序列化,则不使用 REPORT_VIRTUALIZER
             if (e.getCause() instanceof NotSerializableException) {
                 logger.error(e.getMessage() + " 取消 REPORT_VIRTUALIZER ,重新打印");
-                return print(userName, profile, reportName, whereCondition, otherParameters, exportType, pageIndex, false);
+                return export(userName, profile, reportName, whereCondition, otherParameters, exportType, exportFile, pageIndex, false);
             }
             throw e;
         } finally {
@@ -497,15 +446,17 @@ public class PrintServiceImpl implements PrintService {
 	 * 以xls的格式导出报表
 	 *
 	 * @param jasperPrint
-	 * @param outputStream
+	 * @param outFile
+	 * @param pageIndex
 	 * @throws JRException
 	 */
-	private void exportReportToXls(JasperPrint jasperPrint, OutputStream outputStream, boolean customCellStyle)
+	private void exportReportToXls(JasperPrint jasperPrint, File outFile, boolean customCellStyle, Integer pageIndex)
 			throws JRException {
 		JRXlsExporter exporter = new CustomJRXlsExporter(customCellStyle);
+		setPageConfiguration(exporter, jasperPrint, pageIndex);
 		ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
 		exporter.setExporterInput(exporterInput);
-		OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
+		OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outFile);
 		exporter.setExporterOutput(exporterOutput);
 		exporter.exportReport();
 	}
@@ -514,15 +465,17 @@ public class PrintServiceImpl implements PrintService {
 	 * 以xlsx的格式导出报表
 	 *
 	 * @param jasperPrint
-	 * @param outputStream
+	 * @param outFile
+	 * @param pageIndex
 	 * @throws JRException
 	 */
-	private void exportReportToXlsx(JasperPrint jasperPrint, OutputStream outputStream, boolean customCellStyle)
+	private void exportReportToXlsx(JasperPrint jasperPrint, File outFile, boolean customCellStyle, Integer pageIndex)
 			throws JRException {
 		JRXlsxExporter exporter = new CustomJRXlsxExporter(customCellStyle);
+		setPageConfiguration(exporter, jasperPrint, pageIndex);
 		ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
 		exporter.setExporterInput(exporterInput);
-		OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
+		OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outFile);
 		exporter.setExporterOutput(exporterOutput);
 		exporter.exportReport();
 	}
@@ -531,27 +484,30 @@ public class PrintServiceImpl implements PrintService {
 	 * 以doc的格式导出报表
 	 *
 	 * @param jasperPrint
-	 * @param outputStream
+	 * @param outFile
+	 * @param pageIndex
 	 * @throws JRException
 	 * @throws IOException
 	 */
-	private void exportReportToDoc(JasperPrint jasperPrint, OutputStream outputStream) throws JRException, IOException {
-		exportReportToRtf(jasperPrint, outputStream);
+	private void exportReportToDoc(JasperPrint jasperPrint, File outFile, Integer pageIndex) throws JRException, IOException {
+		exportReportToRtf(jasperPrint, outFile, pageIndex);
 	}
 
 	/**
 	 * 以rtf的格式导出报表
 	 *
 	 * @param jasperPrint
-	 * @param outputStream
+	 * @param outFile
+	 * @param pageIndex
 	 * @throws JRException
 	 * @throws IOException
 	 */
-	private void exportReportToRtf(JasperPrint jasperPrint, OutputStream outputStream) throws JRException, IOException {
+	private void exportReportToRtf(JasperPrint jasperPrint, File outFile, Integer pageIndex) throws JRException, IOException {
 		JRRtfExporter exporter = new JRRtfExporter();
+		setPageConfiguration(exporter, jasperPrint, pageIndex);
 		ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
 		exporter.setExporterInput(exporterInput);
-		WriterExporterOutput exporterOutput = new SimpleWriterExporterOutput(outputStream);
+		WriterExporterOutput exporterOutput = new SimpleWriterExporterOutput(outFile);
 		exporter.setExporterOutput(exporterOutput);
 		exporter.exportReport();
 	}
@@ -560,27 +516,17 @@ public class PrintServiceImpl implements PrintService {
 	 * 以pdf的格式导出报表
 	 *
 	 * @param jasperPrint
-	 * @param outputStream
+	 * @param outFile
 	 * @param pageIndex
 	 * @throws JRException
 	 */
-	private void exportReportToPdf(JasperPrint jasperPrint, OutputStream outputStream, Integer pageIndex)
+	private void exportReportToPdf(JasperPrint jasperPrint, File outFile, Integer pageIndex)
 			throws JRException {
 		JRPdfExporter exporter = new JRPdfExporter();
-		if (pageIndex != null) {
-			// 前端显示时页码从1开始,生成报表时页码从0开始
-			pageIndex -= 1;
-			// 页码并非有效数值,重置为第一页
-			if (pageIndex < 0 || pageIndex >= jasperPrint.getPages().size()) {
-				pageIndex = 0;
-			}
-			SimplePdfReportConfiguration configuration = new SimplePdfReportConfiguration();
-			configuration.setPageIndex(pageIndex);
-			exporter.setConfiguration(configuration);
-		}
+		setPageConfiguration(exporter, jasperPrint, pageIndex);
 		ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
 		exporter.setExporterInput(exporterInput);
-		OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
+		OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outFile);
 		exporter.setExporterOutput(exporterOutput);
 		exporter.exportReport();
 	}
@@ -589,18 +535,41 @@ public class PrintServiceImpl implements PrintService {
 	 * 以text的格式导出报表
 	 *
 	 * @param jasperPrint
-	 * @param outputStream
+	 * @param outFile
+	 * @param pageIndex
 	 * @throws JRException
 	 */
-	private void exportReportToText(JasperPrint jasperPrint, OutputStream outputStream) throws JRException {
+	private void exportReportToText(JasperPrint jasperPrint, File outFile, Integer pageIndex) throws JRException {
 		JRTextExporter exporter = new JRTextExporter();
+		setPageConfiguration(exporter, jasperPrint, pageIndex);
 		ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
 		exporter.setExporterInput(exporterInput);
-		WriterExporterOutput exporterOutput = new SimpleWriterExporterOutput(outputStream);
+		WriterExporterOutput exporterOutput = new SimpleWriterExporterOutput(outFile);
 		exporter.setExporterOutput(exporterOutput);
 		exporter.exportReport();
 	}
 
+	/**
+	 * 设置分页配置
+	 *
+	 * @param exporter
+	 * @param jasperPrint
+	 * @param pageIndex
+	 */
+	private void setPageConfiguration(JRAbstractExporter exporter, JasperPrint jasperPrint, Integer pageIndex) {
+		if (pageIndex != null) {
+			// 前端显示时页码从1开始,生成报表时页码从0开始
+			pageIndex -= 1;
+			// 页码并非有效数值,重置为第一页
+			if (pageIndex < 0 || pageIndex >= jasperPrint.getPages().size()) {
+				pageIndex = 0;
+			}
+			SimpleReportExportConfiguration configuration = new SimpleReportExportConfiguration();
+			configuration.setPageIndex(pageIndex);
+			exporter.setConfiguration(configuration);
+		}
+	}
+
 	/**
 	 * 移除除了Column Header和Detail之外的元素
 	 *