当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
生成密码字典的方法
Interviewer: Have you ever used a lock at work?Talk about the advantages, disadvantages and usage scenarios of optimistic locking and pessimistic locking
【AGC】开放式测试示例
中原银行实时风控体系建设实践
Flink Record has Long.MIN_VALUE timestamp (= no timestamp marker). Is the time characteristic
方案 | 医疗单据OCR识别,为医保零星报销打造安全屏障
DVWA全级别详细通关教程
Literature Learning (part33)--Clustering by fast search and find of density peaks
数学基础(一)矩阵对角化、SVD分解以及应用
使用分类权重,轻松解决数据不平衡的问题
蔚来杯2022牛客暑期多校训练营6 ABGJM
【收藏】3. 壁纸收藏
你一定要看的安装及卸载测试用例的步骤及方法总结
Kotlin Compose MiUI13.0.4 版本 Livedata不生效
Go 函数与方法
ES8 | async and await
67:第五章:开发admin管理服务:20:开发【解冻/冻结用户,接口】;(用户状态变更后,需要刷新用户状态,即变更用户会话信息:我们一般通过“删除redis中会话信息,强制用户重新登录“来做的;)
Spark2 struct SQL processing
Stanford Fall 21: Practical Machine Learning [Chapter 5]
深度解析网易严选和京东的会员体系,建议收藏