当前位置:网站首页>SSM uses POI to export data to excel
SSM uses POI to export data to excel
2022-07-19 10:52:00 【A bowl of noodles from the landlady】
1、 introduce poi rely on
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>2、 front end jsp page
<span>
<button id="exportActivityAllBtn"> export excel</button>
</span>js Code
$("#exportActivityAllBtn").click(function () {
window.location.href="${pageContext.request.contextPath}/exportAllActivitys" ;
});3、controller Layer code
@RequestMapping("exportAllActivitys")
public void exportAllActivitys(HttpServletResponse response) throws IOException {
// call service Of findAll() Method to get data
// obtain service All data acquisition methods of layer
List<User> list = userService.findAll();
// use apache Of poi establish excel file , And put list write in excel In file
// establish HSSFWorkbook object , Corresponding to one excel file
HSSFWorkbook wb = new HSSFWorkbook();
// Use wb establish HSSFSheet object , Corresponding wb A page in a document
HSSFSheet sheet = wb.createSheet("user list ");// Set page name
// Use sheet establish HSSFRow object , Corresponding sheet A row in the
HSSFRow row = sheet.createRow(0);// The index for 0 It's the first line
// Use row establish HSSFCell object , Corresponding row Column in
HSSFCell cell = row.createCell(0);// The index for 0 That's the first column
// Set the value in the column
cell.setCellValue("id");
cell=row.createCell(1);
cell.setCellValue(" full name ");
cell=row.createCell(2);
cell.setCellValue(" account number ");
cell=row.createCell(3);
cell.setCellValue(" Gender ");
cell=row.createCell(4);
cell.setCellValue(" Date of birth ");
cell=row.createCell(5);
cell.setCellValue(" Department ");
cell=row.createCell(6);
cell.setCellValue(" state ");
cell=row.createCell(7);
// Traverse ActivityList, establish Row
// First judge list There's no data
if(list!=null && list.size()>0){
User user=null;//user If you get the external definition, you don't need to create it every time , More efficient
for (int i=0;i<list.size();i++){
user=list.get(i);
// Every time you traverse one user, Generate a line
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());
}
}
// After improvement !!!!!!!!!!!!!!!!!!!!!
// Set response type application/octet-stream Express excel file
response.setContentType("application/octet-stream;charset=UTF-8");
// Set the response header Content-Disposition Open mode attachment In the form of an attachment
response.addHeader("Content-Disposition","attachment;filename=userList.xls");
OutputStream out = response.getOutputStream();
wb.write(out);
wb.close();
out.flush();//out from response Generated ,tomcat Will close itself , You can't close it yourself , No flush Data may be lost
}边栏推荐
- 英伟达用AI设计GPU:最新H100已经用上,比传统EDA减少25%芯片面积
- IP SAN拥有独立的文件系统,应用服务器通过网络共享协议访问到IP SAN后,可以对文件系统中的文件进行读写操作
- Pytorch.nn实现多层感知机
- 6G智慧内生:技术挑战、架构和关键特征
- vulnhub inclusiveness: 1
- SAP S4 Material Management 库存模块 MARD 数据库表读取技术细节介绍
- 论文笔记:Mind the Gap An Experimental Evaluation of Imputation ofMissing Values Techniques in TimeSeries
- SAP ABAP CDS view 视图的 Replacement 技术介绍
- 追根问底:Objective-C关联属性原理分析
- 多元线性回归详解
猜你喜欢

ue4对动画蓝图的理解

LeetCode 2335. Minimum total time required to fill the cup

leetcode-08

win10开始键点击无响应

Over fitting and under fitting

英伟达用AI设计GPU:最新H100已经用上,比传统EDA减少25%芯片面积

LeetCode 2331. 计算布尔二叉树的值(树的遍历)

SVN学习

因果学习将开启下一代AI浪潮?九章云极DataCanvas正式发布YLearn因果学习开源项目

Beego framework realizes file upload + seven cattle cloud storage
随机推荐
Pytorch学习记录2 线性回归(Tensor,Variable)
博弈论(depu)与投资(40/100)
SAP S4 Material Management 库存模块 MARD 数据库表读取技术细节介绍
数据库面基知识汇总后
军品研制过程所需文件-进阶版
Pytoch framework learning record 1 cifar-10 classification
腾讯云服务器利用镜像部署WordPress个人网站!
基于“7·20郑州特大暴雨”对空天地一体化通信的思考
How to build dashboard and knowledge base in double chain note taking software? Take the embedded widget library notionpet as an example
LeetCode 2325. Decrypt message (map)
从预测到决策,九章云极DataCanvas推出YLearn因果学习开源项目
高数_第1章空间解析几何与向量代数__点到平面的距离
Find balanced binary tree
SSM使用poi将数据导出到excel
Beego框架实现文件上传+七牛云存储
Autojs learning - multi function treasure chest - bottom
新增、修改操作時自定義複雜邏輯校驗-2022新項目
SAP Fiori Launchpad 上看不到任何 tile 应该怎么办?
[acwing] game 60 c-acwing 4496 eat fruit
6G智慧内生:技术挑战、架构和关键特征