| | |
| | | import org.apache.poi.ss.usermodel.Cell;
|
| | | import org.apache.poi.ss.usermodel.CellStyle;
|
| | | import org.apache.poi.ss.usermodel.CellType;
|
| | | import org.apache.poi.ss.usermodel.ClientAnchor;
|
| | | import org.apache.poi.ss.usermodel.DataValidation;
|
| | | import org.apache.poi.ss.usermodel.DataValidationConstraint;
|
| | | import org.apache.poi.ss.usermodel.DataValidationHelper;
|
| | | import org.apache.poi.ss.usermodel.DateUtil;
|
| | | import org.apache.poi.ss.usermodel.Drawing;
|
| | | import org.apache.poi.ss.usermodel.FillPatternType;
|
| | | import org.apache.poi.ss.usermodel.Font;
|
| | | import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
| | |
| | | import org.apache.poi.ss.usermodel.WorkbookFactory;
|
| | | import org.apache.poi.ss.util.CellRangeAddressList;
|
| | | import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
| | | import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
|
| | | import org.apache.poi.xssf.usermodel.XSSFDataValidation;
|
| | | import org.slf4j.Logger;
|
| | | import org.slf4j.LoggerFactory;
|
| | |
| | | import com.ruoyi.common.utils.DateUtils;
|
| | | import com.ruoyi.common.utils.DictUtils;
|
| | | import com.ruoyi.common.utils.StringUtils;
|
| | | import com.ruoyi.common.utils.file.FileTypeUtils;
|
| | | import com.ruoyi.common.utils.file.ImageUtils;
|
| | | import com.ruoyi.common.utils.reflect.ReflectUtils;
|
| | | import com.ruoyi.framework.aspectj.lang.annotation.Excel;
|
| | | import com.ruoyi.framework.aspectj.lang.annotation.Excel.ColumnType;
|
| | |
| | | private List<Object[]> fields;
|
| | |
|
| | | /**
|
| | | * æå¤§é«åº¦
|
| | | */
|
| | | private short maxHeight;
|
| | |
|
| | | /**
|
| | | * ç»è®¡å表
|
| | | */
|
| | | private Map<Integer, Double> statistics = new HashMap<Integer, Double>();
|
| | |
|
| | | |
| | | /**
|
| | | * æ°åæ ¼å¼
|
| | | */
|
| | | private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00");
|
| | |
|
| | | |
| | | /**
|
| | | * å®ä½å¯¹è±¡
|
| | | */
|
| | |
| | | }
|
| | | else
|
| | | {
|
| | | val = Convert.toStr(val);
|
| | | String dateFormat = field.getAnnotation(Excel.class).dateFormat();
|
| | | if (StringUtils.isNotEmpty(dateFormat))
|
| | | {
|
| | | val = DateUtils.parseDateToStr(dateFormat, (Date) val);
|
| | | }
|
| | | else
|
| | | {
|
| | | val = Convert.toStr(val);
|
| | | }
|
| | | }
|
| | | }
|
| | | else if ((Integer.TYPE == fieldType || Integer.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val)))
|
| | |
| | | {
|
| | | cell.setCellValue(StringUtils.contains(Convert.toStr(value), ".") ? Convert.toDouble(value) : Convert.toInt(value));
|
| | | }
|
| | | else if (ColumnType.IMAGE == attr.cellType())
|
| | | {
|
| | | ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1),
|
| | | cell.getRow().getRowNum() + 1);
|
| | | String imagePath = Convert.toStr(value);
|
| | | if (StringUtils.isNotEmpty(imagePath))
|
| | | {
|
| | | byte[] data = ImageUtils.getImage(imagePath);
|
| | | getDrawingPatriarch(cell.getSheet()).createPicture(anchor,
|
| | | cell.getSheet().getWorkbook().addPicture(data, getImageType(data)));
|
| | | }
|
| | | }
|
| | | }
|
| | | |
| | | /**
|
| | | * è·åç»å¸
|
| | | */
|
| | | public static Drawing<?> getDrawingPatriarch(Sheet sheet)
|
| | | {
|
| | | if (sheet.getDrawingPatriarch() == null)
|
| | | {
|
| | | sheet.createDrawingPatriarch();
|
| | | }
|
| | | return sheet.getDrawingPatriarch();
|
| | | }
|
| | |
|
| | | /**
|
| | | * è·åå¾çç±»å,设置å¾çæå
¥ç±»å
|
| | | */
|
| | | public int getImageType(byte[] value)
|
| | | {
|
| | | String type = FileTypeUtils.getFileExtendName(value);
|
| | | if ("JPG".equalsIgnoreCase(type))
|
| | | {
|
| | | return Workbook.PICTURE_TYPE_JPEG;
|
| | | }
|
| | | else if ("PNG".equalsIgnoreCase(type))
|
| | | {
|
| | | return Workbook.PICTURE_TYPE_PNG;
|
| | | }
|
| | | return Workbook.PICTURE_TYPE_JPEG;
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | {
|
| | | // 设置å宽
|
| | | sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
|
| | | row.setHeight((short) (attr.height() * 20));
|
| | | }
|
| | | // å¦æè®¾ç½®äºæç¤ºä¿¡æ¯åé¼ æ æ¾ä¸å»æç¤º.
|
| | | if (StringUtils.isNotEmpty(attr.prompt()))
|
| | |
| | | try
|
| | | {
|
| | | // 设置è¡é«
|
| | | row.setHeight((short) (attr.height() * 20));
|
| | | row.setHeight(maxHeight);
|
| | | // æ ¹æ®Excelä¸è®¾ç½®æ
åµå³å®æ¯å¦å¯¼åº,æäºæ
åµéè¦ä¿æä¸ºç©º,å¸æç¨æ·å¡«åè¿ä¸å.
|
| | | if (attr.isExport())
|
| | | {
|
| | |
| | | }
|
| | | return StringUtils.stripEnd(propertyString.toString(), separator);
|
| | | }
|
| | |
|
| | | |
| | | /**
|
| | | * è§£æåå
¸å¼
|
| | | *
|
| | |
| | | {
|
| | | return DictUtils.getDictValue(dictType, dictLabel, separator);
|
| | | }
|
| | |
|
| | | |
| | | /**
|
| | | * å计ç»è®¡ä¿¡æ¯
|
| | | */
|
| | |
| | | cell = row.createCell(0);
|
| | | cell.setCellStyle(styles.get("total"));
|
| | | cell.setCellValue("å计");
|
| | |
|
| | | |
| | | for (Integer key : keys)
|
| | | {
|
| | | cell = row.createCell(key);
|
| | |
| | | }
|
| | | }
|
| | | this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
|
| | | this.maxHeight = getRowHeight();
|
| | | }
|
| | | |
| | | /**
|
| | | * æ ¹æ®æ³¨è§£è·åæå¤§è¡é«
|
| | | */
|
| | | public short getRowHeight()
|
| | | {
|
| | | double maxHeight = 0;
|
| | | for (Object[] os : this.fields)
|
| | | {
|
| | | Excel excel = (Excel) os[1];
|
| | | maxHeight = maxHeight > excel.height() ? maxHeight : excel.height();
|
| | | }
|
| | | return (short) (maxHeight * 20);
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | }
|
| | | return val;
|
| | | }
|
| | | }
|
| | | } |