国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

java poi導出圖片到excel示例代碼

2024-07-14 08:43:50
字體:
來源:轉載
供稿:網友

本文實例為大家分享了java使用poi導出圖片到Excel的具體代碼,供大家參考,具體內容如下

java,poi,導出圖片,excel,代碼

代碼實現

Controller

/** * 導出志愿者/人才數據 * @param talent_type * @return */@RequestMapping("/exportData")public void exportData(Integer talent_type, HttpServletResponse response) {  String fileId = UUID.randomUUID().toString().replace("-", "");  Map<String, Object> param = new HashMap<>() ;  param.put("talent_type", talent_type) ;  try {   List<Map<String, Object>> volunteerMapList = volunteerService.getExportData(param) ;   String rootPath = SysConfigManager.getInstance().getText("/config/sys/rootPath");   String filePath = rootPath + "/" + fileId + ".xlsx" ;   volunteerService.exportData(volunteerMapList, filePath) ;   // 下載   FileInputStream inputStream = null;   try{     //設置發送到客戶端的響應內容類型     response.reset();     response.setContentLength((int) new File(filePath).length());     response.setContentType("application/octet-stream");     response.addHeader("Content-Disposition", "attachment; filename=/"" + URLEncoder.encode("文件名.xlsx", "UTF-8")+ "/"");     //讀取本地圖片輸入流     inputStream = new FileInputStream(filePath);     // 循環取出流中的數據     byte[] b = new byte[1024];     int len;     while ((len = inputStream.read(b)) > 0)      response.getOutputStream().write(b, 0, len);   } finally{     if(inputStream != null){      inputStream.close();     }   }   logger.debug("導出志愿者/人才數據成功!");  } catch (Exception e) {   e.printStackTrace();   logger.error("導出志愿者/人才數據異常!");  }}

Service

public void exportData(List<Map<String, Object>> volunteerMapList, String filePath) throws Exception {   String[] alias = {"頭像", "名稱", "個人/團體", "志愿者/人才", "性別", "生日", "手機號",       "身份證", "省份", "市", "區/縣", "詳細地址", "郵箱", "政治面貌", "學歷", "民族",       "職業", "團隊人數", "藝術特長", "介紹"};   String[] keys = {"photo", "name", "type", "talent_type", "sex", "birth_day", "mobile",       "idcard", "province", "city", "county", "address", "email", "political",       "education", "nation", "profession", "member_count", "art_spetiality", "content"};   File file = new File(filePath);   if (!file.exists()) file.createNewFile();   FileOutputStream fileOutput = new FileOutputStream(file);   XSSFWorkbook workbook = new XSSFWorkbook();   int sheetSize = volunteerMapList.size() + 50;   double sheetNo = Math.ceil(volunteerMapList.size() / sheetSize);   String photoImgPath = SysConfigManager.getInstance().getText("/config/sys/rootPath") ;   for (int index = 0; index <= sheetNo; index++) {     XSSFSheet sheet = workbook.createSheet();     workbook.setSheetName(index, "人才、志愿者" + index);     XSSFRow row = sheet.createRow(0);     sheet.setColumnWidth(0, 2048);     XSSFCell cell;     XSSFCellStyle cellStyle = workbook.createCellStyle();     XSSFFont font = workbook.createFont();     font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);     // 居中     cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);     // 加粗     cellStyle.setFont(font);     //創建標題     for (int i = 0; i < alias.length; i++) {       cell = row.createCell(i);       cell.setCellValue(alias[i]);       cell.setCellStyle(cellStyle);     }     int startNo = index * sheetSize;     int endNo = Math.min(startNo + sheetSize, volunteerMapList.size());     cellStyle = workbook.createCellStyle();     // 居中     cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);     cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);     // 寫入各條記錄,每條記錄對應excel表中的一行     for (int i = startNo; i < endNo; i++) {       int rowNum = i + 1 - startNo ;       row = sheet.createRow(rowNum);       Map<String, Object> map = (Map<String, Object>) volunteerMapList.get(i);       for (int j = 0; j < keys.length; j++) {         cell = row.createCell(j);         String key = keys[j] ;         if (key.equals("photo")){           sheet.addMergedRegion(new CellRangeAddress(i + 1,i + 1,i + 1,i + 1)) ;           // 頭像           File photoFile = new File(photoImgPath + map.get(key)) ;           if (photoFile.exists()){             BufferedImage bufferedImage = ImageIO.read(photoFile) ;             ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();             ImageIO.write(bufferedImage, "jpg", byteArrayOut);             byte[] data = byteArrayOut.toByteArray();             XSSFDrawing drawingPatriarch = sheet.createDrawingPatriarch();             XSSFClientAnchor anchor = new XSSFClientAnchor(480, 30, 700, 250, (short)0, i + 1, (short) 1, i + 2);             drawingPatriarch.createPicture(anchor, workbook.addPicture(data, XSSFWorkbook.PICTURE_TYPE_JPEG));             sheet.setColumnWidth((short)500, (short)500);             row.setHeight((short)500);           } else {             cell.setCellType(XSSFCell.CELL_TYPE_STRING);             cell.setCellValue("");           }         } else {           cell.setCellType(XSSFCell.CELL_TYPE_STRING);           Object value = map.get(key);           cell.setCellValue(value == null ? "" : value.toString());           cell.setCellStyle(cellStyle);         }       }     }     // 設置列寬    for (int i = 1; i < alias.length; i++)       sheet.autoSizeColumn(i);     // 處理中文不能自動調整列寬的問題this.setSizeColumn(sheet, alias.length);   }   fileOutput.flush();   workbook.write(fileOutput);   fileOutput.close(); }  // 自適應寬度(中文支持) private void setSizeColumn(XSSFSheet sheet, int size) {   for (int columnNum = 0; columnNum < size; columnNum++) {     int columnWidth = sheet.getColumnWidth(columnNum) / 256;     for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {       XSSFRow currentRow;       //當前行未被使用過       if (sheet.getRow(rowNum) == null) {         currentRow = sheet.createRow(rowNum);       } else {         currentRow = sheet.getRow(rowNum);       }       if (currentRow.getCell(columnNum) != null) {         XSSFCell currentCell = currentRow.getCell(columnNum);         if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {           int length = currentCell.getStringCellValue().getBytes().length;           if (columnWidth < length) columnWidth = length;         }       }     }     columnWidth = columnWidth * 256 ;     sheet.setColumnWidth(columnNum, columnWidth >= 65280 ? 6000 : columnWidth);   } }

以上所述是小編給大家介紹java poi導出圖片到excel示例代碼解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江安县| 蓬莱市| 修水县| 威海市| 卢氏县| 房产| 白玉县| 玉山县| 马龙县| 华池县| 嘉黎县| 淳安县| 杭州市| 镇雄县| 清徐县| 吴桥县| 衡山县| 云阳县| 迁安市| 湘乡市| 赣榆县| 张掖市| 泾阳县| 安平县| 涡阳县| 山东省| 疏附县| 和顺县| 扶余县| 寻乌县| 湾仔区| 临夏市| 探索| 威远县| 镇江市| 扶风县| 石林| 右玉县| 陇川县| 德庆县| 偏关县|