Просмотр исходного кода

导出excel时自定义单元格格式

sunyj 9 лет назад
Родитель
Сommit
808a32a5cc

+ 74 - 0
src/main/java/com/uas/report/jasperreports/engine/export/CustomJRXlsExporter.java

@@ -0,0 +1,74 @@
+package com.uas.report.jasperreports.engine.export;
+
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
+
+import net.sf.jasperreports.engine.export.Cut;
+import net.sf.jasperreports.engine.export.JRXlsExporter;
+import net.sf.jasperreports.engine.export.XlsRowLevelInfo;
+
+/**
+ * 报表导出为Xls
+ * 
+ * @author sunyj
+ * @since 2016年12月14日 下午8:10:56
+ */
+public class CustomJRXlsExporter extends JRXlsExporter {
+
+	/**
+	 * 单元格宽度
+	 */
+	public static final int CELL_WIDTH = 50;
+
+	/**
+	 * 单元格高度
+	 */
+	public static final int CELL_HEIGHT = 12;
+
+	/**
+	 * 单元格边框
+	 */
+	public static final short CELL_BORDER = 1;
+
+	/**
+	 * 是否自定义单元格格式
+	 */
+	private boolean customCellStyle = false;
+
+	public CustomJRXlsExporter(boolean ifCustomCellStyle) {
+		this.customCellStyle = ifCustomCellStyle;
+	}
+
+	@Override
+	protected void setColumnWidth(int col, int width, boolean autoFit) {
+		if (customCellStyle) {
+			width = CELL_WIDTH;
+		}
+		super.setColumnWidth(col, width, autoFit);
+	}
+
+	@Override
+	protected void setRowHeight(int rowIndex, int lastRowHeight, Cut yCut, XlsRowLevelInfo levelInfo) {
+		if (customCellStyle) {
+			lastRowHeight = CELL_HEIGHT;
+		}
+		super.setRowHeight(rowIndex, lastRowHeight, yCut, levelInfo);
+	}
+
+	@Override
+	protected HSSFCellStyle getLoadedCellStyle(StyleInfo style) {
+		// if(style!=null){
+		// Field boxField=style.getClass().getField("box");
+		// boxField.setAccessible(true);
+		// BoxStyle box = new JRXlsExporter.StyleInfo.
+		// boxField.set(style, value);
+		// }
+		HSSFCellStyle cellStyle = super.getLoadedCellStyle(style);
+		if (customCellStyle) {
+			cellStyle.setBorderTop(CELL_BORDER);
+			cellStyle.setBorderLeft(CELL_BORDER);
+			cellStyle.setBorderBottom(CELL_BORDER);
+			cellStyle.setBorderRight(CELL_BORDER);
+		}
+		return cellStyle;
+	}
+}

+ 36 - 16
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -24,13 +24,19 @@ import org.springframework.util.StringUtils;
 
 import com.alibaba.fastjson.JSONObject;
 import com.uas.report.core.exception.ReportException;
+import com.uas.report.jasperreports.engine.export.CustomJRXlsExporter;
 import com.uas.report.service.FileService;
 import com.uas.report.service.PrintService;
 import com.uas.report.util.DataSourceUtils;
 import com.uas.report.util.FileUtils;
 import com.uas.report.util.ReportConstants;
 
+import net.sf.jasperreports.engine.JRBand;
+import net.sf.jasperreports.engine.JRElement;
 import net.sf.jasperreports.engine.JRException;
+import net.sf.jasperreports.engine.JRExporterParameter;
+import net.sf.jasperreports.engine.JRSection;
+import net.sf.jasperreports.engine.JRTextField;
 import net.sf.jasperreports.engine.JasperCompileManager;
 import net.sf.jasperreports.engine.JasperExportManager;
 import net.sf.jasperreports.engine.JasperFillManager;
@@ -39,12 +45,15 @@ 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;
+import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
 import net.sf.jasperreports.engine.xml.JRXmlLoader;
 import net.sf.jasperreports.export.ExporterInput;
 import net.sf.jasperreports.export.OutputStreamExporterOutput;
 import net.sf.jasperreports.export.SimpleExporterInput;
 import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
 import net.sf.jasperreports.export.SimplePdfReportConfiguration;
+import net.sf.jasperreports.export.SimpleXlsExporterConfiguration;
+import net.sf.jasperreports.export.XlsExporterConfiguration;
 
 /**
  * 报表打印
@@ -138,8 +147,11 @@ public class PrintServiceImpl implements PrintService {
 			// exportFileType导出文件的格式不为空,表示是导出,并非预览
 			if (!StringUtils.isEmpty(exportFileType)) {
 				logger.info("export fillReport...");
+				boolean customCellStyle = false;
 				// 只导出数据
 				if (exportFileType.equals("xls_with_only_data")) {
+					// 需自定义单元格格式
+					customCellStyle = true;
 					JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFilePath);
 					// 移除模板中多余元素
 					removeUnusedElements(jasperDesign);
@@ -151,7 +163,7 @@ public class PrintServiceImpl implements PrintService {
 					jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
 				}
 
-				exportReport(jasperPrint, exportFileType, outputStream);
+				exportReportToXls(jasperPrint, outputStream, customCellStyle);
 				logger.info("export fillReport done...");
 				data = outputStream.toByteArray();
 				outputStream.close();
@@ -285,19 +297,20 @@ public class PrintServiceImpl implements PrintService {
 	 * @param exportFileType
 	 * @param outputStream
 	 */
-	private void exportReport(JasperPrint jasperPrint, String exportFileType, OutputStream outputStream) {
-		try {
-			if (exportFileType.equals("pdf")) {
-				exportReportToPdf(jasperPrint, outputStream);
-			} else if (exportFileType.equals("xls")) {
-				exportReportToXls(jasperPrint, outputStream);
-			} else {
-				throw new ReportException("不支持导出为 " + exportFileType + "格式!");
-			}
-		} catch (JRException e) {
-			throw new ReportException(e).setDetailedMessage(e);
-		}
-	}
+	// private void exportReport(JasperPrint jasperPrint, String exportFileType,
+	// OutputStream outputStream) {
+	// try {
+	// if (exportFileType.equals("pdf")) {
+	// exportReportToPdf(jasperPrint, outputStream);
+	// } else if (exportFileType.equals("xls")) {
+	// exportReportToXls(jasperPrint, outputStream);
+	// } else {
+	// throw new ReportException("不支持导出为 " + exportFileType + "格式!");
+	// }
+	// } catch (JRException e) {
+	// throw new ReportException(e).setDetailedMessage(e);
+	// }
+	// }
 
 	/**
 	 * 以pdf的格式导出报表
@@ -317,8 +330,15 @@ public class PrintServiceImpl implements PrintService {
 	 * @param outputStream
 	 * @throws JRException
 	 */
-	private void exportReportToXls(JasperPrint jasperPrint, OutputStream outputStream) throws JRException {
-		JRXlsExporter exporter = new JRXlsExporter();
+	private void exportReportToXls(JasperPrint jasperPrint, OutputStream outputStream, boolean customCellStyle)
+			throws JRException {
+		JRXlsExporter exporter = new CustomJRXlsExporter(customCellStyle);
+		// SimpleXlsExporterConfiguration configuration = new
+		// SimpleXlsExporterConfiguration();
+		// configuration.setCreateCustomPalette(true);
+		// configuration.setKeepWorkbookTemplateSheets(true);
+		// configuration.setOverrideHints(true);
+		// exporter.setConfiguration(configuration);
 		ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
 		exporter.setExporterInput(exporterInput);
 		OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);