java中將資料和圖片匯出到Excel檔案中

來源:互聯網
上載者:User

 public ActionForward picExcel(ActionMapping mapping,ActionForm form,
  HttpServletRequest request,HttpServletResponse response)throws Exception {
  response.reset();
  response.setContentType("application/vnd.ms-excel");
  ServletContext context = this.getServlet().getServletContext();
  try {
   String path = context.getRealPath("/reports/hab/apsfa/PicStat.xls");//匯出的Excel檔案在伺服器上的輸出路徑
   java.io.File reportFile1 =
    new File(context.getRealPath("/reports/hab/apsfa/statPic.xls"));//此Excel檔案是伺服器上的,一個包含一張統計圖片的Excel模板
   java.io.FileInputStream fos = new FileInputStream(reportFile1);//檔案輸入資料流
   java.io.FileOutputStream os = new FileOutputStream(path);//檔案輸出資料流

   String barName =
    FileHelper.getUploadDir() + "/" + "collentResult2.jpg";//要匯出到Excel檔案中的圖片路徑

   org.apache.poi.poifs.filesystem.POIFSFileSystem fs = new POIFSFileSystem(fos);
   org.apache.poi.hssf.usermodel.HSSFWorkbook wb = new HSSFWorkbook(fs);
   org.apache.poi.hssf.usermodel.HSSFSheet sheet = wb.getSheetAt(0);//Excel檔案中的第一個工作表
   org.apache.poi.hssf.usermodel.HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
   org.apache.poi.hssf.usermodel.HSSFClientAnchor anchor =new HSSFClientAnchor(
     0,0,350,100,(short) 0,1,(short) 10,10);

   HkSfaStatBC bc = new HkSfaStatBCImpl();//後台實作類別 --結果統計
   HkSfaActivityBC abc = new HkSfaActivityBCImpl();//後台實作類別 --審計活動
   String activitypk = request.getParameter("activitypk");//活動PK
   List childGroups = abc.getChildgroup(activitypk);//得到此活動的所有小組

   double[][] data = bc.problemCollect(activitypk);//得到統計結果,一個二維數組

   org.apache.poi.hssf.usermodel.HSSFRow row = sheet.createRow(11);//在第一個工作表中建立一行(第12行)
   for (int k = 0; k < childGroups.size(); k++) {
    ApSfaGroupactivity activityGroup =(ApSfaGroupactivity) childGroups.get(k);//ApSfaGroupactivity為活動小組的Pojo
    org.apache.poi.hssf.usermodel.HSSFCell cell = row.createCell((short) (k + 1));//在row中建立儲存格
    cell.setEncoding(HSSFCell.ENCODING_UTF_16);//設定編碼
    cell.setCellValue(activityGroup.getName());//給儲存格設定值
   }

   /* 同前一個迴圈,給Excel添加資料 */
   String[] strs ={ "不適用", "符合", "整改關閉", "不符合", "未檢查", "檢查數量", "已選擇總數" };
   for (int i = 12; i < strs.length + 12; i++) {
    org.apache.poi.hssf.usermodel.HSSFRow row1 = sheet.createRow(i);
    for (int j = 0; j < data[0].length + 1; j++) {
     org.apache.poi.hssf.usermodel.HSSFCell cell = row1.createCell((short) j);
     cell.setEncoding(HSSFCell.ENCODING_UTF_16);
     if (j == 0) {
      cell.setCellValue(strs[i - 12]);
     } else {
      int a = (int) data[i - 12][j - 1];
      cell.setCellValue(a);
     }
    }
   }

   java.io.File jpgfile = new File(barName);//barName 為統計圖片在伺服器上的路徑
   org.apache.commons.io.output.ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();//位元組輸出資料流,用來寫二進位檔案
   java.awt.image.BufferedImage bufferImg = ImageIO.read(jpgfile);
   javax.imageio.ImageIO.write(bufferImg, "jpg", byteArrayOut);

   patriarch.createPicture(anchor,wb.addPicture(
     byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));//將統計圖片添加到Excel檔案中
   wb.write(os);
   os.close();
   net.sf.excelutils.ExcelUtils.addValue("actionServlet", this);
   String config = null;

   config = "/reports/hab/apsfa/PicStat.xls";
   response.setHeader("Content-Disposition","attachment; filename=/"" + "PicStat.xls/"");
   net.sf.excelutils.ExcelUtils.export(getServlet().getServletContext(),config,response.getOutputStream());
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return null;
 }

/*

相關Jar包和excel檔案在我的資源中有,去下載就是了,

地址為:http://download.csdn.net/source/313015

*/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.