|
|
@@ -1,11 +1,20 @@
|
|
|
package com.uas.report.jasperreports.engine.export;
|
|
|
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
|
|
import org.apache.poi.ss.usermodel.IndexedColors;
|
|
|
+import org.springframework.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.JRXlsExporter;
|
|
|
import net.sf.jasperreports.engine.export.XlsRowLevelInfo;
|
|
|
+import net.sf.jasperreports.engine.export.data.NumberTextValue;
|
|
|
+import net.sf.jasperreports.engine.export.data.StringTextValue;
|
|
|
+import net.sf.jasperreports.engine.export.data.TextValue;
|
|
|
+import net.sf.jasperreports.engine.util.JRStyledText;
|
|
|
|
|
|
/**
|
|
|
* 报表导出为Xls
|
|
|
@@ -75,4 +84,59 @@ public class CustomJRXlsExporter extends JRXlsExporter {
|
|
|
}
|
|
|
return cellStyle;
|
|
|
}
|
|
|
+
|
|
|
+ @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) {
|
|
|
+ if (isNumber(textStr)) {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|