当前位置:网站首页>The entity List to excel
The entity List to excel
2022-08-08 08:59:00 【[email protected】
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
package com.mabo.excel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class CreateTest<T> {
public static<T> File create(Class<T> aClass, List<T> list, String fileName) throws IOException {
// 创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("sheet1");
// 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow(0);
// 添加标题行
HSSFCell cell = null;
Field[] fields = aClass.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
// 获取行内对应单元格
cell = row.createCell(i);
// 单元格赋值
cell.setCellValue(fields[i].getName());
}
// 写入实体数据,实际应用中这些数据从数据库得到,list中字符串的顺序必须和数组strArray中的顺序一致
int i = 0;
for (int j = 0; j < list.size(); j++) {
T t = list.get(i);
i++;
row = sheet.createRow(i );
// 添加数据行
//数据转为Json
String json= JSON.toJSONString(t);//关键
JSONObject parse = (JSONObject) JSONObject.parse(json);
for (int z = 0; z < fields.length; z++) {
Field field=fields[z];
cell = row.createCell(z);
// 获取行内对应单元格
String name = field.getName();
Object o = parse.get(name);
// 单元格赋值
if (o instanceof Long){
long o1 = (long) o;
Date date = new Date();
date.setTime(o1);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cell.setCellValue(simpleDateFormat.format(date));
}
else if (o instanceof String){
cell.setCellValue((String) o);
}
else if (o instanceof Double){
cell.setCellValue((double) o);
}
else if (o instanceof Boolean){
cell.setCellValue((boolean) o);
}
}
}
// // 第六步,将文件存到指定位置
FileOutputStream fout = new FileOutputStream(fileName);
try {
wb.write(fout);
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
finally {
fout.close();
}
return new File(fileName);
}
}
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/220/202208080827520834.html
边栏推荐
- P7214 [JOISC2020] 治療計画 题解
- Matlab实现异构交通流
- 等式变换(2015届华为校园招聘机试题第三题 )
- 数据库调优:Mysql索引对group by 排序的影响
- Database_JDBC
- lvm creates logical volumes
- The industry's first "Causal Inference Whole Process" Challenge!WAIC 2022 · Hackathon invites global developer elites to challenge
- 百度飞浆EISeg高效交互式标注分割软件的使用教程
- 攻防世界——mfw
- jupyter lab安装、配置教程
猜你喜欢

【AGC】开放式测试示例

2022 - image classification 】 【 MaxViT ECCV

攻防世界——web2
![[Image Classification] 2021-CoAtNet NeurlPS](/img/e6/c1b9d10dc1605ab568d59f082ac419.png)
[Image Classification] 2021-CoAtNet NeurlPS

要写脚本,编程不好不要紧--浅谈CTF中脚本的编写方法

My MySQL installation that is how to solve

【office】word

Today share how to submit a bug

BLOB, TEXT, GEOMETRY or JSON column ‘xxxx‘ can‘t have a default value

Django+MySQL+HarmonyOS------------笔记二
随机推荐
nodeJs--egg框架介绍
Elasticseach实践1
C# - var 关键字
DBeaver 22.1.4 released, a visual database management platform
Kotlin协程:生命周期原理
Database Tuning: The Impact of Mysql Indexes on Group By Sorting
Stanford Fall 21: Practical Machine Learning [Chapter 5]
让百姓消费更安全更放心更满意 江苏出台放心消费创建示范认定管理办法
DBeaver 22.1.4 发布,可视化数据库管理平台
Use of .json()
regular expression
Offensive and defensive world - web2
X射线聚焦系统
面试官:工作中用过锁么?说说乐观锁和悲观锁的优劣势和使用场景
22-08-06 西安 EasyExcel实现字典表导入、导出
lua --- 基本语法学习
【图像分类】2021-CoAtNet NeurlPS
Offensive and defensive world - leaking
蔚来杯2022牛客暑期多校训练营6 ABGJM
Offensive and defensive world - ics-05