jxl导出excel(通讯录)

<%!
//导出功能
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);
}
}
             
       }  




%>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值