java導出exce動態l合並單元格


采用的是poi技術

框架式  ssh 使用的是struts1

 

 

/*

*  

* 導出聯系人  

*  

* @param request  

* @param response

  */

 public void outExcel(ActionMapping mapping, ActionForm form,    HttpServletRequest request, HttpServletResponse response) {  

 ServletContext servletContext = this.getServletContext();  

 WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);   

TraceLxrService service = getTraceLxrService();  

 List<AbstractTraceLxr> list = service.findAllQX();

  

  try {

   // 第一步,創建一個webbook,對應一個Excel文件

   HSSFWorkbook wb = new HSSFWorkbook();

   // 第二步,在webbook中添加一個sheet,對應Excel文件中的sheet  

  HSSFSheet sheet = wb.createSheet("區縣工作小組名單");

   // 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制short

   HSSFRow row = sheet.createRow((int) 0);  

  // 第四步,創建單元格,並設置值表頭 設置表頭居中  

  HSSFCellStyle style = wb.createCellStyle();   

 style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

// 創建一個居中格式       HSSFCell cell = row.createCell((short) 0);  

    cell.setCellValue("區縣");  

    cell.setCellStyle(style);

   cell = row.createCell((short) 1);  

    cell.setCellValue("分工");

    cell.setCellStyle(style);  

   cell = row.createCell((short) 2);   

   cell.setCellValue("姓名");   

   cell.setCellStyle(style);

   cell = row.createCell((short) 3);  

  cell.setCellValue("部門");  

  cell.setCellStyle(style);

   cell = row.createCell((short) 4);  

  cell.setCellValue("職務");  

  cell.setCellStyle(style);

   cell = row.createCell((short) 5);  

  cell.setCellValue("電話");  

  cell.setCellStyle(style);

   cell = row.createCell((short) 6);  

  cell.setCellValue("手機");  

  cell.setCellStyle(style);

   cell = row.createCell((short) 7);   

   cell.setCellValue("傳真");   

   cell.setCellStyle(style);

   cell = row.createCell((short) 8);  

   cell.setCellValue("Email");   

   cell.setCellStyle(style);

   cell = row.createCell((short) 9);

   cell.setCellValue("修改");   

   cell.setCellStyle(style);

   cell = row.createCell((short) 10);

   cell.setCellValue("刪除");  

   cell.setCellStyle(style);        

          List<AbstractTraceLxr> newlist=new ArrayList<AbstractTraceLxr>();

          List<String> list1 = service.findAllQXDz();     

           for (int i = 0; i < list1.size(); i++) {        

           for (int p = 0; p < list.size(); p++) {     

           if(list1.get(i).equals(list.get(p).getSsqx())){    

           TraceLxr sf=new TraceLxr();     

      sf.setSsqx(list.get(p).getSsqx());       

      sf.setFg(list.get(p).getFg());       

      sf.setName(list.get(p).getName());

      sf.setDept(list.get(p).getDept());    

      sf.setJob(list.get(p).getJob());    

      sf.setTel(list.get(p).getTel());     

      sf.setMobile(list.get(p).getMobile());    

      sf.setFax(list.get(p).getFax());     

      sf.setEmail(list.get(p).getEmail());  

      sf.setDelflag("修改");     

      sf.setDelflag("刪除");  

      newlist.add(sf);     

   }      

  }    

  }     

       for (int k1 = 0; k1 < newlist.size(); k1++) {   

       row = sheet.createRow((int) k1 + 1);  

      TraceLxr bgMultiLevel = (TraceLxr) newlist.get(k1);

      row.createCell((short) 0).setCellValue(bgMultiLevel.getSsqx());    

      row.createCell((short) 1).setCellValue(bgMultiLevel.getFg());   

      row.createCell((short) 2).setCellValue(bgMultiLevel.getName());    

      row.createCell((short) 3).setCellValue(bgMultiLevel.getDept());   

      row.createCell((short) 4).setCellValue(bgMultiLevel.getJob());

      row.createCell((short) 5).setCellValue(bgMultiLevel.getTel());  

      row.createCell((short) 6).setCellValue(bgMultiLevel.getMobile());

      row.createCell((short) 7).setCellValue(bgMultiLevel.getFax());   

      row.createCell((short) 8).setCellValue(bgMultiLevel.getEmail());  

      row.createCell((short) 9).setCellValue("0");    

      row.createCell((short) 10).setCellValue("1");  

    if(k1!=0){        

    if(newlist.get(k1-1).getSsqx().equals(newlist.get(k1).getSsqx())){    

       //重點在這里動態合並

          CellRangeAddress cra=new CellRangeAddress(k1, 1+k1, 0, 0);      

                     //在sheet里增加合並單元格           

          sheet.addMergedRegion(cra);        

           }              

     }    

}       

   response.reset();   

   response.setContentType("application/vnd.ms-excel;charset=utf-8");  

  response.setHeader("Content-Disposition", "attachment;filename=" + new String(("exportExcel".toString() + ".xls").getBytes(), "iso-8859-1"));

   OutputStream os = response.getOutputStream();   

    wb.write(os);    os.close();      

    }catch (IOException e) {  

  // TODO Auto-generated catch block  

  e.printStackTrace();

     }

 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM