首页 javaWEB springmvc_java导出excel的学习方法

springmvc_java导出excel的学习方法

java ssh导出excel 这个有做过 今天用了springmvc的框架导出excel 其实大同小益 改下几个参数就…

java ssh导出excel

这个有做过

今天用了springmvc的框架导出excel

其实大同小益

改下几个参数就行了。。

导出代码

/**springmvc_java导出excel的学习方法
       * 设置表头
       *
       * @param wb
       * @return
       * @throws Exception
       */
     public static HSSFCellStyle stylecreateTitle(HSSFWorkbook wb, int layer) throws Exception {
          HSSFCellStyle style = wb.createCellStyle();
          // style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
          // style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
          // style.setBorderRight(HSSFCellStyle.BORDER_THIN);
          // style.setBorderTop(HSSFCellStyle.BORDER_THIN);
          if (layer == 1) {
           /**
           * 设置背景颜色
           */
           style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
           style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
           style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
           /**
           * 设置字体
           */
           HSSFFont f = wb.createFont();
           f.setFontName("宋体"); // 字体
           f.setColor(HSSFColor.WHITE.index);// 字体颜色
           f.setFontHeight((short) 300); // 字高
           f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗
           style.setFont(f);
          } else if (layer == 2) {
           style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);// 背景
           style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
           style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
           HSSFFont font = wb.createFont();
           font.setFontName("宋体");
           font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗
           style.setFont(font);
          } else if (layer == 3) {
           style.setFillForegroundColor(HSSFColor.WHITE.index);// 背景

           style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
          } else {

          }

          return style;
       }
/** springmvc_java导出excel的学习方法
     * 导出excel 表格
     * @author: lyq
     * @date: Jun 5, 2014 10:35:20 AM
     * @return
     */
    @RequestMapping(value = "downExcel.php")
    public  String  downExcel(ModelMap model,HttpServletRequest request,HttpServletResponse response){
        List<CrmManager> list= crmService.findCrmManagerList(null);
        try {
            //第一步,创建一个webbook,对应一个Excel文件
            HSSFWorkbook wb = new HSSFWorkbook();
            //第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet sheet = wb.createSheet("crm客户管理");
            sheet.setDefaultColumnWidth((short) 15); // 设置每列默认宽度
            sheet.setDefaultRowHeightInPoints(15);
             //样式
            HSSFCellStyle style = wb.createCellStyle();
            style.setAlignment(HSSFCellStyle.BORDER_HAIR); //创建一个居中格式

            //第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
            //标题
            HSSFRow row = sheet.createRow((int)0);// 行和列索引都是从0开始
            row.setHeightInPoints(35);
            HSSFCell cell = row.createCell((short) 0);
            cell.setCellStyle(stylecreateTitle(wb,1));
            cell.setCellValue(new HSSFRichTextString("crm客户管理"));

            HSSFRow row2 = sheet.createRow((int)1);// 行和列索引都是从0开始
            row2.setHeightInPoints(25);
            HSSFCell cell2 = row2.createCell((short) 0);
            cell2.setCellValue("客户ID"); cell2.setCellStyle(style);         cell2.setCellStyle(stylecreateTitle(wb, 2));
            cell2 = row2.createCell((short) 1);
            cell2.setCellValue("客户名"); cell2.setCellStyle(style);        cell2.setCellStyle(stylecreateTitle(wb, 2));
            cell2 = row2.createCell((short) 2);
            cell2.setCellValue("客户类型"); cell2.setCellStyle(style);       cell2.setCellStyle(stylecreateTitle(wb, 2));
            cell2 = row2.createCell((short) 3);
            cell2.setCellValue("客户电话"); cell2.setCellStyle(style);         cell2.setCellStyle(stylecreateTitle(wb, 2));
            cell2 = row2.createCell((short) 4);
            cell2.setCellValue("客户状态"); cell2.setCellStyle(style);          cell2.setCellStyle(stylecreateTitle(wb, 2));
            cell2 = row2.createCell((short) 5);
            cell2.setCellValue("创建时间"); cell2.setCellStyle(style);        cell2.setCellStyle(stylecreateTitle(wb, 2));
            cell2 = row2.createCell((short) 6);
            cell2.setCellValue("放出时间"); cell2.setCellStyle(style);           cell2.setCellStyle(stylecreateTitle(wb, 2));
            cell2 = row2.createCell((short) 7);
            cell2.setCellValue("跟入时间"); cell2.setCellStyle(style);           cell2.setCellStyle(stylecreateTitle(wb, 2));
            cell2 = row2.createCell((short) 8);
            cell2.setCellValue("跟入人"); cell2.setCellStyle(style);           cell2.setCellStyle(stylecreateTitle(wb, 2));
            cell2 = row2.createCell((short) 9);
            cell2.setCellValue("放出人"); cell2.setCellStyle(style);   cell2.setCellStyle(stylecreateTitle(wb, 2));
            int i =0;
            for(CrmManager p:list){
                row = sheet.createRow((int)i+2);
                row.setHeightInPoints(20);
//              GamePayDetailEntity gs =gamePayDetailEntitys.get(i);
                //第四步,创建单元格,并设置值
                row.createCell((short)0).setCellValue(p.getId());
                row.createCell((short)1).setCellValue(p.getCrmName());
                row.createCell((short)2).setCellValue(p.getCrmType());//p.getRefererSiteId().toString()
                row.createCell((short)3).setCellValue(p.getCrmPhone());
                row.createCell((short)4).setCellValue(p.getCrmStatus());
                row.createCell((short)5).setCellValue(p.getCrmCreatetime());
                System.out.println(p.getCrmEndtime());
                 if(p.getCrmEndtime()!=null)
                row.createCell((short)6).setCellValue(p.getCrmEndtime().toString());
                 if(p.getCrmStarttime()!=null)
                row.createCell((short)7).setCellValue(p.getCrmStarttime().toString());
                 if(p.getCrmStartname()!=null)
                row.createCell((short)8).setCellValue(p.getCrmStartname().toString());
                 if(p.getCrmEndname()!=null)
                row.createCell((short)9).setCellValue(p.getCrmEndname().toString());
//              cell = row.createCell((short)3);
//              cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu.getBrithday()));
                i++;
            }
            Region region =new Region();
            region.setColumnFrom((short) 0);
            region.setColumnTo((short) 11);
            region.setRowFrom(0);
            region.setRowTo(0);
            sheet.addMergedRegion(region);

            response.reset();
            response.setContentType("application/x-download;charset=GBK");
            response.setHeader("Content-Disposition", "attachment;filename=USER_SITE_"+System.currentTimeMillis()+".xls");
            wb.write(response.getOutputStream());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        return  null;
    }

java导出excel的学习方法 大概就是这样

免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。

为您推荐

nodejs 整理记录

nodejs 整理记录

下载包 https://blog.csdn.net/m0_59878114/article/details/120274...
websocket测试html

websocket测试html

<!DOCTYPE html> <html> <head> <meta cha...
bigdemical两个数比较大小

bigdemical两个数比较大小

/*int result = bigdemical1.compareTo(bigdemical2) result = -...
Beetl2.7 中文文档

Beetl2.7 中文文档

Beetl目前版本是2.7.23,相对于其他java模板引擎,具有功能齐全,语法直观,性能超高,以及编写的模板容易维护等...
纯CSS实现多个便签在一行展示,拖动滚动

纯CSS实现多个便签在一行展示,拖动滚动

div <h2>请注意需要在移动端预览,PC端拖拽无效果</h2> <div class=...
返回顶部