Java代码导出Excel表格,并且可以合并单元格

1、引入POI的jar包依赖

Java代码导出Excel表格,并且可以合并单元格_第1张图片

2.工具类代码:

public class ExcelUtil {

   /**
     * 导出Excel
     * @param sheetName sheet名称
     * @param title 标题
     * @param values 内容
     * @param wb HSSFWorkbook对象
     * @return
     */
    public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb,String mergeCell,Integer cellNum ){

        // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
        if(wb == null){
            wb = new HSSFWorkbook();
        }

        // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);

        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        //声明列对象
        HSSFCell cell = null;
        
        if (StringUtils.isNotEmpty(mergeCell)) {
           //合并单元格
            CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,cellNum);//起始行,结束行,起始列,结束列
            sheet.addMergedRegion(callRangeAddress);
            cell = row.createCell(0);
            cell.setCellValue(mergeCell);
            cell.setCellStyle(style);
            row = sheet.createRow(1);
         // HSSFCell cell = null;
            //创建标题
            for(int i=0;i 
  

 

3.控制器代码:

@RequestMapping(value = "/manage/enroCount/export")
@ResponseBody
public void enroCountExport(HttpServletRequest request, HttpServletResponse response) throws Exception {
   
    String aguserId = request.getParameter("aguserId"); 
    String start = request.getParameter("start"); 
    String end = request.getParameter("end"); 
    Aguser aguser = aguserService.findById(aguserId);
    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd"); //加上时间
    Date startDate = null;
   Date endDate = null;
   
    if (org.apache.commons.lang.StringUtils.isNotBlank(start)) {
       
       startDate = sDateFormat.parse(start); 
    } 
    if(org.apache.commons.lang.StringUtils.isNotBlank(end)) { 
       endDate = sDateFormat.parse(end); 
   }
   // 获取数据
   //List list = aguserReportService.findByAgent(agent, startDate, endDate);
   //获取数据
   List list = userService.findEnroCount(aguserId,startDate,endDate);
   

   // excel标题
   String[] title = { "学生姓名", "手机号", "身份证号", "地址", "课程名称", "课程付费状态"};

   // excel文件名
   String fileName = "代理商"+aguser.getUserName()+"的招生信息统计表" + System.currentTimeMillis() + ".xls";
   // CellRangeAddress callRangeAddress = new
   // CellRangeAddress(0,0,0,6);//起始行,结束行,起始列,结束列

   // sheet名
   String sheetName = "招生信息统计表";
   String[][] content = new String[list.size()][title.length];
   String state = "";
   for (int i = 0; i < list.size(); i++) {
      // content[i] = new String[title.length];
      User user = list.get(i);
      if(null != user.getState() && true == user.getState()){
         state="已付费";
      }else {
         
         state = "未付费";
      }
      /*
       * Date date = obj.getReportDate(); content[i][0] =
       * sDateFormat.format(date);
       */
      
      content[i][0] = user.getUserName();
      content[i][1] = user.getPhone() + "";
      content[i][2] = user.getCardNo() + "";
      content[i][3] = user.getAddress() + "";
      content[i][4] = user.getTitle();
      content[i][5] = state;
   }
   //合并单元格的内容
   String mergeCell = "代理商"+aguser.getUserName()+"招生总人数:"+list.size()+" 人";
   Integer cellNum = 5;//合并单元格6列
   // 创建HSSFWorkbook
   // HSSFWorkbook wb1 = new HSSFWorkbook();
   HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null,mergeCell,cellNum);
   // File errFile = new File(CoinConfig.RESOURCE_BASIC_PATH+"/"
   // +fileName);
   // 响应到客户端
   try {
      this.setResponseHeader(response, fileName);
      OutputStream os = response.getOutputStream();
      // OutputStream os = new FileOutputStream(errFile);
      wb.write(os);
      os.flush();
      os.close();
   } catch (Exception e) {
      e.printStackTrace();
   }
}




// 发送响应流方法
public void setResponseHeader(HttpServletResponse response, String fileName) {
   try {
      try {
         fileName = new String(fileName.getBytes(), "ISO8859-1");
      } catch (UnsupportedEncodingException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      response.setContentType("application/octet-stream;charset=ISO8859-1");
      response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
      response.addHeader("Pargam", "no-cache");
      response.addHeader("Cache-Control", "no-cache");
   } catch (Exception ex) {
      ex.printStackTrace();
   }
}

 

 

4.HTML代码:

 

Java代码导出Excel表格,并且可以合并单元格_第2张图片

5.JS代码:

你可能感兴趣的:(jQuery,后台管理)