您的位置:首页 > 其它

使用jxl导出数据到excel中

2017-05-27 16:15 387 查看
1.
function exportDataToExcel(){
  var formData = $("#user_auth_form").formSerialize();
  location.href="${ctx}/userAuth/ExportDataToExcel.do?"+formData
 
 }

2.
@RequestMapping("/ExportDataToExcel.do")
 public void ExportDataToExcel(UserQuery queryObject,OutputStream outputStream,HttpServletResponse response) throws Exception{
          Map<String,Object> model = new HashMap<String, Object>();
          try{
               queryObject.setUsePage(false);
               List<UserVO> userAuthedList = userAuthService.queryUserPassRecs(queryObject);
               //清空输出流
               response.reset();
               //设置对中文的处理
               response.setCharacterEncoding("UTF-8");
               //设置文件名称
               String date = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(new Date());
               String fileName = URLEncoder.encode("已认证用户统计"+date,"UTF-8");
               response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("UTF-8"),"UTF-8")+".xls");
               //定义输出类型
               response.setContentType("application/msexcel");
               //创建工作薄
               WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
               //创建工作表
               WritableSheet sheet = workbook.createSheet("已认证用户统计",0);
                sheet.setRowView(0, 400);//设置第一行的高度,值越小,高度越小
               sheet.setColumnView(0,15);//设置第一列的宽度,值越大,宽度越小
               sheet.setColumnView(1,30);//设置第一列的宽度,值越大,宽度越小
               sheet.setColumnView(2,15);//设置第一列的宽度,值越大,宽度越小
               sheet.setColumnView(3,30);//设置第一列的宽度,值越大,宽度越小
               sheet.setColumnView(4,30);//设置第一列的宽度,值越大,宽度越小
               sheet.setColumnView(5,20);//设置第一列的宽度,值越大,宽度越小
               //设置标题单元格样式
               WritableFont titleCellStyle = new WritableFont(WritableFont.ARIAL,12,WritableFont.BOLD, false,                      UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
//字体、字体大小、无下划线、粗体、字体颜色
   WritableFont titleCellStyle2 = new WritableFont(WritableFont.ARIAL,9,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED); //字体、字体大小、无下划线、粗体、字体颜色
               //单元格定义
               WritableCellFormat titleCellFormat = new WritableCellFormat(titleCellStyle);
               WritableCellFormat titleCellFormat2 = new WritableCellFormat(titleCellStyle2);
               //设置对齐方式
               titleCellFormat.setAlignment(jxl.format.Alignment.CENTRE);
               titleCellFormat2.setAlignment(jxl.format.Alignment.CENTRE);
               //定义内容单元格
               WritableCellFormat contentCellFormat = new WritableCellFormat();
               //设置对齐方式
               contentCellFormat.setAlignment(jxl.format.Alignment.CENTRE);
               //创建excel标题
               String[] dataTitle = {"认证账户","会员名称","认证类型","认证时间","所在地"};
               for(int i=0;i<userAuthedList.size();i++){
                    if(i<1){
                         for(int j=0;j<5;j++){
                         Label  label = new Label(j,0,dataTitle[j],titleCellFormat);
                         sheet.addCell(label);
                       }
                  }
                //获取DB数据
                UserVO userVO = use
4000
rAuthedList.get(i);
                //认证账户
                Label accountName = new Label(0,i+1,userVO.getAccountName(),contentCellFormat);
                sheet.addCell(accountName);
                //会员名称
                if(userVO.getUserType()==DataConstants.DRIVER || userVO.getUserType()== DataConstants.P_GOODS_OWNER){
                 Label nickName = new Label(1,i+1,userVO.getNickName());
                 sheet.addCell(nickName);
                }else{
                 if(userVO.getNickName() !=null && userVO.getEnterpriseName() !=null){
                      Label nickName = new Label(1,i+1,userVO.getNickName()+"("+userVO.getEnterpriseName()+")");
                      sheet.addCell(nickName);
                 }else if(userVO.getNickName() !=null && userVO.getEnterpriseName() ==null){
                      Label nickName = new Label(1,i+1,userVO.getNickName());
                      sheet.addCell(nickName);
                  }else{
                      Label nickName = new Label(1,i+1,userVO.getEnterpriseName());
                      sheet.addCell(nickName);
                 }
                }
                //认证类型
                if(userVO.getUserType()==DataConstants.DRIVER){
                         Label userType = new Label(2,i+1,"司机认证",titleCellFormat2);
                         sheet.addCell(userType);
                  }else if(userVO.getUserType()==DataConstants.P_GOODS_OWNER){
                         Label userType = new Label(2,i+1,"个人货主认证",contentCellFormat);
                         sheet.addCell(userType);
                    }else{
                           Label userType = new Label(2,i+1,"企业认证",contentCellFormat);
                           sheet.addCell(userType);
                      }
                //认证时间
                Label authTime = new Label(3,i+1,new SimpleDateFormat("yyyy-MM-dd HH:                    mm").format(userVO.getAuthTime()),contentCellFormat);
                //所在地
                Label address = new Label(4,i+1,userVO.getAddress(),titleCellFormat2);
                sheet.addCell(authTime);
                sheet.addCell(address);
               }
           workbook.write();
           workbook.close();
           outputStream.close();
          }catch(Exception e){
               e.printStackTrace();
               throw new Exception("导出数据出错!");
          }
     }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐