源头质量 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. 类库选 ...
随机推荐
- 后台异常 - org.apache.jasper.JasperException
问题描述 org.apache.jasper.JasperException: Unable to compile class for JSP 问题原因 tomcat6不支持jdk1.8版本
- (转)DNS使用的是TCP协议还是UDP协议
转自:DNS使用的是TCP协议还是UDP协议 DNS同时占用UDP和TCP端口53是公认的,这种单个应用协议同时使用两种传输协议的情况在TCP/IP栈也算是个另类.但很少有人知道DNS分别在什么情况下 ...
- jsp中的javascript的$(document).ready( function() { $("#loginForm").validate()
转自:https://bbs.csdn.net/topics/392459787?list=71147533 下面是jsp页面中的JavaScript代码 $(document).ready( fun ...
- C#对config.ini文件进行读取和修改
C#对config.ini文件进行读取和修改: public partial class Patrolcar : Form之后可以加入如下类: #region public class IniFile ...
- MANIFEST.MF详解及配置的注意事项
一.详解 打开Java的JAR文件我们经常可以看到文件中包含着一个META-INF目录, 这个目录下会有一些文件,其中必有一个MANIFEST.MF,这个文件描述了该Jar文件的很多信息,下面将详细介 ...
- c数据结构 -- 线性表之 复杂的链式存储结构
复杂的链式存储结构 循环链表 定义:是一种头尾相接的链表(即表中最后一个结点的指针域指向头结点,整个链表形成一个环) 优点:从表中任一节点出发均可找到表中其他结点 注意:涉及遍历操作时,终止条件是判断 ...
- Linux - cron - cron 表达式
概述 之前 cron 的补充 这次介绍下 表达式 背景 之前有说过 cron 这次说下 表达式 1. 准备 环境 os centos7 2. 模板文件 文件 位置 /etc/crontab 作用 系统 ...
- c++基础语法规则
1,c++存储类:定义函数或者变量的生命周期 auto 关键字用于两种情况:声明变量时根据初始化表达式自动推断该变量的类型.声明函数时函数返回值的占位符. register 存储类用于定义存储 ...
- Django - DRF自带的token认证和JWT区别
问题重现 当查看DRF 文档时发现DRF内置的token是存储在数据库里,这和我在网上搜索资料时认识的token-based authentication有出入. from rest_framewor ...
- [vue学习]快速搭建一个项目
安装node.js 官网:https://nodejs.org/en/ 淘宝NPM镜像(npm是外网,用国内代理下载安装贼快) $ npm install -g cnpm --registry=htt ...