zoukankan      html  css  js  c++  java
  • 根据模板导出excel或者word

      //所需依赖

    <dependency> <groupId>net.sf.jxls</groupId> <artifactId>jxls-core</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>org.jxls</groupId> <artifactId>jxls</artifactId> <version>2.4.7</version> </dependency> <dependency> <groupId>org.jxls</groupId> <artifactId>jxls-jexcel</artifactId> <version>1.0.7</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>RELEASE</version> </dependency>
    @RequestMapping("/exportExcelqymx")
      public void exportExcelqymx(HttpServletRequest request, HttpServletResponse response,String monitorDate,String RegionCode,String MaintainEntName,String entid) {
        try {
          Date date = new Date();
          Calendar instance = Calendar.getInstance();
          instance.setTime(date);
          //实现日期减一操作,昨天
          instance.add(Calendar.DATE, -1);
          SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
          //定义查询日期
          monitorDate = dateFormat.format(instance.getTime());
        
          List list = new ArrayList();
          SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日");
          String format = simpleDateFormat.format(instance.getTime());
          List<ExcelImpl> findT_data_entpointlxmx = tdataentpointlxdwmxService.findT_data_entpointlxmx(monitorDate, RegionCode, MaintainEntName, entid, 1, 99999999);
          Map resultMap = new HashMap();
          resultMap.put("month",format);
          int index = 0;
          for(int i=0;i<findT_data_entpointlxmx.size();i++) {
              ExcelImpl excelImpl = findT_data_entpointlxmx.get(i);
              String maintainEntName = excelImpl.getMaintainEntName();
                  Map resMaps = new HashMap();
                  resMaps.put("index", (index+1)+"");
                  resMaps.put("qxmc", excelImpl.getRegionName());
                  resMaps.put("ywmc", excelImpl.getMaintainEntName());
                  resMaps.put("qymc", excelImpl.getEntName());
                  resMaps.put("jcdid", excelImpl.getPointId());
                  resMaps.put("jcdmc",  excelImpl.getPointName());
                  resMaps.put("mn", excelImpl.getMn());
                  index++;
                  list.add(resMaps);
            }
          //数据列表
          resultMap.put("BodyList", list);
          //其他数据
          resultMap.put("DateTime", DateUtil.getYearStr() + "年" + DateUtil.getMonthStr() + "月" + DateUtil.getDayOfMonthStr() + "日");
          //模板文件位置
          String filepath = templatePath + "excel_lxqyqd.xlsx";
          //导出文件名字
          String newFileName = "表4_" + DateUtil.getMonth() + "月" + DateUtil.getDayOfMonthStr() + "日离线企业清单.xlsx";
          kbToCreateExcels(request, response, resultMap, filepath, newFileName);
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        }
    
        private void kbToCreateExcel(HttpServletRequest request, HttpServletResponse response, Map resultMap,
                String filepath, String newFileName) throws InvalidFormatException {
            XLSTransformer transformer = new XLSTransformer();
    
            InputStream is = null;
            try {
                is = new BufferedInputStream(new FileInputStream(filepath));
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
    
            // InputStream, List, List, String, Map, int
            HSSFWorkbook workBook = (HSSFWorkbook) transformer.transformXLS(is, resultMap);    //操作Excel2003以前(包括2003)的版本,扩展名是.xls
    // XSSFWorkbook workBook = (XSSFWorkbook) transformer.transformXLS(is, resultMap); //是操作Excel2007的版本,扩展名是.xlsx saveWorkbook(workBook, response, newFileName.trim());
    try { is.close(); } catch (IOException e) { e.printStackTrace(); } } private void saveWorkbook(HSSFWorkbook resultWorkbook, HttpServletResponse response, String fileName) { try { response.setHeader("content-disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); response.setContentType("application/msexcel"); OutputStream os = response.getOutputStream(); resultWorkbook.write(os); os.flush(); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

    excel模板:

     

    参考博客地址:https://www.freesion.com/article/2517161403/

  • 相关阅读:
    C#使用spire.doc对word文档表格进行数据填充(主要针对word插入图像)
    FPGA学习——Xilinx Vivado 实现led流水灯详解
    Xilinx ZYNQ 7000+Vivado2015.2系列(四)之GPIO的三种方式:MIO、EMIO、AXI_GPIO
    清《己亥杂诗》
    FPGA编程语言VHDL OR Verilog
    tar解压包的时候出现错误 gzip: stdin: not in gzip format
    String和List相互转换
    vue:自定义验证form表单中的数组
    qt 数据库连接配置 qt oracle驱动自行编译配置
    不安装oracle client 客户端 Qt连接oracle qocidriver
  • 原文地址:https://www.cnblogs.com/bxbo/p/14636956.html
Copyright © 2011-2022 走看看