源头质量 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. 类库选 ...
随机推荐
- Android 验证APK签名对比
最近OTT制定产品,其中有一条需求是只能安装自己公司签名的APK,所以在网上找了相关资料,最后总结功能实现如下: 1.签名错误码frameworks/base/core/java/android/co ...
- adb 无线连接手机设备
连接语法: $ adb connect ip:port 断开连接: $ adb disconnect ip:port 可能遇到问题:unable to connect to 192.168.199.2 ...
- POJ3258 River Hopscotch(二分最大化最小值)
题目链接:http://poj.org/problem?id=3258 题意:给n个石头,起点和终点也是两个石头,去掉这石头中的m个,使得石头间距的最小值最大. 思路:二分石头间的最短距离,每次贪心地 ...
- Idea实用小Tips
设置keymap 自己根据习惯选择keymap(键位) 插件安装 ###省去set.get方法以及基于注解的日志框架 lombok plugin ###找bug用的 FindBugs-IDEA ### ...
- windows10 +ubuntu双系统
1,安装之前的准备: 制作启动盘 确定给ubuntu多少分区并且清理为free状态 确定电脑的开机引导方式,传统方式引号和uefi引导并不一样,因此我们需要根据引导方式选择新系统制作什么样的启动盘 在 ...
- Spring MVC 中使用properties文件
首先要搭建Spring mvc的环境,然后开始properties文件里的配置: 第一步:在springcontext中注入properties,具体路径自己调整 <bean id=" ...
- 题解【UVA10054】The Necklace
题目描述 输入格式 输出格式 题意简述 有一种由彩色珠子连接而成的项链.每个珠子的两半由不同颜色组成.如图所示,相邻两个珠子在接触的地方颜色相同.现在有一些零碎的珠子,需要确认它们是否可以复原成完整的 ...
- vue-cli 3 脚手架搭建(create)
地址:https://cli.vuejs.org/zh/guide/ 安装步骤: 提示:node 版本要 8.9+ 两种方式: (1) npm install -g @vue/cli (2) yarn ...
- 在java中使用FFmpeg处理视频与音频
FFmpeg是一个非常好用的视频处理工具,下面讲讲如何在java中使用该工具类. 一.首先,让我们来认识一下FFmpeg在Dos界面的常见操作 1.拷贝视频,并指定新的视频的名字以及格式 ffmpeg ...
- Python爬虫连载7-cookie的保存与读取、SSL讲解
一.cookie的保存与读取 1.cookie的保存-FileCookie.Jar from urllib import request,parse from http import cookieja ...