您的位置:首页 > 其它

jxl导出excel(通讯录)

2016-10-29 16:11 381 查看
<%!
//导出功能
public void exportExcel(Vector temV,Pub pub,HttpServletResponse response,HttpServletRequest request){

   File file = null;  
   BufferedInputStream br = null;
OutputStream outStream = null;
String filename = "通讯录.xls";
   //PrintWriter out = null;  

        try{  

            //out = response.getWriter();  

            file = new File("通讯录.xls");  

            String agent = request.getHeader("User-Agent");
//如果是火狐浏览器
if(agent.contains("Firefox")) {
filename = new String(filename.getBytes(),"ISO8859-1");
}else {//ie浏览器 url编码
filename = URLEncoder.encode(filename, "utf-8");
}

            WritableWorkbook wwb;     

            wwb = Workbook.createWorkbook(file);  

            WritableSheet ws = wwb.createSheet("用户信息Excel", 0);  

            //格式

            SheetSettings ss = ws.getSettings();  

            ss.setVerticalFreeze(1);//冻结表头  

            WritableFont font1 =new WritableFont(WritableFont.createFont("微软雅黑"), 12 ,WritableFont.BOLD);  

            WritableFont font2 =new WritableFont(WritableFont.createFont("宋体"), 12 ,WritableFont.NO_BOLD);  

            WritableCellFormat wcf = new WritableCellFormat(font1);  

            WritableCellFormat wcf2 = new WritableCellFormat(font2);           

            //创建单元格样式  

            //WritableCellFormat wcf = new WritableCellFormat();  

            //背景颜色  

            wcf.setBackground(jxl.format.Colour.YELLOW);  

            wcf.setAlignment(Alignment.CENTRE);  //平行居中  

            wcf.setVerticalAlignment(VerticalAlignment.CENTRE);  //垂直居中  

            wcf2.setAlignment(Alignment.CENTRE);  //平行居中  

            wcf2.setVerticalAlignment(VerticalAlignment.CENTRE);  //垂直居中  

  

            /* 

             * 这个是单元格内容居中显示 

             * 还有很多很多样式 

             */  

            wcf.setAlignment(Alignment.CENTRE);  
            //头信息
           String[] columns = {"序号","姓名","职务","部门","办公电话","移动电话","邮箱"};
           for (int i = 0; i < columns.length; i++) {     
               ws.addCell(new Label(i, 0, columns[i],wcf)); 
               ws.setColumnView(i, 20);  
           }  
           for(int i =0;i<temV.size();i++){
    Hashtable ht = (Hashtable)temV.elementAt((i));

    String userName=pub.trimNull((String)ht.get("username"));

    String dispalyName=pub.trimNull((String)ht.get("displayname"));
    String userContainerName=pub.trimNull((String)ht.get("usercontainername"));
    String telephoneNumber=pub.trimNull((String)ht.get("telephonenumber"));
    String mobile=pub.trimNull((String)ht.get("mobile"));
    String mail = pub.trimNull((String)ht.get("mail"));
               ws.addCell(new Label(0, i+1, String.valueOf(i+1),wcf2));   
               ws.addCell(new Label(1, i+1, userName,wcf2));    
               ws.addCell(new Label(2, i+1, dispalyName,wcf2));  
               ws.addCell(new Label(3, i+1, userContainerName,wcf2));   
               ws.addCell(new Label(4, i+1, telephoneNumber,wcf2));   
               ws.addCell(new Label(5, i+1, mobile,wcf2));     
               ws.addCell(new Label(6, i+1, mail,wcf2));   
           }  
           wwb.write();  
           wwb.close();  
           br = new BufferedInputStream(new FileInputStream(file));  
           byte[] buf = new byte[1024];  
           int len = 0;  
 
           response.reset(); 
           response.setContentType("application/x-msdownload");  
           response.setHeader("Content-Disposition", "attachment; filename="+filename);    
           outStream = response.getOutputStream();   
           while ((len = br.read(buf)) > 0){  
               outStream.write(buf, 0, len);
           }    
           br.close();  
           outStream.close();  
       }catch(Exception ex){  
           ex.printStackTrace();  
           //out.print("<script>alert('导出用户信息失败、请从试!');</script>");  
       }finally{  
           if (file.exists()) {//下载完毕删除文件  
               file.delete();  
           }  
           if(br!=null){
try{
br.close();
}catch(Exception e2){
System.out.println(e2);
}
}
if(outStream!=null){
try{
outStream.close();
}catch(Exception e3){
System.out.println(e3);
}
}
             
       }  



%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: