1. 程式人生 > >源頭質量 PageHelper(分頁),導出功能

源頭質量 PageHelper(分頁),導出功能

.get strong ktr try 一個 mms XP 密碼 return

今天星期五,本來想直接關電腦走人的,但想想自己弄出來的,寫寫留個記憶吧。兩個功能 導出 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(分頁),導出功能