JAVA 導出 Excel, 將Excel下載到本地


昨天說了如何將數據導出成 excel 的兩種方式,今天完善一下將 java 導出(文件在服務器)並下載到本地

 

1. 觸發導出 ajax 代碼

              $.ajax({
                   type: "POST",
                   url: "${ctx}/website/clsecurity/XXXXXAction_exportUserinfoData.do", 
           async:
false,
           dataType:
"json",
           data: {
              "province": province,
              "userType": userType,
              "startDate": startDate,
              "endDate": endDate
          },
          success:
function(data) {
            
var json = eval( '(' + data + ')' );
            window.open(
"${ctx}" + json.url);
          }
       });

 

2. 處理導出的方法代碼片段

       List<ClUserinfo> regUsers = clSecurityService.findClUserinfos(clSecurityForm);
            
            List<TempUser> userList = new ArrayList<TempUser>();
            for (ClUserinfo userinfo : regUsers) {
                TempUser user = new TempUser();
                
                user.setUserName(userinfo.getUserName());
                user.setUserType(userinfo.getUserType());
                user.setCompany(userinfo.getCompany());
                user.setKeshi(userinfo.getKeshi());
                user.setProvince(userinfo.getProvince());
                user.setCreatedDate(userinfo.getCreatedDate());
                user.setCreateDateStr(userinfo.getCreatedDate().toString());
                
                userList.add(user);
            }
            
            HSSFWorkbook wb = new HSSFWorkbook();  
            HSSFSheet sheet = wb.createSheet("注冊用戶表");  
            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);  
      
            for (int i = 0; i < userList.size(); i++) {  
                row = sheet.createRow((int) i + 1);  
                TempUser user = (TempUser) userList.get(i);  
                
                row.createCell((short) 0).setCellValue(user.getUserName());  
                row.createCell((short) 1).setCellValue(user.getUserType());  
                row.createCell((short) 2).setCellValue(user.getCompany());
                row.createCell((short) 3).setCellValue(user.getKeshi());
                row.createCell((short) 4).setCellValue(user.getCreateDateStr());
            }  

            try {  
                String fileName = "RegistUserList-" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + ".xls";
                String savaPath = ServletActionContext.getRequest().getRealPath("/upload/excel");
                FileOutputStream fos = new FileOutputStream(savaPath + "\\" + fileName); 
                wb.write(fos); 
                fos.close();  

                Map<String,String> map = new HashMap<String,String>();
                map.put("url", "/upload/excel/" + fileName);
                JSONObject json = JSONObject.fromObject(map);
                
                this.result = json.toString();
            } catch (Exception e) {  
                e.printStackTrace();  
            }  

 

3. result 裝返回結果

  /**
     * json return
     */
    private String result;     
    
    public String getResult() {
        return result;
    }
public void setResult(String result) { this.result = result; }

 

4. XXXXXAction_XXXXXXX 方法以及 result 在 struts2 的配置

    <action name="XXXXXAction_exportUserinfoData" class="com.gzewell.ucomweb.web.security.action.XXXXXAction" method="exportUserinfoData"> 
       <result name="success" type="json"> 
         <param name="root">result</param> 
       </result> 
    </action>

 

 

說明:

  fos:文件輸出流,將文件放在服務器的 /upload/excel 目錄

  result: 將路徑放入map 結果以 json 的形式返回

   window.open:打開新的頁面,即為需要下載的文件在服務器的位置

   ${ctx}: 為服務器地址ip | 網址的表達式 (127.0.0.1) 

  

 

 


免責聲明!

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



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