码迷,mamicode.com
首页 > 其他好文 > 详细

源头质量 PageHelper(分页),导出功能

时间:2018-04-13 19:06:42      阅读:452      评论:0      收藏:0      [点我收藏+]

标签:getpass   acl   string   validate   service   style   ges   mit   ora   

今天星期五,本来想直接关电脑走人的,但想想自己弄出来的,写写留个记忆吧。两个功能 导出 Mybatis的插件 PageHelper 分页

一,导出功能代码实现:这里是需要jar包的啊

<!--poi-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.14</version>
    </dependency>

 

前端:

<div>
        <form id="searchFrom">
            用户姓名:<input type="text" name="userName" class="easyui-validatebox"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a id="serach" class="easyui-linkbutton" iconCls="icon-search" onclick="serach();">查询</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a id="export" class="easyui-linkbutton" iconCls="icon-edit" onclick="exportFrom();">导出</a>
        </form>
    </div>

<!--js部分-->
//导出
    function exportFrom() {
        $("#searchFrom").attr({action: "${ctx}/user/exportExcel"}).submit();
    }

后台代码:

//导出
    @RequestMapping(value = "exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response,People p) {
        List<People> userInfo = userInfoService.findByConditions(p);
        // 第一步,创建一个webbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("用户信息");
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow(0);
        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        // 设置表头
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("账号");
        cell.setCellStyle(style);
        cell = row.createCell( 1);
        cell.setCellValue("密码");
        cell.setCellStyle(style);
        cell = row.createCell( 2);
        cell.setCellValue("真实姓名");
        cell.setCellStyle(style);
        cell = row.createCell(3);
        cell.setCellValue("性别");
        cell.setCellStyle(style);
        cell = row.createCell(4);
        cell.setCellValue("家庭住址");
        cell.setCellStyle(style);
        cell = row.createCell(5);
        cell.setCellValue("电话");
        cell.setCellStyle(style);
        cell = row.createCell(6);
        cell.setCellValue("工作");
        cell.setCellStyle(style);
        cell = row.createCell(7);
        cell.setCellValue("备注");
        cell.setCellStyle(style);



        for (int i = 0; i < userInfo.size(); i++) {
            row = sheet.createRow((int) i + 1);
            People people = userInfo.get(i);

            if (StringUtils.isNotEmpty(people.getUserName())) {
                row.createCell(0).setCellValue(people.getUserName());
            }

            if (StringUtils.isNotEmpty(people.getPassword())) {
                row.createCell(1).setCellValue(people.getPassword());
            }

            if (StringUtils.isNotEmpty(people.getRealName())) {
                row.createCell(2).setCellValue(people.getRealName());
            }

            if (StringUtils.isNotEmpty(people.getSex())) {
                row.createCell(3).setCellValue(people.getSex());
            }

            if (StringUtils.isNotEmpty(people.getAddress())) {
                row.createCell(4).setCellValue(people.getAddress());
            }
            row.createCell(5).setCellValue(people.getPhone());
            row.createCell(6).setCellValue(people.getJob());
            row.createCell(7).setCellValue(people.getBL01());
        }
        // 第六步,将文件配置
        try {
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("YYYYmmDDHHmmss");
            String fileName = sdf.format(d) + ".xls";
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);// 指定下载的文件名
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            OutputStream output = response.getOutputStream();
            wb.write(output);
            output.flush();
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

二,Mybatis的插件 PageHelper 分页

先说配置文件

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mappers/*.xml"/>
        <property name="typeAliasesPackage" value="cn.test.model"/>
<!--这里就是 PageHelper 配置--> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=oracle offsetAsPageNum=true pageSizeZero=true rowBoundsWithCount=true reasonable=true </value> </property> </bean> </array> </property> </bean>

Controller层:

    @ResponseBody
    @RequestMapping("/userList")
    public PageBean userListToJson(People userInfo, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        List<People> userInfoList = userInfoService.findAll(userInfo);
        int total = userInfoService.getTotal();
        PageBean pageBean = new PageBean();
        pageBean.setTotal(total);
        pageBean.setRows(userInfoList);
        return pageBean;
    }
PageBean 类:
package cn.test.model;

import java.util.List;

public class PageBean {
    private  int total;  //总数
    private List rows;  //数据集合

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }
}

是不是很简单呀。哈哈,下班了,以后看到了再改改。感觉写的有点草率

源头质量 PageHelper(分页),导出功能

标签:getpass   acl   string   validate   service   style   ges   mit   ora   

原文地址:https://www.cnblogs.com/xinxin-ting/p/8822579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!