Java带图片的excel数据导入
生活随笔
收集整理的這篇文章主要介紹了
Java带图片的excel数据导入
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
帶圖片的EXCEL數(shù)據(jù)導(dǎo)入
這里使用的是POI,所以這里使用的很雜,不過方便對它們的了解。模板下載與圖片導(dǎo)出到excel都不一樣。
下面會把對應(yīng)連接貼上。
帶入依賴:不要使用3.17的版本,直接上代碼:
接口:
@PostMapping("/import")public CommonResult importRoadsAndBridgesData(@RequestParam("file") MultipartFile[] files){MultipartFile file = files[0];String fileName = file.getOriginalFilename();// 上傳文件為空if (StringUtils.isEmpty(fileName)) {throw new ServiceException("沒有導(dǎo)入文件");}// 上傳文件名格式不正確if (fileName.lastIndexOf(".") != -1 && !".xlsx".equals(fileName.substring(fileName.lastIndexOf(".")))) {throw new ServiceException( "文件名格式不正確, 請使用后綴名為.xlsx的文件");}urbanManageDataService.importRoadsAndBridgesData(file);return CommonResult.success();} @Overridepublic void importRoadsAndBridgesData(MultipartFile file) {ExcelUtil<DetailsOfRoadMaintainDto> util = new ExcelUtil<>(DetailsOfRoadMaintainDto.class);List<DetailsOfRoadMaintainDto> detailsOfRoadDtos = null;try {//文件,sheet名稱,標(biāo)題占用行數(shù)detailsOfRoadDtos = util.importExcel(file, "附表1維護(hù)情況明細(xì)表", 3);} catch (Exception e) {log.error("導(dǎo)入數(shù)據(jù)解析流失敗:{}", e.getMessage());throw new ServiceException("導(dǎo)入數(shù)據(jù)文件解析失敗!");}//封裝實(shí)體數(shù)據(jù)和文件上傳List<DetailsOfRoadMaintainEntity> list = new ArrayList<>();//道路數(shù)據(jù)Map<String, DetailsOfRoadMaintainDto> dtoMap = dataConversion(detailsOfRoadDtos, 1, list);//數(shù)據(jù)保存,替換成你自己的if(!CollectionUtils.isEmpty(list)){detailsOfRoadMaintainMapper.insertList(list);}}//下面都是圖片上傳和數(shù)據(jù)存取,就不過多貼上了,主要看excel圖片的獲取方式 private Map<String, DetailsOfRoadMaintainDto> dataConversion(List<DetailsOfRoadMaintainDto> dtos, Integer type, List<DetailsOfRoadMaintainEntity> list){Map<String, DetailsOfRoadMaintainDto> dtoMap = new HashMap<>();if(!CollectionUtils.isEmpty(dtos)){dtos.forEach(dto ->{DetailsOfRoadMaintainEntity entity = new DetailsOfRoadMaintainEntity();//基礎(chǔ)數(shù)據(jù)轉(zhuǎn)換BeanUtils.copyProperties(dto, entity);entity.setId(IdUtil.simpleUUID());entity.setMaintainType(type);//上傳的圖片信息entity.setBeforeimg(uploadFile(dto.getBeforeimg()));entity.setCreateTime(new Date());//存入entitylist.add(entity);});}return dtoMap;} //這里就是已經(jīng)獲取到圖片后的正常上傳而已,獲取到的圖片信息為PictureData,轉(zhuǎn)為MultipartFile進(jìn)行的文件上傳 private String uploadFile(PictureData pictureData){if(null != pictureData && pictureData.getData().length > 0){InputStream inputStream = new ByteArrayInputStream(pictureData.getData());SysFile sysFile = null;try {MultipartFile multipartFile = new MockMultipartFile(getFileName(pictureData), null, ContentType.IMAGE_JPEG.toString(), inputStream);sysFile = attachmentService.getUploadFileInfo(multipartFile);} catch (IOException e) {log.error("字節(jié)流轉(zhuǎn)文件失敗:{}", e.getMessage());throw new ServiceException("字節(jié)流轉(zhuǎn)文件失敗!");}if(Objects.isNull(sysFile) || StringUtils.isEmpty(sysFile.getUrl())){throw new ServiceException("未獲取到上傳文件的信息!");}return sysFile.getUrl();}return null;}上面都是一些圖片上傳和下面要獲取到后的信息進(jìn)行存儲,主要看圖片怎么從excel獲取的,下面先看參數(shù)映射:
@Data public class DetailsOfRoadMaintainDto implements Serializable {/*** 序號*/@Excel(name = "序號")private String no;@Excel(name = "維護(hù)前照片", getPicture = true)private PictureData beforeimg; public interface ExcelHandlerAdapter {/*** 格式化* * @param value 單元格數(shù)據(jù)值* @param args excel注解args參數(shù)組** @return 處理后的值*/Object format(Object value, String[] args); } @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Excels {Excel[] value(); } package com.goktech.common.core.annotation;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.math.BigDecimal; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors;/*** 自定義導(dǎo)出Excel數(shù)據(jù)注解*/ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Excel {/*** 導(dǎo)出時在excel中排序*/public int sort() default Integer.MAX_VALUE;/*** 導(dǎo)出到Excel中的名字.*/public String name() default "";/*** 日期格式, 如: yyyy-MM-dd*/public String dateFormat() default "";/*** 讀取內(nèi)容轉(zhuǎn)表達(dá)式 (如: 0=男,1=女,2=未知)*/public String readConverterExp() default "";/*** 讀取圖片 true=是,false=否)*/public boolean getPicture() default false;/*** 分隔符,讀取字符串組內(nèi)容*/public String separator() default ",";/*** BigDecimal 精度 默認(rèn):-1(默認(rèn)不開啟BigDecimal格式化)*/public int scale() default -1;/*** BigDecimal 舍入規(guī)則 默認(rèn):BigDecimal.ROUND_HALF_EVEN*/public int roundingMode() default BigDecimal.ROUND_HALF_EVEN;/*** 導(dǎo)出時在excel中每個列的高度 單位為字符*/public double height() default 14;/*** 導(dǎo)出時在excel中每個列的寬 單位為字符*/public double width() default 16;/*** 文字后綴,如% 90 變成90%*/public String suffix() default "";/*** 當(dāng)值為空時,字段的默認(rèn)值*/public String defaultValue() default "";/*** 提示信息*/public String prompt() default "";/*** 設(shè)置只能選擇不能輸入的列內(nèi)容.*/public String[] combo() default {};/*** 是否導(dǎo)出數(shù)據(jù),應(yīng)對需求:有時我們需要導(dǎo)出一份模板,這是標(biāo)題需要但內(nèi)容需要用戶手工填寫.*/public boolean isExport() default true;/*** 另一個類中的屬性名稱,支持多級獲取,以小數(shù)點(diǎn)隔開*/public String targetAttr() default "";/*** 是否自動統(tǒng)計數(shù)據(jù),在最后追加一行統(tǒng)計數(shù)據(jù)總和*/public boolean isStatistics() default false;/*** 導(dǎo)出類型(0數(shù)字 1字符串)*/public ColumnType cellType() default ColumnType.STRING;/*** 導(dǎo)出字體顏色*/public IndexedColors color() default IndexedColors.BLACK;/*** 導(dǎo)出字段對齊方式*/public HorizontalAlignment align() default HorizontalAlignment.CENTER;/*** 自定義數(shù)據(jù)處理器*/public Class<?> handler() default ExcelHandlerAdapter.class;/*** 自定義數(shù)據(jù)處理器參數(shù)*/public String[] args() default {};/*** 字段類型(0:導(dǎo)出導(dǎo)入;1:僅導(dǎo)出;2:僅導(dǎo)入)*/Type type() default Type.ALL;public enum Type{ALL(0), EXPORT(1), IMPORT(2);private final int value;Type(int value){this.value = value;}public int value(){return this.value;}}public enum ColumnType{NUMERIC(0), STRING(1), IMAGE(2);private final int value;ColumnType(int value){this.value = value;}public int value(){return this.value;}} }導(dǎo)入的工具類:
package com.goktech.common.core.utils.poi;import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.math.BigDecimal; import java.rmi.ServerException; import java.text.DecimalFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; import javax.servlet.http.HttpServletResponse;import com.common.core.annotation.Excel; import com.common.core.annotation.Excels; import com.common.core.exception.ServiceException; import com.common.core.text.Convert; import com.common.core.utils.DateUtils; import com.common.core.utils.StringUtils; import com.common.core.utils.file.ImageUtils; import com.common.core.utils.reflect.ReflectUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.RegExUtils; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.util.IOUtils; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.*; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.goktech.common.core.utils.file.FileTypeUtils; import org.springframework.util.ResourceUtils; import org.springframework.web.multipart.MultipartFile;/*** Excel相關(guān)處理** @author ruoyi*/ public class ExcelUtil<T> {private static final Logger log = LoggerFactory.getLogger(ExcelUtil.class);public static final String FORMULA_REGEX_STR = "=|-|\\+|@";public static final String[] FORMULA_STR = { "=", "-", "+", "@" };/*** Excel sheet最大行數(shù),默認(rèn)65536*/public static final int SHEET_SIZE = 65536;/*** 工作表名稱*/private String sheetName;/*** 導(dǎo)出類型(EXPORT:導(dǎo)出數(shù)據(jù);IMPORT:導(dǎo)入模板)*/private Excel.Type type;/*** 工作薄對象*/private Workbook wb;/*** 工作表對象*/private Sheet sheet;/*** 樣式列表*/private Map<String, CellStyle> styles;/*** 導(dǎo)入導(dǎo)出數(shù)據(jù)列表*/private List<T> list;/*** 注解列表*/private List<Object[]> fields;/*** 當(dāng)前行號*/private int rownum;/*** 標(biāo)題*/private String title;/*** 最大高度*/private short maxHeight;/*** 統(tǒng)計列表*/private Map<Integer, Double> statistics = new HashMap<Integer, Double>();/*** 數(shù)字格式*/private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00");/*** 實(shí)體對象*/public Class<T> clazz;public ExcelUtil(Class<T> clazz){this.clazz = clazz;}public void init(List<T> list, String sheetName, String title, Excel.Type type){if (list == null){list = new ArrayList<T>();}this.list = list;this.sheetName = sheetName;this.type = type;this.title = title;createExcelField();createWorkbook();createTitle();}/*** 創(chuàng)建excel第一行標(biāo)題*/public void createTitle(){if (StringUtils.isNotEmpty(title)){Row titleRow = sheet.createRow(rownum == 0 ? rownum++ : 0);titleRow.setHeightInPoints(30);Cell titleCell = titleRow.createCell(0);titleCell.setCellStyle(styles.get("title"));titleCell.setCellValue(title);sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(),this.fields.size() - 1));}}/*** 對excel表單默認(rèn)第一個索引名轉(zhuǎn)換成list** @param is 輸入流* @return 轉(zhuǎn)換后集合*/public List<T> importExcel(InputStream is) throws Exception{return importExcel(is, 0);}/*** 對excel表單默認(rèn)第一個索引名轉(zhuǎn)換成list** @param is 輸入流* @param titleNum 標(biāo)題占用行數(shù)* @return 轉(zhuǎn)換后集合*/public List<T> importExcel(InputStream is, int titleNum) throws Exception{return importExcel(StringUtils.EMPTY, is, titleNum, null);}/*** @description: 導(dǎo)入帶有圖片的excel* @param: [sheetName, file]* @return: java.util.List<T>* @date: 2022/8/19 15:46* @version: 1.0**/public List<T> importExcel(MultipartFile file, String sheetName, int ttitleNum) throws Exception{Map<String, PictureData> pictures = getPictures(file, sheetName, file.getInputStream());return importExcel(sheetName, file.getInputStream(), ttitleNum, pictures);}/*** 對excel表單指定表格索引名轉(zhuǎn)換成list** @param sheetName 表格索引名* @param titleNum 標(biāo)題占用行數(shù)* @param is 輸入流* @return 轉(zhuǎn)換后集合*/public List<T> importExcel(String sheetName, InputStream is, int titleNum, Map<String, PictureData> pictures) throws Exception{this.type = Excel.Type.IMPORT;this.wb = WorkbookFactory.create(is);List<T> list = new ArrayList<T>();// 如果指定sheet名,則取指定sheet中的內(nèi)容 否則默認(rèn)指向第1個sheetSheet sheet = StringUtils.isNotEmpty(sheetName) ? wb.getSheet(sheetName) : wb.getSheetAt(0);if (sheet == null){throw new IOException("文件sheet不存在");}// 獲取最后一個非空行的行下標(biāo),比如總行數(shù)為n,則返回的為n-1int rows = sheet.getLastRowNum();if (rows > 0){// 定義一個map用于存放excel列的序號和field.Map<String, Integer> cellMap = new HashMap<String, Integer>();// 獲取表頭Row heard = sheet.getRow(titleNum);for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++){Cell cell = heard.getCell(i);if (StringUtils.isNotNull(cell)){String value = this.getCellValue(heard, i).toString();cellMap.put(value, i);}else{cellMap.put(null, i);}}// 有數(shù)據(jù)時才處理 得到類的所有field.List<Object[]> fields = this.getFields();Map<Integer, Object[]> fieldsMap = new HashMap<Integer, Object[]>();for (Object[] objects : fields){Excel attr = (Excel) objects[1];Integer column = cellMap.get(attr.name());if (column != null){fieldsMap.put(column, objects);}}for (int i = titleNum + 1; i <= rows; i++){// 從第2行開始取數(shù)據(jù),默認(rèn)第一行是表頭.Row row = sheet.getRow(i);// 判斷當(dāng)前行是否是空行if (isRowEmpty(row)){continue;}T entity = null;for (Map.Entry<Integer, Object[]> entry : fieldsMap.entrySet()){Object val = this.getCellValue(row, entry.getKey());String mergedRegionValue = getMergedRegionValue(sheet, i, entry.getKey());if(ObjectUtils.isEmpty(val) && StringUtils.isNotEmpty(mergedRegionValue)){val = mergedRegionValue;}// 如果不存在實(shí)例則新建.entity = (entity == null ? clazz.newInstance() : entity);// 從map中得到對應(yīng)列的field.Field field = (Field) entry.getValue()[0];Excel attr = (Excel) entry.getValue()[1];// 取得類型,并根據(jù)對象類型設(shè)置值.Class<?> fieldType = field.getType();if (String.class == fieldType){String s = Convert.toStr(val);if (StringUtils.endsWith(s, ".0")){val = StringUtils.substringBefore(s, ".0");}else{String dateFormat = field.getAnnotation(Excel.class).dateFormat();if (StringUtils.isNotEmpty(dateFormat)){val = parseDateToStr(dateFormat, val);}else{val = Convert.toStr(val);}}}else if ((Integer.TYPE == fieldType || Integer.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val))){val = Convert.toInt(val);}else if ((Long.TYPE == fieldType || Long.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val))){val = Convert.toLong(val);}else if (Double.TYPE == fieldType || Double.class == fieldType){val = Convert.toDouble(val);}else if (Float.TYPE == fieldType || Float.class == fieldType){val = Convert.toFloat(val);}else if (BigDecimal.class == fieldType){val = Convert.toBigDecimal(val);}else if (Date.class == fieldType){if (val instanceof String){val = DateUtils.parseDate(val);}else if (val instanceof Double){val = DateUtil.getJavaDate((Double) val);}}else if (Boolean.TYPE == fieldType || Boolean.class == fieldType){val = Convert.toBool(val, false);}if (StringUtils.isNotNull(fieldType)){String propertyName = field.getName();if (StringUtils.isNotEmpty(attr.targetAttr())){propertyName = field.getName() + "." + attr.targetAttr();}else if (StringUtils.isNotEmpty(attr.readConverterExp())){val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());}else if (!attr.handler().equals(ExcelHandlerAdapter.class)){val = dataFormatHandlerAdapter(val, attr);}else if(attr.getPicture()){String rowCell = i + "-" + entry.getKey();PictureData pictureData = pictures.get(rowCell);if(Objects.nonNull(pictureData)){val = pictureData;}else {val = null;}}ReflectUtils.invokeSetter(entity, propertyName, val);}}list.add(entity);}}return list;}/*** 對list數(shù)據(jù)源將其里面的數(shù)據(jù)導(dǎo)入到excel表單** @param response 返回數(shù)據(jù)* @param list 導(dǎo)出數(shù)據(jù)集合* @param sheetName 工作表的名稱* @return 結(jié)果* @throws IOException*/public void exportExcel(HttpServletResponse response, List<T> list, String sheetName){exportExcel(response, list, sheetName, StringUtils.EMPTY);}/*** 對list數(shù)據(jù)源將其里面的數(shù)據(jù)導(dǎo)入到excel表單** @param response 返回數(shù)據(jù)* @param list 導(dǎo)出數(shù)據(jù)集合* @param sheetName 工作表的名稱* @param title 標(biāo)題* @return 結(jié)果* @throws IOException*/public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title){response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setCharacterEncoding("utf-8");this.init(list, sheetName, title, Excel.Type.EXPORT);exportExcel(response);}/*** 對list數(shù)據(jù)源將其里面的數(shù)據(jù)導(dǎo)入到excel表單** @param sheetName 工作表的名稱* @return 結(jié)果*//*** 對list數(shù)據(jù)源將其里面的數(shù)據(jù)導(dǎo)入到excel表單** @param sheetName 工作表的名稱* @return 結(jié)果*/public void importTemplateExcel(HttpServletResponse response, String sheetName){importTemplateExcel(response, sheetName, StringUtils.EMPTY);}/*** 對list數(shù)據(jù)源將其里面的數(shù)據(jù)導(dǎo)入到excel表單** @param sheetName 工作表的名稱* @param title 標(biāo)題* @return 結(jié)果*/public void importTemplateExcel(HttpServletResponse response, String sheetName, String title){response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setCharacterEncoding("utf-8");this.init(null, sheetName, title, Excel.Type.IMPORT);exportExcel(response);}/*** 對list數(shù)據(jù)源將其里面的數(shù)據(jù)導(dǎo)入到excel表單** @return 結(jié)果*/public void exportExcel(HttpServletResponse response){try{writeSheet();wb.write(response.getOutputStream());}catch (Exception e){log.error("導(dǎo)出Excel異常{}", e.getMessage());}finally{IOUtils.closeQuietly(wb);}}/*** 創(chuàng)建寫入數(shù)據(jù)到Sheet*/public void writeSheet(){// 取出一共有多少個sheet.int sheetNo = Math.max(1, (int) Math.ceil(list.size() * 1.0 / SHEET_SIZE));for (int index = 0; index < sheetNo; index++){createSheet(sheetNo, index);// 產(chǎn)生一行Row row = sheet.createRow(rownum);int column = 0;// 寫入各個字段的列頭名稱for (Object[] os : fields){Excel excel = (Excel) os[1];this.createCell(excel, row, column++);}if (Excel.Type.EXPORT.equals(type)){fillExcelData(index, row);addStatisticsRow();}}}/*** 填充excel數(shù)據(jù)** @param index 序號* @param row 單元格行*/public void fillExcelData(int index, Row row){int startNo = index * SHEET_SIZE;int endNo = Math.min(startNo + SHEET_SIZE, list.size());for (int i = startNo; i < endNo; i++){row = sheet.createRow(i + 1 + rownum - startNo);// 得到導(dǎo)出對象.T vo = (T) list.get(i);int column = 0;for (Object[] os : fields){Field field = (Field) os[0];Excel excel = (Excel) os[1];this.addCell(excel, row, vo, field, column++);}}}/*** 創(chuàng)建表格樣式** @param wb 工作薄對象* @return 樣式列表*/private Map<String, CellStyle> createStyles(Workbook wb){// 寫入各條記錄,每條記錄對應(yīng)excel表中的一行Map<String, CellStyle> styles = new HashMap<String, CellStyle>();CellStyle style = wb.createCellStyle();style.setAlignment(HorizontalAlignment.CENTER);style.setVerticalAlignment(VerticalAlignment.CENTER);Font titleFont = wb.createFont();titleFont.setFontName("Arial");titleFont.setFontHeightInPoints((short) 16);titleFont.setBold(true);style.setFont(titleFont);styles.put("title", style);style = wb.createCellStyle();style.setAlignment(HorizontalAlignment.CENTER);style.setVerticalAlignment(VerticalAlignment.CENTER);style.setBorderRight(BorderStyle.THIN);style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());style.setBorderLeft(BorderStyle.THIN);style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());style.setBorderTop(BorderStyle.THIN);style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());style.setBorderBottom(BorderStyle.THIN);style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());Font dataFont = wb.createFont();dataFont.setFontName("Arial");dataFont.setFontHeightInPoints((short) 10);style.setFont(dataFont);styles.put("data", style);style = wb.createCellStyle();style.cloneStyleFrom(styles.get("data"));style.setAlignment(HorizontalAlignment.CENTER);style.setVerticalAlignment(VerticalAlignment.CENTER);style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());style.setFillPattern(FillPatternType.SOLID_FOREGROUND);Font headerFont = wb.createFont();headerFont.setFontName("Arial");headerFont.setFontHeightInPoints((short) 10);headerFont.setBold(true);headerFont.setColor(IndexedColors.WHITE.getIndex());style.setFont(headerFont);styles.put("header", style);style = wb.createCellStyle();style.setAlignment(HorizontalAlignment.CENTER);style.setVerticalAlignment(VerticalAlignment.CENTER);Font totalFont = wb.createFont();totalFont.setFontName("Arial");totalFont.setFontHeightInPoints((short) 10);style.setFont(totalFont);styles.put("total", style);return styles;}/*** 創(chuàng)建單元格*/public Cell createCell(Excel attr, Row row, int column){// 創(chuàng)建列Cell cell = row.createCell(column);// 寫入列信息cell.setCellValue(attr.name());setDataValidation(attr, row, column);cell.setCellStyle(styles.get("header"));return cell;}/*** 設(shè)置單元格信息** @param value 單元格值* @param attr 注解相關(guān)* @param cell 單元格信息*/public void setCellVo(Object value, Excel attr, Cell cell){if (Excel.ColumnType.STRING == attr.cellType()){String cellValue = Convert.toStr(value);// 對于任何以表達(dá)式觸發(fā)字符 =-+@開頭的單元格,直接使用tab字符作為前綴,防止CSV注入。if (StringUtils.startsWithAny(cellValue, FORMULA_STR)){cellValue = RegExUtils.replaceFirst(cellValue, FORMULA_REGEX_STR, "\t$0");}cell.setCellValue(StringUtils.isNull(cellValue) ? attr.defaultValue() : cellValue + attr.suffix());}else if (Excel.ColumnType.NUMERIC == attr.cellType()){if (StringUtils.isNotNull(value)){cell.setCellValue(StringUtils.contains(Convert.toStr(value), ".") ? Convert.toDouble(value) : Convert.toInt(value));}}else if (Excel.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();}/*** 獲取圖片類型,設(shè)置圖片插入類型*/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;}/*** 創(chuàng)建表格樣式*/public void setDataValidation(Excel attr, Row row, int column){if (attr.name().indexOf("注:") >= 0){sheet.setColumnWidth(column, 6000);}else{// 設(shè)置列寬sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));}if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0){// 提示信息或只能選擇不能輸入的列內(nèi)容.setPromptOrValidation(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);}}/*** 添加單元格*/public Cell addCell(Excel attr, Row row, T vo, Field field, int column){Cell cell = null;try{// 設(shè)置行高row.setHeight(maxHeight);// 根據(jù)Excel中設(shè)置情況決定是否導(dǎo)出,有些情況需要保持為空,希望用戶填寫這一列.if (attr.isExport()){// 創(chuàng)建cellcell = row.createCell(column);setDataCell(cell, attr);// 用于讀取對象中的屬性Object value = getTargetValue(vo, field, attr);String dateFormat = attr.dateFormat();String readConverterExp = attr.readConverterExp();String separator = attr.separator();if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value)){cell.setCellValue(parseDateToStr(dateFormat, value));}else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value)){cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator));}else if (value instanceof BigDecimal && -1 != attr.scale()){cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).toString());}else if (!attr.handler().equals(ExcelHandlerAdapter.class)){cell.setCellValue(dataFormatHandlerAdapter(value, attr));}else{// 設(shè)置列類型setCellVo(value, attr, cell);}addStatisticsData(column, Convert.toStr(value), attr);}}catch (Exception e){log.error("導(dǎo)出Excel失敗{}", e);}return cell;}/*** 設(shè)置單元格樣式* * @param cell 單元格* @param excel 注解信息*/public void setDataCell(Cell cell, Excel excel){CellStyle style = wb.createCellStyle();style.setAlignment(HorizontalAlignment.CENTER);style.setVerticalAlignment(VerticalAlignment.CENTER);style.setBorderRight(BorderStyle.THIN);style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());style.setBorderLeft(BorderStyle.THIN);style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());style.setBorderTop(BorderStyle.THIN);style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());style.setBorderBottom(BorderStyle.THIN);style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());style.setAlignment(excel.align());Font dataFont = wb.createFont();dataFont.setFontName("Arial");dataFont.setFontHeightInPoints((short) 10);dataFont.setColor(excel.color().index);style.setFont(dataFont);cell.setCellStyle(style);}/*** 設(shè)置 POI XSSFSheet 單元格提示或選擇框* * @param sheet 表單* @param textlist 下拉框顯示的內(nèi)容* @param promptContent 提示內(nèi)容* @param firstRow 開始行* @param endRow 結(jié)束行* @param firstCol 開始列* @param endCol 結(jié)束列*/public void setPromptOrValidation(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow,int firstCol, int endCol){DataValidationHelper helper = sheet.getDataValidationHelper();DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1");CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);DataValidation dataValidation = helper.createValidation(constraint, regions);if (StringUtils.isNotEmpty(promptContent)){// 如果設(shè)置了提示信息則鼠標(biāo)放上去提示dataValidation.createPromptBox("", promptContent);dataValidation.setShowPromptBox(true);}// 處理Excel兼容性問題if (dataValidation instanceof XSSFDataValidation){dataValidation.setSuppressDropDownArrow(true);dataValidation.setShowErrorBox(true);}else{dataValidation.setSuppressDropDownArrow(false);}sheet.addValidationData(dataValidation);}/*** 解析導(dǎo)出值 0=男,1=女,2=未知** @param propertyValue 參數(shù)值* @param converterExp 翻譯注解* @param separator 分隔符* @return 解析后值*/public static String convertByExp(String propertyValue, String converterExp, String separator){StringBuilder propertyString = new StringBuilder();String[] convertSource = converterExp.split(",");for (String item : convertSource){String[] itemArray = item.split("=");if (StringUtils.containsAny(separator, propertyValue)){for (String value : propertyValue.split(separator)){if (itemArray[0].equals(value)){propertyString.append(itemArray[1] + separator);break;}}}else{if (itemArray[0].equals(propertyValue)){return itemArray[1];}}}return StringUtils.stripEnd(propertyString.toString(), separator);}/*** 反向解析值 男=0,女=1,未知=2** @param propertyValue 參數(shù)值* @param converterExp 翻譯注解* @param separator 分隔符* @return 解析后值*/public static String reverseByExp(String propertyValue, String converterExp, String separator){StringBuilder propertyString = new StringBuilder();String[] convertSource = converterExp.split(",");for (String item : convertSource){String[] itemArray = item.split("=");if (StringUtils.containsAny(separator, propertyValue)){for (String value : propertyValue.split(separator)){if (itemArray[1].equals(value)){propertyString.append(itemArray[0] + separator);break;}}}else{if (itemArray[1].equals(propertyValue)){return itemArray[0];}}}return StringUtils.stripEnd(propertyString.toString(), separator);}/*** 數(shù)據(jù)處理器** @param value 數(shù)據(jù)值* @param excel 數(shù)據(jù)注解* @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("不能格式化數(shù)據(jù) " + excel.handler(), e.getMessage());}return Convert.toStr(value);}/*** 合計統(tǒng)計信息*/private void addStatisticsData(Integer index, String text, Excel entity){if (entity != null && entity.isStatistics()){Double temp = 0D;if (!statistics.containsKey(index)){statistics.put(index, temp);}try{temp = Double.valueOf(text);}catch (NumberFormatException e){}statistics.put(index, statistics.get(index) + temp);}}/*** 創(chuàng)建統(tǒng)計行*/public void addStatisticsRow(){if (statistics.size() > 0){Row row = sheet.createRow(sheet.getLastRowNum() + 1);Set<Integer> keys = statistics.keySet();Cell cell = row.createCell(0);cell.setCellStyle(styles.get("total"));cell.setCellValue("合計");for (Integer key : keys){cell = row.createCell(key);cell.setCellStyle(styles.get("total"));cell.setCellValue(DOUBLE_FORMAT.format(statistics.get(key)));}statistics.clear();}}/*** 獲取bean中的屬性值** @param vo 實(shí)體對象* @param field 字段* @param excel 注解* @return 最終的屬性值* @throws Exception*/private Object getTargetValue(T vo, Field field, Excel excel) throws Exception{Object o = field.get(vo);if (StringUtils.isNotEmpty(excel.targetAttr())){String target = excel.targetAttr();if (target.contains(".")){String[] targets = target.split("[.]");for (String name : targets){o = getValue(o, name);}}else{o = getValue(o, target);}}return o;}/*** 以類的屬性的get方法方法形式獲取值** @param o* @param name* @return value* @throws Exception*/private Object getValue(Object o, String name) throws Exception{if (StringUtils.isNotNull(o) && StringUtils.isNotEmpty(name)){Class<?> clazz = o.getClass();Field field = clazz.getDeclaredField(name);field.setAccessible(true);o = field.get(o);}return o;}/*** 得到所有定義字段*/private void createExcelField(){this.fields = getFields();this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());this.maxHeight = getRowHeight();}/*** 獲取字段注解信息*/public List<Object[]> getFields(){List<Object[]> fields = new ArrayList<Object[]>();List<Field> tempFields = new ArrayList<>();tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));for (Field field : tempFields){// 單注解if (field.isAnnotationPresent(Excel.class)){Excel attr = field.getAnnotation(Excel.class);if (attr != null && (attr.type() == Excel.Type.ALL || attr.type() == type)){field.setAccessible(true);fields.add(new Object[] { field, attr });}}// 多注解if (field.isAnnotationPresent(Excels.class)){Excels attrs = field.getAnnotation(Excels.class);Excel[] excels = attrs.value();for (Excel attr : excels){if (attr != null && (attr.type() == Excel.Type.ALL || attr.type() == type)){field.setAccessible(true);fields.add(new Object[] { field, attr });}}}}return fields;}/*** 根據(jù)注解獲取最大行高*/public short getRowHeight(){double maxHeight = 0;for (Object[] os : this.fields){Excel excel = (Excel) os[1];maxHeight = Math.max(maxHeight, excel.height());}return (short) (maxHeight * 20);}/*** 創(chuàng)建一個工作簿*/public void createWorkbook(){this.wb = new SXSSFWorkbook(500);this.sheet = wb.createSheet();wb.setSheetName(0, sheetName);this.styles = createStyles(wb);}/*** 創(chuàng)建工作表** @param sheetNo sheet數(shù)量* @param index 序號*/public void createSheet(int sheetNo, int index){// 設(shè)置工作表的名稱.if (sheetNo > 1 && index > 0){this.sheet = wb.createSheet();this.createTitle();wb.setSheetName(index, sheetName + index);}}/*** 獲取單元格值** @param row 獲取的行* @param column 獲取單元格列號* @return 單元格值*/public Object getCellValue(Row row, int column){if (row == null){return row;}Object val = "";try{Cell cell = row.getCell(column);if (StringUtils.isNotNull(cell)){if (cell.getCellType() == CellType.NUMERIC || cell.getCellType() == CellType.FORMULA){val = cell.getNumericCellValue();if (DateUtil.isCellDateFormatted(cell)){val = DateUtil.getJavaDate((Double) val); // POI Excel 日期格式轉(zhuǎn)換}else{if ((Double) val % 1 != 0){val = new BigDecimal(val.toString());}else{val = new DecimalFormat("0").format(val);}}}else if (cell.getCellType() == CellType.STRING){val = cell.getStringCellValue();}else if (cell.getCellType() == CellType.BOOLEAN){val = cell.getBooleanCellValue();}else if (cell.getCellType() == CellType.ERROR){val = cell.getErrorCellValue();}}}catch (Exception e){return val;}return val;}/*** 判斷是否是空行** @param row 判斷的行* @return*/private boolean isRowEmpty(Row row){if (row == null){return true;}for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++){Cell cell = row.getCell(i);if (cell != null && cell.getCellType() != CellType.BLANK){return false;}}return true;}/*** 格式化不同類型的日期對象* * @param dateFormat 日期格式* @param val 被格式化的日期對象* @return 格式化后的日期字符*/public String parseDateToStr(String dateFormat, Object val){if (val == null){return "";}String str;if (val instanceof Date){str = DateUtils.parseDateToStr(dateFormat, (Date) val);}else if (val instanceof LocalDateTime){str = DateUtils.parseDateToStr(dateFormat, DateUtils.toDate((LocalDateTime) val));}else if (val instanceof LocalDate){str = DateUtils.parseDateToStr(dateFormat, DateUtils.toDate((LocalDate) val));}else{str = val.toString();}return str;}/*** 獲取合并單元格的值** @param sheet* @param row* @param column* @return*/public static String getMergedRegionValue(Sheet sheet, int row, int column) {int sheetMergeCount = sheet.getNumMergedRegions();for (int i = 0; i < sheetMergeCount; i++) {CellRangeAddress ca = sheet.getMergedRegion(i);int firstColumn = ca.getFirstColumn();int lastColumn = ca.getLastColumn();int firstRow = ca.getFirstRow();int lastRow = ca.getLastRow();if (row >= firstRow && row <= lastRow) {if (column >= firstColumn && column <= lastColumn) {Row fRow = sheet.getRow(firstRow);Cell fCell = fRow.getCell(firstColumn);return getCellValues(fCell);}}}return null;}/*** 獲取單元格的值** @param fCell* @return*/public static String getCellValues(Cell fCell) {if(fCell == null){return "";}return fCell.toString();}//獲取圖片信息和所在行號列號,//已擴(kuò)展,還能獲取所在行指定列的數(shù)據(jù)private static Map<String, PictureData> getPictures(MultipartFile file, String sheetName, InputStream inputStream){String fileName = file.getOriginalFilename();boolean isXlsx = fileName.contains(".xlsx");Map<String, PictureData> map = new HashMap<>();try (InputStream is = inputStream) {Workbook wb;// 重點(diǎn)if (isXlsx) {wb = new XSSFWorkbook(is);List<POIXMLDocumentPart> list = ((XSSFSheet) wb.getSheet(sheetName)).getRelations(); // Map<String, XSSFPictureData> pictures = getPictures((XSSFSheet) wb.getSheet(sheetName));for (POIXMLDocumentPart part : list) {if (part instanceof XSSFDrawing) {XSSFDrawing drawing = (XSSFDrawing) part;List<XSSFShape> shapes = drawing.getShapes();for (XSSFShape shape : shapes) {XSSFPicture picture = (XSSFPicture) shape;XSSFClientAnchor anchor = picture.getPreferredSize();CTMarker marker = anchor.getFrom();String key = marker.getRow() + "-" + marker.getCol();map.put(key, picture.getPictureData());}}}} else {wb = new HSSFWorkbook(is);List<HSSFShape> list = ((HSSFSheet) wb.getSheet(sheetName)).getDrawingPatriarch().getChildren();for (HSSFShape shape : list) {if (shape instanceof HSSFPicture) {HSSFPicture picture = (HSSFPicture) shape;HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor();PictureData pdata = picture.getPictureData();String key = cAnchor.getRow1() + "-" + cAnchor.getCol1(); //列號(左上角)map.put(key, pdata);}}}return map;} catch (Exception e) {throw new ServiceException("解析圖片異常!");}} }如上就是整個導(dǎo)入圖片的操作,先獲取excel中圖片和所在的列,在讀取excel時判斷該列在實(shí)體映射中是否有g(shù)etPicture = true的標(biāo)識,有就用當(dāng)前列獲取開始取到的圖片信息就可以了。
導(dǎo)入可能遇到的問題記錄
報錯信息如下:
minio The field file exceeds its maximum permitted size of 1048576 bytes.
可以拉取后看我調(diào)用的方法,一目了然。
resource下的模板下載,可查看文章:模板下載
帶圖片的數(shù)據(jù)導(dǎo)出到excel,可查看文章:Java中帶圖片的數(shù)據(jù)導(dǎo)出到excel
死鬼~看完了來個三連哦!O.O`
反手就是一個贊贊贊——————————奧里給
總結(jié)
以上是生活随笔為你收集整理的Java带图片的excel数据导入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 静态时序分析—脉冲宽度检查(Pulse
- 下一篇: 纯Java实现PDF转txt文件