当前位置:网站首页>SSM使用poi将数据导出到excel
SSM使用poi将数据导出到excel
2022-07-17 13:25:00 【老板娘来碗面】
1、引入poi依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>2、前端jsp页面
<span>
<button id="exportActivityAllBtn">导出excel</button>
</span>js代码
$("#exportActivityAllBtn").click(function () {
window.location.href="${pageContext.request.contextPath}/exportAllActivitys" ;
});3、controller层代码
@RequestMapping("exportAllActivitys")
public void exportAllActivitys(HttpServletResponse response) throws IOException {
//调用service的findAll()方法获取数据
//获取service层的获取所有数据方法即可
List<User> list = userService.findAll();
//用apache的poi创建excel文件,并把list写入excel文件中
//创建HSSFWorkbook对象,对应一个excel文件
HSSFWorkbook wb = new HSSFWorkbook();
//使用wb创建HSSFSheet对象,对应wb文件中的一页
HSSFSheet sheet = wb.createSheet("user列表");//设置页名
//使用sheet创建HSSFRow对象,对应sheet中的一行
HSSFRow row = sheet.createRow(0);//索引为0就是第一行
//使用row创建HSSFCell对象,对应row中的列
HSSFCell cell = row.createCell(0);//索引为0就是第一列
//往列里设置值
cell.setCellValue("id");
cell=row.createCell(1);
cell.setCellValue("姓名");
cell=row.createCell(2);
cell.setCellValue("账号");
cell=row.createCell(3);
cell.setCellValue("性别");
cell=row.createCell(4);
cell.setCellValue("出生日期");
cell=row.createCell(5);
cell.setCellValue("所属部门");
cell=row.createCell(6);
cell.setCellValue("状态");
cell=row.createCell(7);
//遍历ActivityList,创建Row
//先判断list有没有数据
if(list!=null && list.size()>0){
User user=null;//user拿到外面定义就不用每次循环都创建,效率更高
for (int i=0;i<list.size();i++){
user=list.get(i);
//每遍历出一个user,生成一行
row=sheet.createRow(i+1);
cell = row.createCell(0);
cell.setCellValue(user.getUserId());
cell = row.createCell(1);
cell.setCellValue(user.getUserName());
cell = row.createCell(2);
cell.setCellValue(user.getAccount());
cell = row.createCell(3);
cell.setCellValue(user.isGender());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String birthDay = simpleDateFormat.format(user.getBirthDay());
cell = row.createCell(4);
cell.setCellValue(birthDay);
cell = row.createCell(5);
cell.setCellValue(user.getDept().getDeptName());
cell = row.createCell(6);
cell.setCellValue(user.getState());
}
}
//改进后!!!!!!!!!!!!!!!!!!!!!
//设置响应类型 application/octet-stream表示excel文件
response.setContentType("application/octet-stream;charset=UTF-8");
//设置响应头 Content-Disposition打开方式 attachment以附件的形式
response.addHeader("Content-Disposition","attachment;filename=userList.xls");
OutputStream out = response.getOutputStream();
wb.write(out);
wb.close();
out.flush();//out由response生成的,tomcat会自己关闭,不能自己关闭,不flush可能会数据丢失
}边栏推荐
- 【PostgreSQL 】PostgreSQL 15对distinct的优化
- CPU负载与CPU使用率之区别
- 博弈论(depu)与投资(40/100)
- Three programming implementations to quickly determine whether the site is alive
- Thread pool principle
- LeetCode 2249. Count the number of grid points in the circle
- 开发第一个Flink应用
- LeetCode 2331. 计算布尔二叉树的值(树的遍历)
- 追根问底:Objective-C关联属性原理分析
- LeetCode 2249. 统计圆内格点数目
猜你喜欢

vulnhub inclusiveness: 1

Leetcode丑数题解

C serialport configuration and attribute understanding

Takeout ordering system based on wechat applet

手机键盘(模拟题)

Bidirectional NAT Technology

Attachment handling of SAP Fiori

37. Flex layout

Use testeract JS offline recognition picture text record

Mobile keyboard (simulation question)
随机推荐
【手写数字识别】基于Lenet网络实现手写数字识别附matlab代码
unity3d中的旋转
OpenCV编程:OpenCV3.X训练自己的分类器
[in vivado middle note ILA IP core]
分类任务中的类别不平衡问题
内核态和用户态
LeetCode 2331. Calculate the value of Boolean binary tree (tree traversal)
华为机试:连续出牌数量
AutoJs学习-动态解密
高数_第1章空间解析几何与向量代数__点到平面的距离
String type function transfer problem
【PostgreSQL 】PostgreSQL 15对distinct的优化
openfoam热流边界条件
从预测到决策,九章云极DataCanvas推出YLearn因果学习开源项目
新增、修改操作时自定义复杂逻辑校验-2022新项目
Detailed explanation of C language custom types
Vérification logique complexe personnalisée lors de l'ajout et de la modification - 2022 nouvel élément
企业远程办公如何更高效?
过拟合与欠拟合
37. flex布局