| | |
| | | import java.io.InputStream;
|
| | | import java.io.OutputStream;
|
| | | import java.lang.reflect.Field;
|
| | | import java.lang.reflect.Method;
|
| | | import java.math.BigDecimal;
|
| | | import java.text.DecimalFormat;
|
| | | import java.util.ArrayList;
|
| | |
| | | import org.slf4j.Logger;
|
| | | import org.slf4j.LoggerFactory;
|
| | | import com.ruoyi.common.core.text.Convert;
|
| | | import com.ruoyi.common.exception.CustomException;
|
| | | import com.ruoyi.common.exception.UtilException;
|
| | | import com.ruoyi.common.utils.DateUtils;
|
| | | import com.ruoyi.common.utils.DictUtils;
|
| | | import com.ruoyi.common.utils.StringUtils;
|
| | |
| | | Map<String, PictureData> pictures;
|
| | | if (isXSSFWorkbook)
|
| | | {
|
| | | pictures = getSheetPictrues07((XSSFSheet) sheet, (XSSFWorkbook) wb);
|
| | | pictures = getSheetPictures07((XSSFSheet) sheet, (XSSFWorkbook) wb);
|
| | | }
|
| | | else
|
| | | {
|
| | | pictures = getSheetPictrues03((HSSFSheet) sheet, (HSSFWorkbook) wb);
|
| | | pictures = getSheetPictures03((HSSFSheet) sheet, (HSSFWorkbook) wb);
|
| | | }
|
| | | // 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
|
| | | int rows = sheet.getLastRowNum();
|
| | |
| | | {
|
| | | val = reverseDictByExp(Convert.toStr(val), attr.dictType(), attr.separator());
|
| | | }
|
| | | else if (!attr.handler().equals(ExcelHandlerAdapter.class))
|
| | | {
|
| | | val = dataFormatHandlerAdapter(val, attr);
|
| | | }
|
| | | else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures))
|
| | | {
|
| | | PictureData image = pictures.get(row.getRowNum() + "_" + entry.getKey());
|
| | |
| | | {
|
| | | val = "";
|
| | | }
|
| | | byte[] data = image.getData();
|
| | | val = FileUtils.writeImportBytes(data);
|
| | | else
|
| | | {
|
| | | byte[] data = image.getData();
|
| | | val = FileUtils.writeImportBytes(data);
|
| | | }
|
| | | }
|
| | | ReflectUtils.invokeSetter(entity, propertyName, val);
|
| | | }
|
| | |
| | | catch (Exception e)
|
| | | {
|
| | | log.error("导出Excel异常{}", e.getMessage());
|
| | | throw new CustomException("导出Excel失败,请联系网站管理员!");
|
| | | throw new UtilException("导出Excel失败,请联系网站管理员!");
|
| | | }
|
| | | finally
|
| | | {
|
| | |
| | | {
|
| | | cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).toString());
|
| | | }
|
| | | else if (!attr.handler().equals(ExcelHandlerAdapter.class))
|
| | | {
|
| | | cell.setCellValue(dataFormatHandlerAdapter(value, attr));
|
| | | }
|
| | | else
|
| | | {
|
| | | // 设置列类型
|
| | |
| | | public static String reverseDictByExp(String dictLabel, String dictType, String separator)
|
| | | {
|
| | | return DictUtils.getDictValue(dictType, dictLabel, separator);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 数据处理器
|
| | | * |
| | | * @param value 数据值
|
| | | * @param excel 数据注解
|
| | | * @return
|
| | | */
|
| | | public String dataFormatHandlerAdapter(Object value, Excel excel)
|
| | | {
|
| | | try
|
| | | {
|
| | | Object instance = excel.handler().newInstance();
|
| | | Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class });
|
| | | value = formatMethod.invoke(instance, value, excel.args());
|
| | | }
|
| | | catch (Exception e)
|
| | | {
|
| | | log.error("不能格式化数据 " + excel.handler(), e.getMessage());
|
| | | }
|
| | | return Convert.toStr(value);
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | * @param workbook 工作簿对象
|
| | | * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
|
| | | */
|
| | | public static Map<String, PictureData> getSheetPictrues03(HSSFSheet sheet, HSSFWorkbook workbook)
|
| | | public static Map<String, PictureData> getSheetPictures03(HSSFSheet sheet, HSSFWorkbook workbook)
|
| | | {
|
| | | Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
|
| | | List<HSSFPictureData> pictures = workbook.getAllPictures();
|
| | |
| | | * @param workbook 工作簿对象
|
| | | * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
|
| | | */
|
| | | public static Map<String, PictureData> getSheetPictrues07(XSSFSheet sheet, XSSFWorkbook workbook)
|
| | | public static Map<String, PictureData> getSheetPictures07(XSSFSheet sheet, XSSFWorkbook workbook)
|
| | | {
|
| | | Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
|
| | | for (POIXMLDocumentPart dr : sheet.getRelations())
|