|
|
@@ -0,0 +1,220 @@
|
|
|
+package com.uas.report.jasperreports.engine.export;
|
|
|
+
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+
|
|
|
+import org.apache.poi.ss.usermodel.IndexedColors;
|
|
|
+
|
|
|
+import com.uas.report.util.ObjectUtils;
|
|
|
+import com.uas.report.util.StringUtils;
|
|
|
+
|
|
|
+import net.sf.jasperreports.engine.JRException;
|
|
|
+import net.sf.jasperreports.engine.JRPrintText;
|
|
|
+import net.sf.jasperreports.engine.export.Cut;
|
|
|
+import net.sf.jasperreports.engine.export.JRExporterGridCell;
|
|
|
+import net.sf.jasperreports.engine.export.XlsRowLevelInfo;
|
|
|
+import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
|
|
|
+import net.sf.jasperreports.engine.export.ooxml.XlsxBorderHelper;
|
|
|
+import net.sf.jasperreports.engine.export.ooxml.XlsxStyleHelper;
|
|
|
+import net.sf.jasperreports.engine.util.FileBufferedWriter;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 报表导出为Xls
|
|
|
+ *
|
|
|
+ * @author sunyj
|
|
|
+ * @since 2016年12月14日 下午8:10:56
|
|
|
+ */
|
|
|
+public class CustomJRXlsxExporter extends JRXlsxExporter {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 单元格宽度
|
|
|
+ */
|
|
|
+ public static final int CELL_WIDTH = 50;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 单元格高度
|
|
|
+ */
|
|
|
+ public static final int CELL_HEIGHT = 12;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 单元格边框宽度
|
|
|
+ */
|
|
|
+ public static final short CELL_BORDER_WIDTH = 1;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 单元格边框颜色
|
|
|
+ */
|
|
|
+ public static final short CELL_BORDER_CORLOR = IndexedColors.GREY_25_PERCENT.index;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否自定义单元格格式
|
|
|
+ */
|
|
|
+ private boolean customCellStyle = false;
|
|
|
+
|
|
|
+ public CustomJRXlsxExporter(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 rowHeight, Cut yCut, XlsRowLevelInfo levelInfo) throws JRException {
|
|
|
+ if (customCellStyle) {
|
|
|
+ rowHeight = CELL_HEIGHT;
|
|
|
+ }
|
|
|
+ super.setRowHeight(rowIndex, rowHeight, yCut, levelInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ // @Override
|
|
|
+ // protected HSSFCellStyle getLoadedCellStyle(StyleInfo style) {
|
|
|
+ // HSSFCellStyle cellStyle = super.getLoadedCellStyle(style);
|
|
|
+ // if (customCellStyle) {
|
|
|
+ // cellStyle.setBorderTop(CELL_BORDER_WIDTH);
|
|
|
+ // cellStyle.setBorderLeft(CELL_BORDER_WIDTH);
|
|
|
+ // cellStyle.setBorderBottom(CELL_BORDER_WIDTH);
|
|
|
+ // cellStyle.setBorderRight(CELL_BORDER_WIDTH);
|
|
|
+ // cellStyle.setTopBorderColor(CELL_BORDER_CORLOR);
|
|
|
+ // cellStyle.setLeftBorderColor(CELL_BORDER_CORLOR);
|
|
|
+ // cellStyle.setBottomBorderColor(CELL_BORDER_CORLOR);
|
|
|
+ // cellStyle.setRightBorderColor(CELL_BORDER_CORLOR);
|
|
|
+ // }
|
|
|
+ // return cellStyle;
|
|
|
+ // }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void exportText(JRPrintText text, JRExporterGridCell gridCell, int colIndex, int rowIndex)
|
|
|
+ throws JRException {
|
|
|
+ // GridCellStyle style = gridCell.getStyle();
|
|
|
+ // JRLineBox box = style.getBox();
|
|
|
+ // System.out.println(box.getPen().getLineWidth()+" "
|
|
|
+ // +box.getPen().getOwnLineWidth()+" " +box.getPen().getLineColor()+"
|
|
|
+ // "+box.getPen().getOwnLineColor()+"
|
|
|
+ // "+box.getPen().getLineStyleValue()+"
|
|
|
+ // "+box.getPen().getOwnLineStyleValue());
|
|
|
+ // System.out.println(box.getTopPen().getLineWidth()+" "
|
|
|
+ // +box.getTopPen().getOwnLineWidth()+" "
|
|
|
+ // +box.getTopPen().getLineColor()+"
|
|
|
+ // "+box.getTopPen().getOwnLineColor()+"
|
|
|
+ // "+box.getTopPen().getLineStyleValue()+"
|
|
|
+ // "+box.getTopPen().getOwnLineStyleValue());
|
|
|
+ // System.out.println(box.getBottomPen().getLineWidth()+" "
|
|
|
+ // +box.getBottomPen().getOwnLineWidth()+" "
|
|
|
+ // +box.getBottomPen().getLineColor()+"
|
|
|
+ // "+box.getBottomPen().getOwnLineColor()+"
|
|
|
+ // "+box.getBottomPen().getLineStyleValue()+"
|
|
|
+ // "+box.getBottomPen().getOwnLineStyleValue());
|
|
|
+ // System.out.println(box.getLeftPen().getLineWidth()+" "
|
|
|
+ // +box.getLeftPen().getOwnLineWidth()+" "
|
|
|
+ // +box.getLeftPen().getLineColor()+"
|
|
|
+ // "+box.getLeftPen().getOwnLineColor()+"
|
|
|
+ // "+box.getLeftPen().getLineStyleValue()+"
|
|
|
+ // "+box.getLeftPen().getOwnLineStyleValue());
|
|
|
+ // System.out.println(box.getRightPen().getLineWidth()+" "
|
|
|
+ // +box.getRightPen().getOwnLineWidth()+" "
|
|
|
+ // +box.getRightPen().getLineColor()+"
|
|
|
+ // "+box.getRightPen().getOwnLineColor()+"
|
|
|
+ // "+box.getRightPen().getLineStyleValue()+"
|
|
|
+ // "+box.getRightPen().getOwnLineStyleValue());
|
|
|
+ // JRBoxPen pen = box.getPen();
|
|
|
+ // JRBoxPen topPen = box.getTopPen();
|
|
|
+ // JRBoxPen rightPen = box.getRightPen();
|
|
|
+ // JRBoxPen bottomPen = box.getBottomPen();
|
|
|
+ // JRBoxPen leftPen = box.getLeftPen();
|
|
|
+ //
|
|
|
+ // pen.setLineWidth(1);
|
|
|
+ // topPen.setLineWidth(1);
|
|
|
+ // rightPen.setLineWidth(1);
|
|
|
+ // bottomPen.setLineWidth(1);
|
|
|
+ // leftPen.setLineWidth(1);
|
|
|
+ //
|
|
|
+ // box.copyPen(pen);
|
|
|
+ // box.copyTopPen(topPen);
|
|
|
+ // box.copyRightPen(rightPen);
|
|
|
+ // box.copyBottomPen(bottomPen);
|
|
|
+ // box.copyLeftPen(leftPen);
|
|
|
+ super.exportText(text, gridCell, colIndex, rowIndex);
|
|
|
+ }
|
|
|
+
|
|
|
+ // @Override
|
|
|
+ // protected void createTextCell(JRPrintText textElement, JRExporterGridCell
|
|
|
+ // gridCell, int colIndex, int rowIndex,
|
|
|
+ // JRStyledText styledText, StyleInfo baseStyle, short forecolor) throws
|
|
|
+ // JRException {
|
|
|
+ // if (customCellStyle) {
|
|
|
+ // String textStr = styledText.getText();
|
|
|
+ // TextValue textValue = getTextValue(textElement, textStr);
|
|
|
+ // // 类型为String,但是实际值为数值,需设置为字符串类型,否则会提示转为数值型
|
|
|
+ // if ((textValue instanceof StringTextValue && isNumber(textStr)) ||
|
|
|
+ // textValue instanceof NumberTextValue) {
|
|
|
+ // baseStyle.setDataFormat(dataFormat.getFormat("@"));
|
|
|
+ // }
|
|
|
+ // super.createTextCell(textElement, gridCell, colIndex, rowIndex,
|
|
|
+ // styledText, baseStyle, forecolor);
|
|
|
+ //
|
|
|
+ // // 数值类型需明确指定,否则会提示转为数值型
|
|
|
+ // if (textValue instanceof NumberTextValue) {
|
|
|
+ // setNumberTextValue(textStr);
|
|
|
+ // }
|
|
|
+ // } else {
|
|
|
+ // super.createTextCell(textElement, gridCell, colIndex, rowIndex,
|
|
|
+ // styledText, baseStyle, forecolor);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断字符串是否为数值
|
|
|
+ *
|
|
|
+ * @param textStr
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private boolean isNumber(String textStr) {
|
|
|
+ if (StringUtils.isEmpty(textStr)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 数值可能为1,320.6的形式
|
|
|
+ Double.parseDouble(textStr.replaceAll(",", ""));
|
|
|
+ return true;
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置单元格格式为数值类型
|
|
|
+ *
|
|
|
+ * @param textStr
|
|
|
+ */
|
|
|
+ // private void setNumberTextValue(String textStr) {
|
|
|
+ // if (isNumber(textStr)) {
|
|
|
+ // cell.setCellValue(Double.parseDouble(textStr.replaceAll(",", "")));
|
|
|
+ // cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void openWorkbook(OutputStream os) throws JRException {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+ super.openWorkbook(os);
|
|
|
+ // styleHelper=new CustomXlsxStyleHelper(jasperReportsContext,
|
|
|
+ // xlsxZip.getStylesEntry().getWriter(),
|
|
|
+ // getExporterKey());
|
|
|
+ FileBufferedWriter bordersWriter = (FileBufferedWriter) ObjectUtils.recursivelyGetValue("bordersWriter",
|
|
|
+ styleHelper);
|
|
|
+ XlsxBorderHelper borderHelper = new CustomXlsxBorderHelper(jasperReportsContext, bordersWriter);
|
|
|
+ Field borderHelperField = ObjectUtils.recursivelyGetField("borderHelper", XlsxStyleHelper.class);
|
|
|
+ borderHelperField.setAccessible(true);
|
|
|
+ try {
|
|
|
+ borderHelperField.set(styleHelper, borderHelper);
|
|
|
+ } catch (Throwable e) {
|
|
|
+ throw new IllegalArgumentException("修改 XlsxStyleHelper 失败", e);
|
|
|
+ }
|
|
|
+ borderHelperField.setAccessible(false);
|
|
|
+ }
|
|
|
+}
|