最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java poi導(dǎo)出圖片到excel示例代碼

 更新時(shí)間:2019年03月11日 10:45:21   作者:954L  
這篇文章主要介紹java poi如何導(dǎo)出圖片到excel,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

本文實(shí)例為大家分享了java使用poi導(dǎo)出圖片到Excel的具體代碼,供大家參考,具體內(nèi)容如下

代碼實(shí)現(xiàn)

Controller

/**
 * 導(dǎo)出志愿者/人才數(shù)據(jù)
 * @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{
     //設(shè)置發(fā)送到客戶端的響應(yīng)內(nèi)容類型
     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);
     // 循環(huán)取出流中的數(shù)據(jù)
     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("導(dǎo)出志愿者/人才數(shù)據(jù)成功!");
  } catch (Exception e) {
   e.printStackTrace();
   logger.error("導(dǎo)出志愿者/人才數(shù)據(jù)異常!");
  }
}

Service

public void exportData(List<Map<String, Object>> volunteerMapList, String filePath) throws Exception {
   String[] alias = {"頭像", "名稱", "個(gè)人/團(tuán)體", "志愿者/人才", "性別", "生日", "手機(jī)號(hào)",
       "身份證", "省份", "市", "區(qū)/縣", "詳細(xì)地址", "郵箱", "政治面貌", "學(xué)歷", "民族",
       "職業(yè)", "團(tuán)隊(duì)人數(shù)", "藝術(shù)特長(zhǎng)", "介紹"};
   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);
     //創(chuàng)建標(biāo)題
     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);
     // 寫入各條記錄,每條記錄對(duì)應(yīng)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);
         }
       }
     }
     // 設(shè)置列寬
    for (int i = 1; i < alias.length; i++)
       sheet.autoSizeColumn(i);
     // 處理中文不能自動(dòng)調(diào)整列寬的問題
this.setSizeColumn(sheet, alias.length);
   }
   fileOutput.flush();
   workbook.write(fileOutput);
   fileOutput.close();
 }
 
 // 自適應(yīng)寬度(中文支持)
 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;
       //當(dāng)前行未被使用過
       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導(dǎo)出圖片到excel示例代碼解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • java jdk1.8 使用stream流進(jìn)行l(wèi)ist 分組歸類操作

    java jdk1.8 使用stream流進(jìn)行l(wèi)ist 分組歸類操作

    這篇文章主要介紹了java jdk1.8 使用stream流進(jìn)行l(wèi)ist 分組歸類操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-10-10
  • LinkedBlockingQueue鏈?zhǔn)阶枞?duì)列的使用和原理解析

    LinkedBlockingQueue鏈?zhǔn)阶枞?duì)列的使用和原理解析

    這篇文章主要介紹了LinkedBlockingQueue鏈?zhǔn)阶枞?duì)列的使用和原理解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • .NET Core使用SignalR實(shí)現(xiàn)實(shí)時(shí)通訊的示例代碼

    .NET Core使用SignalR實(shí)現(xiàn)實(shí)時(shí)通訊的示例代碼

    SignalR是一個(gè)ASP.NETCore庫(kù),用于在客戶端和服務(wù)器之間實(shí)現(xiàn)實(shí)時(shí)通訊,本文主要介紹了.NETCore中使用SignalR實(shí)現(xiàn)實(shí)時(shí)通訊,感興趣的可以了解一下
    2024-11-11
  • 淺談普通for循環(huán)遍歷LinkedList弊端

    淺談普通for循環(huán)遍歷LinkedList弊端

    下面小編就為大家?guī)?lái)一篇淺談普通for循環(huán)遍歷LinkedList弊端。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2017-01-01
  • Mybatis中resultMap的Colum和property屬性詳解

    Mybatis中resultMap的Colum和property屬性詳解

    這篇文章主要介紹了Mybatis中resultMap的Colum和property屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
    2022-01-01
  • 詳解java注解相關(guān)知識(shí)

    詳解java注解相關(guān)知識(shí)

    今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著java注解的使用展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 一篇文章詳解Java異常處理

    一篇文章詳解Java異常處理

    異常處理是編程語(yǔ)言或計(jì)算機(jī)硬件里的一種機(jī)制,用于處理軟件或信息系統(tǒng)中出現(xiàn)的異常狀況(即超出程序正常執(zhí)行流程的某些特殊條件),這篇文章主要給大家介紹了關(guān)于Java異常處理的相關(guān)資料,需要的朋友可以參考下
    2023-12-12
  • java之this關(guān)鍵字用法實(shí)例分析

    java之this關(guān)鍵字用法實(shí)例分析

    這篇文章主要介紹了java之this關(guān)鍵字用法實(shí)例分析,較為詳細(xì)的講述了Java中this關(guān)鍵字的用法及適用范圍,并附帶實(shí)例程序加以說(shuō)明,需要的朋友可以參考下
    2014-09-09
  • 你知道JVM中GC?Root對(duì)象有哪些嗎

    你知道JVM中GC?Root對(duì)象有哪些嗎

    這篇文章主要介紹了你知道JVM中GC?Root對(duì)象有哪些,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Quarkus集成Dubbo服務(wù)Rpc遠(yuǎn)程通訊框架整合

    Quarkus集成Dubbo服務(wù)Rpc遠(yuǎn)程通訊框架整合

    這篇文章主要為大家介紹了Quarkus集成Dubbo服務(wù)Rpc遠(yuǎn)程通訊框架的整合,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-02-02

最新評(píng)論

汉川市| 中山市| 英山县| 楚雄市| 汾西县| 平和县| 蓝山县| 秭归县| 上犹县| 榆树市| 太和县| 华池县| 黔西县| 镇赉县| 姚安县| 酉阳| 天柱县| 本溪| 安龙县| 昆山市| 津南区| 余干县| 忻州市| 宿州市| 沭阳县| 淮滨县| 宣汉县| 伊吾县| 彭山县| 宾阳县| 烟台市| 耿马| 罗定市| 抚远县| 屏南县| 木里| 塘沽区| 玉环县| 盈江县| 乐陵市| 两当县|