源头质量 PageHelper(分页),导出功能
2024-10-08 10:32:11
今天星期五,本来想直接关电脑走人的,但想想自己弄出来的,写写留个记忆吧。两个功能 导出 和 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;
}
}
是不是很简单呀。哈哈,下班了,以后看到了再改改。感觉写的有点草率
最新文章
- Python dir
- PostgreSQL的OGG -- bucardo
- Xcode插件安装
- Android中SQLite下 Cursor的使用。
- (转)Spring的编程式事务例子
- Android-PullToRefresh下拉刷新库基本用法
- SymmetricDS文档翻译--【Chapter 3. 具体配置(Configuration)[section C]】
- JS的预编译和执行顺序 详析
- Angular2 + NativeScript 跨平台开发笔记(一)
- BZOJ 1497: [NOI2006]最大获利(最大权闭合子图)
- leanote使用本地账户时,去掉待同步的小红点
- get_k_data 接口文档 全新的免费行情数据接口
- kafka 发送确认参数acks的几种模式
- 基于 Consul 实现 MagicOnion(GRpc) 服务注册与发现
- Troubleshooting SQL Server RESOURCE_SEMAPHORE Waittype Memory Issues
- spring相关jar包的作用讲解(转)
- c需要注意的细节
- 【Java面试题】26 多线程有几种实现方法?同步有几种实现方法? 当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法?
- mybatis家族
- node操作mongoDB数据库的最基本例子
热门文章
- EF CodeFirst简介、默认约定、数据库初始化策略
- css样式读取
- 解决无法将“babel”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。
- JavaWeb项目中的Servlet
- YAML(YML)语法详解
- AC3 mantissa quantization and decoding
- 【C语言】请输入一个n(n<;=10)并输出一个n行n列的杨辉三角
- node.js 和 yarn 安装
- 搭建私人通讯录/日历同步服务_使用cardDAV/calDAV服务
- spring 的异步处理