源头质量 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"/>
<a id="serach" class="easyui-linkbutton" iconCls="icon-search" onclick="serach();">查询</a>
<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(分页),导出功能的更多相关文章
- java 分页导出百万级数据到excel
最近修改了一个导出员工培训课程的历史记录(一年数据),导出功能本来就有的,不过前台做了时间限制(只能选择一个月时间内的),还有一些必选条件, 导出的数据非常有局限性.心想:为什么要做出这么多条件限制呢 ...
- Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件
前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...
- PageHelper分页插件的使用
大家好!今天写ssm项目实现分页的时候用到pageHelper分页插件,在使用过程中出现了一些错误,因此写篇随笔记录下整个过程 1.背景:在项目的开发的过程中,为了实现所有的功能. 2.目标:实现分页 ...
- 记录pageHelper分页orderby的坑
pageHelper的count查询会过滤查询sql中的order by条件! pageHelper分页功能很强大,如果开启count统计方法,在你执行查询条件时会再执行一条selet count(* ...
- pageHelper分页
引入jar包 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pag ...
- SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件
原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...
- 如何在实际项目中使用PageHelper分页插件
PageHelper是一个分页插件,能够简单快速的帮助开发人员完成常见的分页功能,你只需要简单的使用两行代码就可以完成一个分页效果- 最近做一个科创项目,使用Maven+SSM的环境,有分页的功能,于 ...
- 【HOW】如何限制Reporting Services报表导出功能中格式选项
Reporting Services报表导出功能中缺省会提供多种导出格式选项,但很多情况下不需要全部的格式选项,因此需要对这些选项进行限制.下面我们以SQL Server 2008 R2为例来说明对这 ...
- Atitit.excel导出 功能解决方案 php java C#.net版总集合.doc
Atitit.excel导出 功能解决方案 php java C#.net版总集合.docx 1.1. Excel的保存格式office2003 office2007/2010格式1 1.2. 类库选 ...
随机推荐
- web前端常用库
项目中可能用到的web前端库(持续记录): 1.轻量化货币库:https://github.com/openexchangerates/accounting.js ,美元形式.欧元形式等 2.时间 ...
- MyEclipse 安装 emmet 插件
1.在线安装 地址:http://download.emmet.io/eclipse/updates/ 安装完成后重新启动myeclipse 2.离线安装 下载jar包:https://github. ...
- 剑指offer 面试题40. 最小的k个数
O(N)划分法,注意这个方法会改变原数据(函数参数是引用的情况下)!当然也可以再定义一个新容器对其划分 要求前k小的数,只要执行快排划分,每次划分都会把数据分成大小两拨.直到某一次划分的中心点正好在k ...
- os 和shutil模块的使用方法
1.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法. 1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 2.返回指定目录下的所有文件 ...
- JS高级---函数作为返回值使用
函数作为返回值使用 function f1() { console.log("f1函数开始"); return function () { console.log("函数 ...
- ubuntu18.04双卡机安装ubidia驱动遇到的坑
在ubuntu 18的软件更新中选择英伟达驱动进行安装,输入nvidia-smi总是提示缺少驱动,未驱动之类的报错. 何解? 最初以为安装系统的问题,重装ubuntu也未果. 最后查了很多资料,找到一 ...
- ssh连不上的问题
新安装的ubuntu,想要ssh远程连接,发现连接不上,何解? 答 : 在ubuntu上安装ssh. sudo apt-get install openssh-server
- ES6新的数据类型 generato,在AJAX中的应用
1.next()方法会执行generator的代码,然后,每次遇到yield x;就返回一个对象{value: x, done: true/false},然后“暂停”.返回的value就是yield的 ...
- TCP协议的3次握手与4次挥手过程【深度详解】
一.前沿 尽管TCP和UDP都使用相同的网络层(IP),TCP却向应用层提供与UDP完全不同的服务.TCP提供一种面向连接的.可靠的字节流服务.面向连接意味着两个使用TCP的应用(通常是一个客户和一个 ...
- 如和针对CPU时间百分比,Mem使用bytes,以及Network RecvBytes/SendBytes指标性能压测数据可视化
设计思路:通过jmeter5.1压测获取cpu,Mem,Network的压测指标数据利用pandas+openpyxl进行数据可视化: 涉及添加jar包:下载地址:https://files.cnbl ...