当前位置:网站首页>实体List转为excel
实体List转为excel
2022-08-08 08:28: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://blog.csdn.net/weixin_47053123/article/details/126197277
边栏推荐
猜你喜欢
随机推荐
方案 | 医疗单据OCR识别,为医保零星报销打造安全屏障
[Optimized scheduling] Based on particle swarm to realize economic scheduling optimization of microgrid under grid-connected model with matlab code
分清成员函数非成员函数和友元函数
DVWA full level detailed customs clearance tutorial
Redis读写分离(三)
P7214 [JOISC2020] 治療計画 题解
推荐系统 使用surprise库基于协同过滤的方法实现
mysql三种安装方式 你知道了哪种
【项目问题】Ionic开发移动端app,手把手教你如何打包生成apk
【回归预测】基于GPML工具箱的高斯过程回归附matlab代码
生产者消费者模型
Defense - MFW all over the world
攻防世界——web2
【回归预测】基于GPML工具箱的高斯过程回归附matlab代码
lua --- 基本语法学习
LabVIEW前面板和程序框图的最大尺寸
Literature Learning (part33)--Clustering by fast search and find of density peaks
文件包含漏洞-知识点
微软 .NET Core 3.1 年底将结束支持,请升级到.NET 6
选择适合投稿的英文期刊或会议的方法









