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

今天星期五,本来想直接关电脑走人的,但想想自己弄出来的,写写留个记忆吧。两个功能 导出 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;
    }
}

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

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

时间: 2024-08-29 07:06:02

源头质量 PageHelper(分页),导出功能的相关文章

SpringBoot2.0系列教程(七)Springboot框架添加PageHelper分页查询功能

Hello大家好,本章我们添加PageHelper分页查询功能.另求各路大神指点,感谢 一:什么是PageHelper PageHelper是一款好用的开源免费的Mybatis第三方物理分页插件 物理分页 支持常见的 12 种数据库.Oracle,MySql,MariaDB,SQLite,DB2,PostgreSQL,SqlServer 等 支持多种分页方式 支持常见的RowBounds(PageRowBounds),PageHelper.startPage 方法调用,Mapper 接口参数调用

easyUI一览页面查询、导出功能

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <

数据报表导出功能改进

一.           前言 财务在每月月底做财务数据统计时,需要统计每月产品的出货单及退货单报表数据,故要求在每月月底要汇总当月的报表数据,每一季度或者半年也要相应的统计报表数据,并能把数据导出到excl表格,所以数据报表导出功能则是必然的.财务提供的excel表格字段繁多,汇总下关联到数据库表达到7张以上,故在进行出货单或者退货单报表查询之时,由于涉及到多张表关联,故每次查询都需要耗费相当多的时间和空间. 二.           实现 数据报表只需要按照起始时间和结束时间进行查询和导出.

SpringBoot整合Pagehelper分页插件

在web开发中,数据的分页是必不可少的.Pagehelper分页插件很强大,虽说平时我们不需要用到它的很多功能,但是了解下还是有必要的. 官网:https://pagehelper.github.io/ 注:在 MyBatis下使用. 一.Pagehelper分页插件介绍 原文地址:https://www.cnblogs.com/myitnews/p/12349655.html

利用Aspose.Cells完成easyUI中DataGrid数据的Excel导出功能

我准备在项目中实现该功能之前,google发现大部分代码都是利用一般处理程序 HttpHandler实现的服务器端数据的Excel导出,但是这样存在的问题是ashx读取的数据一般都是数据库中视图的数据,难免会含有方便操作的 主键ID这列的记录.现在项目需要在easyUI的DataGrid中显示的数据能全部导出Excel,包括DataGrid中的中文标题,其他的统统不 要. 完成该功能所需的工具和环境:Newtonsoft.Json序列化和反序列化类库.easyUI前端UI框架.HttpHandl

excel导出功能优化

先说说优化前,怎么做EXCEL导出功能的: 1. 先定义一个VO类,类中的字段按照EXCEL的顺序定义,并且该类只能用于EXCEL导出使用,不能随便修改. 2. 将查询到的结果集循环写入到这个VO类中. 3. 将这个VO类的数据集写入到EXCEL中. 缺点: 1.每次做一个功能的excel导出需要定义一个vo类,并且vo类不可随便变更. 2. 从数据库查询到结果集不能直接输出到excel,需要二次遍历写入到vo中. 3. excel导出的顺序必须与vo定义的字段顺序一致,并且输出vo中所有的字段

写了一个Windows API Viewer,提供VBA语句的导出功能。提供两万多个API的MSDN链接查询

世面上的API Viewer已经不少了,但给VBA用的几乎没有.我自己写了一个Windows API Viewer,提供VBA语句的导出功能,并可以提供两万多个API的MSDN链接查询. 环境需求:Windows 7.1 SP1及以上操作系统,安装.Net 4.5.2及以上框架. 下载地址:API Viewer v1.1 下载

【HOW】如何限制Reporting Services报表导出功能中格式选项

Reporting Services报表导出功能中缺省会提供多种导出格式选项,但很多情况下不需要全部的格式选项,因此需要对这些选项进行限制.下面我们以SQL Server 2008 R2为例来说明对这些选项进行限制的方法. 1. 打开报表服务配置文件:"C:\Program Files\Microsoft SQL Server\MSRS10_50.QUIST\Reporting Services\Report Server\ rsreportserver.config". 2. 在上述

Jxl、JxCell图表导出功能的实现

最近接触过许多报表导出功能,也用过多种工具进行导出功能的实现,但对于图表的导出一直没有仔细的去展开研究和探讨,直到最近略微整理了下这方面的需求和技术攻克. 首先导出excel功能的实现主要有JXL.JXCELL.POI等工具.目前只实现了JXL和JXCELL. JXL: 先介绍下JXL: jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有两套比较有影响的API可 供使用,一个是POI,一个是jExcelAPI.其中功能相对POI比较弱一点.但jExcelAPI对中文支持非常好