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();
    }

 

posted @ 2018-01-16 16:33  *眉间缘*  阅读(267)  评论(0编辑  收藏  举报