一、前言
数据导出为Excel在我们写项目的过程中经常用到
需要用到的jar包 poi-3.17.jar
二、具体实现步骤
//第一步创建一个webbook,对应一个Excel文件
HSSFWorkbook wb=new HSSFWorkbook();
//第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet=wb.createSheet("食物信息数据");
//第三步,在sheet中添加表头第0行
HSSFRow row = sheet.createRow(0);
//第四步,创建单元格,并设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);//居中格式
HSSFCell cell = row.createCell(0);
cell.setCellValue("编号");
cell.setCellStyle(style);
cell=row.createCell((short)1);
cell.setCellValue("名称");
cell.setCellStyle(style);
cell=row.createCell((short)2);
cell.setCellValue("类型");
cell.setCellStyle(style);
cell=row.createCell((short)3);
cell.setCellValue("单价");
cell.setCellStyle(style);
cell=row.createCell((short)4);
cell.setCellValue("库存");
cell.setCellStyle(style);
//第五步,写入实体数据,从数据库拿数据
FoodController controller=new FoodController();
List<Foods> foodsList = controller.foodsList(null, null);
for (int i = 0; i < foodsList.size(); i++) {
//创建单元格,并赋值
row=sheet.createRow(i+1);
Foods foods = foodsList.get(i);
row.createCell((short)0).setCellValue(foods.getId());
row.createCell((short)1).setCellValue(foods.getName());
row.createCell((short)2).setCellValue(foods.getType());
row.createCell((short)3).setCellValue(foods.getPrice());
row.createCell((short)4).setCellValue(foods.getNum());
}
//第六步,下载Excel
OutputStream out=null;
out=response.getOutputStream();
String fileName="食物信息.xls";
response.setContentType("application/x-=msdownload");
response.setHeader("Content-Disposition", "attachment; filename="
+URLEncoder.encode(fileName, "UTF-8"));
wb.write(out);
三、实现效果图
导出成功后数据成功显示
来源:https://%bcnet%/cst522445906/article/details/115903661
标签:java,数据,导出,Excel,POI
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Http请求方式之RestTemplate常用方法详解
2021-06-29 02:32:51
C#画笔Pen绘制自定义线的帽子
2022-01-09 20:33:55
C#中Lambda表达式的用法
2022-12-22 15:59:17
java开发之闹钟的实现代码
2021-07-08 12:46:49
![](https://img.aspxhome.com/file/2023/5/74905_0s.png)
Java实战之在线租房系统的实现
2022-09-29 04:44:18
![](https://img.aspxhome.com/file/2023/8/60808_0s.jpg)
JAVA实现301永久重定向方法
2023-12-18 10:59:08
Android基础教程数据存储之文件存储
2023-08-05 18:18:10
![](https://img.aspxhome.com/file/2023/3/86743_0s.png)
C#开发Windows服务实例之实现禁止QQ运行
2023-04-13 03:29:29
![](https://img.aspxhome.com/file/2023/6/83296_0s.jpg)
C#实现将Email地址转成图片显示的方法
2022-09-26 09:35:36
Java 自定义Spring框架与核心功能详解
2023-11-29 05:45:19
![](https://img.aspxhome.com/file/2023/3/84793_0s.png)
Android Support Annotations资料整理
2021-09-21 13:39:33
Java设计模式之模版方法模式简介
2023-07-16 14:58:32
![](https://img.aspxhome.com/file/2023/0/61130_0s.jpg)
Spring Boot缓存实战之Redis 设置有效时间和自动刷新缓存功能(时间支持在配置文件中配置)
2023-11-11 01:57:18
![](https://img.aspxhome.com/file/2023/3/59053_0s.png)
android自定义view之模拟qq消息拖拽删除效果
2023-01-29 11:48:34
![](https://img.aspxhome.com/file/2023/4/95014_0s.gif)
java多线程的同步方法实例代码
2022-02-16 19:30:47
Spring框架的环境搭建和测试实现
2023-10-27 17:57:57
![](https://img.aspxhome.com/file/2023/4/108434_0s.jpg)
Java上传文件错误java.lang.NoSuchMethodException的解决办法
2023-11-10 13:15:43
Java基础教程之数组的定义与使用
2022-04-24 10:24:12
Java NIO深入分析
2022-12-29 03:50:27
![](https://img.aspxhome.com/file/2023/1/79941_0s.png)
简单的观察者模式示例分享
2023-02-11 12:52:12