Excel导出(HSSFWorkbook )

    
private void outPut(List smUserList,HttpServletResponse response) {
      String[][] user = {    
            {"用户名", "username"}, {"姓名", "name"},{"性别", "gender"},
            {"邮箱", "email"}, {"工号", "jobnumber"}, {"生日", "birthday"},
            {"描述", "description"}, {"用户头像", "imageurl"},  {"创建时间", "createtime"},
            {"备忘", "remark"}, {"排序号", "sortorder"}, {"用户类型", "usertype"}, 
            {"单位", "unit"}, {"部门", "dept"}, {"角色", "role"}, {"账号类型", "accounttype"}, {"账号有效期", "existtime"}};
      try {
         HSSFWorkbook workbook = new HSSFWorkbook();
         HSSFSheet sheet=workbook.createSheet("SmUser");

         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         // 标题行
         buildHead(sheet, user, workbook);//设置表头标题
         HSSFRow row = null;//行
         HSSFCell cell=null;//列
         SmUser smUser = null;
         for(int i=0;i;i++) {
            row=sheet.createRow(i+1);//设置行(由于第0行表头已设置,所以从第一行开始设置值)
            smUser = smUserList.get(i);
            for(int j=0;jlength;j++){
               Method method = getColumnValue(SmUser.class, user[j][1]);//获取字段名及get方法
               Object o = method.invoke(smUser, null);//通过get方法取值
               if (o instanceof Date) {
                  o = formatter.format(o);//日期时间需要转换
               }
               cell = row.createCell(j);//设置列
               cell.setCellValue(o == null ? "" : String.valueOf(o)); //设置值  //用三元运算符判断o是否为空 
            }
         }
         responseSet(response, "用户信息" + TimeUtil.nowTime());//设置Excel名称及文件保存位置
         OutputStream os = response.getOutputStream();
         workbook.write(os);//写入到Excel中
         os.flush();
         os.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
 
  
public static void buildHead(HSSFSheet sheet,
                             String[][] headArr, HSSFWorkbook workbook) {
    // 标题行
    HSSFRow headRow = sheet.createRow(0);
    for (int i = 0; i < headArr.length; i++) {
        HSSFCellStyle cellStyle = getHeadCellStyle(workbook);
        HSSFCell cell = headRow.createCell(i);
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell.setCellValue(headArr[i][0]);
        cell.setCellStyle(cellStyle);
    }
}

 
  
public static Method getColumnValue(Class cla,
                                    String column) throws NoSuchMethodException {
    String methodStr = "get" + StringUtil.toUpperCaseFirst(column);
    Method method = cla.getMethod(methodStr, null);
    return method;
}

 
  
/**
 * 字符串首字母大写
 * 
 * @param str
 * @return
 */
public static String toUpperCaseFirst(String str) {
   StringBuilder sb = new StringBuilder(str);
   sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
   return sb.toString();
}

 
  
public static void responseSet(HttpServletResponse response,
                               String title) throws UnsupportedEncodingException {
    response.reset();
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/vnd.ms-excel;charset=utf-8");
    response.setHeader("Content-Disposition", "attachment;filename="
            + URLEncoder.encode(title + ".xls", "UTF-8"));
}

你可能感兴趣的:(Excel导出(HSSFWorkbook ))