zoukankan      html  css  js  c++  java
  • poi导出excel

    poi前台页面

        <div class="layui-col-md2">
                    <a class="waves-effect waves-button" href="javascript:;" onclick="exportAction()"><button class="layui-btn" style="transform: translateY(-3px);"
                                data-type="reload">导出签到表格</button></a>
                </div>    

    js代码:

    <script>
        //导出Excel文件
        function exportAction(){
            window.open("<%=basePath%>/teach/export2");//controller调用方法的路径
        }
    </script>   

    后台:数据库链接工具类

    public class DbUtil {
        private String dbUrl="jdbc:mysql://localhost:3306/basepro";
        private String dbUserName="user";
        private String dbPassword="user";
        private String jdbcName = "com.mysql.jdbc.Driver";
        
            public Connection getCon() throws Exception {
                Class.forName(jdbcName);
                Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
                return con;
            }
    
            public void closeCon(Connection con) throws Exception {
                if (con != null) {
                    con.close();
                }
            }
    }

    Excel工具类:

    public class ExcelUtil {
          public static void fillExcelData(ResultSet rs, Workbook wb, String[] headers) throws Exception {
                int rowIndex = 0; //定义行的初始值
                Sheet sheet = wb.createSheet(); //创建sheet页
                Row row = sheet.createRow(rowIndex++); //行数自增+1
                //将头信息填进单元格
                for (int i = 0; i < headers.length; i++) {
                    row.createCell(i).setCellValue(headers[i]);
                }
    
    
                while (rs.next()) {
                    row = sheet.createRow(rowIndex++); //增加行数
                    System.out.println(row);
                    for (int i = 0; i < headers.length; i++) { // 添加内容
                        row.createCell(i).setCellValue(rs.getObject(i + 1).toString());
                    }
                }
            }
    }

    创建excel工具类

    public class ResponseUtil {
        public static void write(HttpServletResponse response, Object o) throws Exception {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.println(o.toString());
            out.flush();
            out.close();
        }
        
        public static void export(HttpServletResponse response, Workbook wb, String fileName) throws Exception{
             //设置头  固定格式
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1"));
            
            response.setContentType("text/html;charset=utf-8");
            
            OutputStream out = response.getOutputStream();
            wb.write(out);
            out.flush();
            out.close();
        }
    
    }

    controller:

        @SuppressWarnings("static-access")
            @ApiOperation(value = "导出Excel")
            @RequiresPermissions("upms:system:export")
            @RequestMapping(value = "/export", method = RequestMethod.GET)
            @ResponseBody
            public String export(HttpServletResponse response,String sdate,String edate) throws Exception {
                ExportDao exportDao = new ExportDao();
                DbUtil dbUtil = new DbUtil();
                Connection con = null;
                ExcelUtil excelUtil = new ExcelUtil();
                try {
                    con = dbUtil.getCon();
                    Workbook wb = new HSSFWorkbook();
                    String headers[] = {"编号","学号","签到时间", "签到日期", "用户名","所属公司","职业","分组",};
                    
                    ResultSet rs = exportDao.exportSign(con,sdate,edate);
                    excelUtil.fillExcelData(rs, wb, headers);
                    ResponseUtil.export( response, wb, "签到管理.xls");
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    try {
                        dbUtil.closeCon(con);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                return null;
    
            }

    dao层:

    public class ExportDao {
        public ResultSet exportSign(Connection con, String sdate, String edate) throws Exception{
            
            String sql="select s.id,u.stunum, FROM_UNIXTIME(s.signtime/1000),s.signdate,u.nickname,u.company,u.job,u.usergroup from teach_sign s join sys_user u on s.user_id=u.id ";
            if(sdate!=null&&sdate!=""){
                if(edate!=null&&edate!=""){
                     sql+=" where s.signdate>='"+sdate+"'  and s.signdate<='"+edate+"' ";
                }else{
                    sql+=" where s.signdate>='"+sdate+"' ";
                }    
            }else{
                if(edate!=null&&edate!=""){
                    sql+=" where s.signdate<='"+edate+"' ";
                }else{
                    
                }
            }
            StringBuffer sb = new StringBuffer(sql);
            PreparedStatement pstmt = con.prepareStatement(sb.toString());
            return pstmt.executeQuery();
        }
  • 相关阅读:
    C# 中类重写 ToString 方法
    虚方法(virtual)和抽象方法(abstract)的区别
    C#的扩展方法学习
    C# .NET 和.NET Framework区别
    C#值类型和引用类型
    抽象和接口的区别和使用
    什么是委托?
    什么是继承?
    设计模式:单一职责原则
    Java 13,最新最全新特性解读
  • 原文地址:https://www.cnblogs.com/NCL--/p/8297352.html
Copyright © 2011-2022 走看看