java中使用POI生成Excel并導出過程
注:本文章中代碼均為本地Demo版本,若后續(xù)代碼更新將不會更新文章
需求說明及實現(xiàn)方式
1.根據(jù)從數(shù)據(jù)庫查詢出的數(shù)據(jù),將其寫入excel表并導出
- 我的想法是通過在實體屬性上寫自定義注解的方式去完成。因為我們在代碼中可以通過反射的方式去獲取實體類中全部的注解及屬性名稱等等。
- 我們可以在自定義注解中聲明一個參數(shù)
value,這里面就存儲其標題,這樣我們
2.數(shù)據(jù)查詢type不同,則顯示的標題數(shù)量不同
- 在注解類中增加
type參數(shù) - 只有滿足對應
type的屬性會被導出至excel中
3.數(shù)據(jù)查詢type不同,則顯示的標題不同(同一個字段)
- 優(yōu)化參數(shù)
value,判斷傳入的value是否為json字符串,如果是json字符串則找到其與type對應的value
4.數(shù)據(jù)的格式化(時間類型格式化、數(shù)據(jù)格式化顯示)
- 數(shù)據(jù)格式化顯示通過在注解類中增加
dict參數(shù),該參數(shù)傳入json字符串。
本來我是想著通過easyExcel來完成這些功能,但是由于項目中已經(jīng)引入了POI的3.9版本依賴,然后easyExcel中POI的依賴版本又高于該版本,而且不管是版本升級還是版本排除降級,總會有一個出現(xiàn)問題,最終也只能通過最基礎的POI編寫代碼實現(xiàn)。
需求完成
依賴引入:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.33</version>
</dependency>
通用代碼
1.ExcelExport
value:標題,也可為json字符串dict:json字符串格式的字典,格式如User中所示type:數(shù)組類型,查詢數(shù)據(jù)type類型是什么值時這個字段會寫入excel中。如我type = {"a"},則我在查詢數(shù)據(jù)時傳入的type為b則不會將這個字段寫入excel中,如果傳入的是a則會正常寫入。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExcelExport {
String value();
String dict() default "";
String[] type() default {};
}2.User
@ExcelExport:即自定義的注解,其中值的含義在上面已經(jīng)說清楚了
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class User {
@ExcelExport(value = "用戶名",type = {"a"})
private String userName;
@ExcelExport(value = "{a: '年齡',b: '年紀'}",type = {"a","b"})
private Integer age;
@ExcelExport(value = "性別",
dict = "[{ value: \"0\", label: \"女\" }," +
"{ value: \"1\", label: \"男\(zhòng)" }]",
type = {"a","b"})
private Integer sex;
@ExcelExport(value = "生日",type = {"b"})
private Date birthday;
}
版本1
版本1中未實現(xiàn)數(shù)據(jù)查詢type不同,則顯示的標題不同(同一個字段)這一功能,如需要加請看PoiExcelUtil中writeTitleCellData方法。
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lzj.anno.ExcelExport;
import com.lzj.entity.User;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.util.StringUtils;
import java.io.*;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* <p>
*
* </p>
*
* @author:雷子杰
* @date:2023/7/4
*/
public class One {
public static void main(String[] args) throws IOException {
List<User> userList = new ArrayList<>();
userList.add(new User("lzj1",1,1,new Date()));
userList.add(new User("lzj2",2,0,new Date()));
userList.add(new User("lzj3",3,1,new Date()));
userList.add(new User("lzj4",4,0,new Date()));
//聲明XSSF對象
XSSFWorkbook xssfSheets = new XSSFWorkbook();
//創(chuàng)建sheet
XSSFSheet userSheet = xssfSheets.createSheet("user");
//創(chuàng)建標題字體
XSSFFont titleFont = xssfSheets.createFont();
titleFont.setBold(true);//加粗
titleFont.setFontName("微軟雅黑");
titleFont.setFontHeightInPoints((short) 12);//字體大小
//創(chuàng)建通用字體
XSSFFont commonFont = xssfSheets.createFont();
commonFont.setBold(false);//加粗
commonFont.setFontName("微軟雅黑");
commonFont.setFontHeightInPoints((short) 12);//字體大小
// 創(chuàng)建標題行單元格樣式
CellStyle titleCellStyle = xssfSheets.createCellStyle();
titleCellStyle.setBorderTop(CellStyle.BORDER_THIN);//框線
titleCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
titleCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
titleCellStyle.setBorderRight(CellStyle.BORDER_THIN);
titleCellStyle.setAlignment(CellStyle.ALIGN_CENTER);//水平對齊方式
titleCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直對齊方式
titleCellStyle.setFont(titleFont);//字體樣式
titleCellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);//單元格前景色
titleCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//填充單元格
//創(chuàng)建通用行單元格樣式
CellStyle commonCellStyle = xssfSheets.createCellStyle();
commonCellStyle.setBorderTop(CellStyle.BORDER_THIN);
commonCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
commonCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
commonCellStyle.setBorderRight(CellStyle.BORDER_THIN);
commonCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
commonCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
commonCellStyle.setFont(commonFont);
commonCellStyle.setWrapText(true);//自動換行
//獲取實體類中全部屬性
Field[] fields = User.class.getDeclaredFields();
//當前行
int currentRow = 0;
//當前列
int currentColumn = 0;
//行高
float rowHeight = 40.1f;
//列寬
int columnWidth = 33 * 256;
//創(chuàng)建行
XSSFRow row = userSheet.createRow(currentRow);
當前行+1
//currentRow++;
//創(chuàng)建標題行
// 遍歷每個字段
for (Field field : fields) {
// 檢查字段是否帶有Explanation注解
if (field.isAnnotationPresent(ExcelExport.class)) {
// 獲取Explanation注解實例
ExcelExport explanation = field.getAnnotation(ExcelExport.class);
// 獲取注解中的解釋
String value = explanation.value();
//創(chuàng)建單元格,傳入值,設置單元格樣式
XSSFCell cell = row.createCell(currentColumn);
cell.setCellValue(value);
cell.setCellStyle(titleCellStyle);
//設置行高度
row.setHeightInPoints(rowHeight);
//設置列的寬度
userSheet.setColumnWidth(currentColumn,columnWidth);
//當前列+1
currentColumn++;
}
}
//重置當前列
currentColumn = 0;
//創(chuàng)建數(shù)據(jù)行
for (User user : userList) {
//每次循環(huán)時重置列
currentColumn = 0;
//當前行+1
currentRow++;
//創(chuàng)建行
row = userSheet.createRow(currentRow);
for (Field field : fields) {
if (field.isAnnotationPresent(ExcelExport.class)) {
try {
//解除private限制
field.setAccessible(true);
// 獲取Explanation注解實例
ExcelExport explanation = field.getAnnotation(ExcelExport.class);
// 獲取屬性的值
Object value = field.get(user);
//日期類型格式化
if (value != null && field.getType() == Date.class){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
value = sdf.format(value);
}
//獲取對應字典
String dict = explanation.dict();
if (!StringUtils.isEmpty(dict) && value != null){
//JSONObject jsonObject = JSON.parseObject(dict);
List<String> list = JSON.parseArray(dict, String.class);
for (String item : list) {
JSONObject jsonObject = JSON.parseObject(item);
if(value == null ? false : jsonObject.getString("value").equals(value.toString()) ){
value = jsonObject.getString("label");
break;
}
}
//value = jsonObject.get(value.toString());
}
//創(chuàng)建單元格,傳入值,設置單元格樣式
XSSFCell cell = row.createCell(currentColumn);
cell.setCellValue(value == null?"":value.toString());
cell.setCellStyle(commonCellStyle);
//設置行高度
row.setHeightInPoints(rowHeight);
//當前列+1
currentColumn++;
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
// 將生成的excel文件輸出流轉為字節(jié)數(shù)組
byte[] bytes = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
xssfSheets.write(outputStream);
outputStream.close();
bytes = outputStream.toByteArray();
//讀取字節(jié)數(shù)組為文件輸入流
InputStream inputStream = new ByteArrayInputStream(bytes);
inputStream.close();
//在聲明一個輸出流將文件下載到本地
File file = new File("C:\\Users\\86158\\Desktop\\zzzzzz.xlsx");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
//將bytes中的內容寫入
bufferedOutputStream.write(bytes);
//刷新輸出流,否則不會寫出數(shù)據(jù)
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}版本2
版本二相比與版本1,其主要優(yōu)勢是將POI相關操作都封裝進了PoiExcelUtil中。
PoiExcelUtil
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzj.anno.ExcelExport;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* <p>
*
* </p>
*
* @author:雷子杰
* @date:2023/7/6
*/
public class PoiExcelUtil {
/**
* 獲取標題字體
* @param xssfWorkbook
* @return
*/
public static XSSFFont getTitleFont(XSSFWorkbook xssfWorkbook){
//創(chuàng)建標題字體
XSSFFont titleFont = xssfWorkbook.createFont();
titleFont.setBold(true);//加粗
titleFont.setFontName("微軟雅黑");
titleFont.setFontHeightInPoints((short) 12);//字體大小
return titleFont;
}
/**
* 獲取通用字體
* @param xssfWorkbook
* @return
*/
public static XSSFFont getCommonFont(XSSFWorkbook xssfWorkbook){
//創(chuàng)建通用字體
XSSFFont commonFont = xssfWorkbook.createFont();
commonFont.setBold(false);//加粗
commonFont.setFontName("微軟雅黑");
commonFont.setFontHeightInPoints((short) 12);//字體大小
return commonFont;
}
/**
* 獲取標題單元格樣式
* @param xssfWorkbook
* @param xssfFont
* @return
*/
public static CellStyle getTitleCellStyle(XSSFWorkbook xssfWorkbook , XSSFFont xssfFont){
// 創(chuàng)建標題行單元格樣式
CellStyle titleCellStyle = xssfWorkbook.createCellStyle();
titleCellStyle.setBorderTop(CellStyle.BORDER_THIN);//框線
titleCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
titleCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
titleCellStyle.setBorderRight(CellStyle.BORDER_THIN);
titleCellStyle.setAlignment(CellStyle.ALIGN_CENTER);//水平對齊方式
titleCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直對齊方式
titleCellStyle.setFont(xssfFont);//字體樣式
titleCellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);//單元格前景色
titleCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//填充單元格
return titleCellStyle;
}
/**
* 獲取通用單元格樣式
* @param xssfWorkbook
* @param xssfFont
* @return
*/
public static CellStyle getCommonCellStyle(XSSFWorkbook xssfWorkbook, XSSFFont xssfFont){
//創(chuàng)建通用行單元格樣式
CellStyle commonCellStyle = xssfWorkbook.createCellStyle();
commonCellStyle.setBorderTop(CellStyle.BORDER_THIN);
commonCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
commonCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
commonCellStyle.setBorderRight(CellStyle.BORDER_THIN);
commonCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
commonCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
commonCellStyle.setFont(xssfFont);
commonCellStyle.setWrapText(true);//自動換行
return commonCellStyle;
}
/**
* 寫入單個單元格數(shù)據(jù)
* @param row 行對象
* @param xssfSheet sheet對象
* @param value 單元格的值
* @param cellStyle 單元格樣式
* @param rowHeight 行高
* @param columnWidth 列寬
*/
public static void writeCellData(XSSFRow row, XSSFSheet xssfSheet , Object value ,CellStyle cellStyle,Integer currentColumn,Float rowHeight,Integer columnWidth){
//創(chuàng)建單元格,傳入值,設置單元格樣式
XSSFCell cell = row.createCell(currentColumn);
cell.setCellValue(value == null ? "" : value.toString());
cell.setCellStyle(cellStyle);
//設置行高度
row.setHeightInPoints(rowHeight);
//設置列的寬度
xssfSheet.setColumnWidth(currentColumn,columnWidth);
}
/**
*
* @param row 行對象
* @param xssfSheet sheet對象
* @param cellStyle 單元格樣式
* @param fields 反射獲取得到的實體對象的全部屬性
* @param currentColumn 當前列
* @param rowHeight 行高
* @param columnWidth 列寬
* @param type 類型
*/
public static void writeTitleCellData(XSSFRow row,XSSFSheet xssfSheet,CellStyle cellStyle,Field[] fields,Integer currentColumn,Float rowHeight,Integer columnWidth,String type){
//創(chuàng)建標題行
// 遍歷每個字段
for (Field field : fields) {
// 檢查字段是否帶有ExcelExport注解
if (field.isAnnotationPresent(ExcelExport.class)) {
// 獲取Explanation注解實例
ExcelExport explanation = field.getAnnotation(ExcelExport.class);
//判斷是否是需要寫入的數(shù)據(jù)類型
String[] typeArray = explanation.type();
Set<String> set = new HashSet<>(Arrays.asList(typeArray));
if (!set.contains(type)){
continue;
}
// 獲取注解中的解釋
String value = explanation.value();
//判斷value是否是json格式數(shù)據(jù)
boolean isJson = true;
try{
Object parse = JSON.parse(value);
}catch (Exception e){
isJson = false;
}
if (isJson == true){//如果是json格式數(shù)據(jù),則給他對應對應類型的值
JSONObject jsonObject = JSON.parseObject(value);
value = jsonObject.getString(type);
}
//寫入單元格數(shù)據(jù)
PoiExcelUtil.writeCellData(row,xssfSheet,value,cellStyle,currentColumn,rowHeight,columnWidth);
//當前列+1
currentColumn++;
}
}
}
/**
* 將集合數(shù)據(jù)全部寫入單元格
* @param list 需要寫入excel的集合數(shù)據(jù)
* @param currentRow 當前行
* @param currentColumn 當前列
* @param row 行對象
* @param xssfSheet sheet對象
* @param cellStyle 單元格樣式
* @param fields 反射獲取得到的實體對象的全部屬性
* @param rowHeight 行高
* @param columnWidth 列寬
* @param type 類型
* @param <T>
*/
public static <T> void writeCommonRowCellData(List<T> list,Integer currentRow ,Integer currentColumn, XSSFRow row,XSSFSheet xssfSheet,CellStyle cellStyle,Field[] fields,Float rowHeight,Integer columnWidth,String type){
//創(chuàng)建數(shù)據(jù)行
for (T obj : list) {
//每次循環(huán)時重置列
currentColumn = 0;
//當前行+1
currentRow++;
//創(chuàng)建行
row = xssfSheet.createRow(currentRow);
for (Field field : fields) {
// 檢查字段是否帶有ExcelExport注解
if (field.isAnnotationPresent(ExcelExport.class)) {
try {
//解除private限制
field.setAccessible(true);
// 獲取Explanation注解實例
ExcelExport explanation = field.getAnnotation(ExcelExport.class);
//判斷是否是需要寫入的數(shù)據(jù)類型
String[] typeArray = explanation.type();
Set<String> set = new HashSet<>(Arrays.asList(typeArray));
if (!set.contains(type)){
continue;
}
// 獲取屬性的值
Object value = field.get(obj);
//日期類型格式化
if (value != null && field.getType() == Date.class){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
value = sdf.format(value);
}
//獲取對應字典
String dict = explanation.dict();
if (!StringUtils.isEmpty(dict) && value != null){
List<String> parseArray = JSON.parseArray(dict, String.class);
for (String item : parseArray) {
JSONObject jsonObject = JSON.parseObject(item);
if(value == null ? false : jsonObject.getString("value").equals(value.toString()) ){
value = jsonObject.getString("label");
break;
}
}
}
//寫入單元格數(shù)據(jù)
PoiExcelUtil.writeCellData(row,xssfSheet,value,cellStyle,currentColumn,rowHeight,columnWidth);
//當前列+1
currentColumn++;
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
}
Two
import com.lzj.entity.User;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.*;
import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* <p>
*
* </p>
*
* @author:雷子杰
* @date:2023/7/4
*/
public class Two {
public static void main(String[] args) throws IOException {
List<User> userList = new ArrayList<>();
userList.add(new User("lzj1",1,1,new Date()));
userList.add(new User("lzj2",2,0,new Date()));
userList.add(new User("lzj3",3,1,new Date()));
userList.add(new User("lzj4",4,0,new Date()));
//聲明XSSF對象
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
//創(chuàng)建sheet
XSSFSheet userSheet = xssfWorkbook.createSheet("user");
//創(chuàng)建標題字體
XSSFFont titleFont = PoiExcelUtil.getTitleFont(xssfWorkbook);
//創(chuàng)建通用字體
XSSFFont commonFont = PoiExcelUtil.getCommonFont(xssfWorkbook);
// 創(chuàng)建標題行單元格樣式
CellStyle titleCellStyle = PoiExcelUtil.getTitleCellStyle(xssfWorkbook,titleFont);
//創(chuàng)建通用行單元格樣式
CellStyle commonCellStyle = PoiExcelUtil.getCommonCellStyle(xssfWorkbook,commonFont);
//獲取實體類中全部屬性
Field[] fields = User.class.getDeclaredFields();
//當前行
int currentRow = 0;
//當前列
int currentColumn = 0;
//行高
float rowHeight = 40.1f;
//列寬
int columnWidth = 33 * 256;
//創(chuàng)建行
XSSFRow row = userSheet.createRow(currentRow);
//創(chuàng)建標題行
PoiExcelUtil.writeTitleCellData(row,userSheet,titleCellStyle,fields,currentColumn,rowHeight,columnWidth,"b");
//重置當前列
currentColumn = 0;
//創(chuàng)建數(shù)據(jù)行
PoiExcelUtil.writeCommonRowCellData(userList,currentRow,currentColumn,row,userSheet,commonCellStyle,fields,rowHeight,columnWidth,"b");
// 將生成的excel文件輸出流轉為字節(jié)數(shù)組
byte[] bytes = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
xssfWorkbook.write(outputStream);
outputStream.close();
bytes = outputStream.toByteArray();
//讀取字節(jié)數(shù)組為文件輸入流
InputStream inputStream = new ByteArrayInputStream(bytes);
inputStream.close();
//在聲明一個輸出流將文件下載到本地
File file = new File("C:\\Users\\86158\\Desktop\\zzzzzz.xlsx");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
//將bytes中的內容寫入
bufferedOutputStream.write(bytes);
//刷新輸出流,否則不會寫出數(shù)據(jù)
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}結果展示
這是我初始化時的數(shù)據(jù)

下面的是我type參數(shù)不同時的數(shù)據(jù),均是以版本2來進行的寫入導出。
type參數(shù)修改位置如下:

type參數(shù)為a

type參數(shù)為b

總結
在項目開發(fā)過程中總會遇到各式各樣的問題,只有不斷的學習,不斷的積累,自身水平才能提高。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
springboot整合RabbitMQ 中的 TTL實例代碼
TTL 是 RabbitMQ 中一個消息或者隊列的屬性,表明一條消息或者該隊列中的所有消息的最大存活時間,單位是毫秒,這篇文章主要介紹了springboot整合RabbitMQ 中的 TTL,需要的朋友可以參考下2022-09-09
springboot2+mybatis多種方式實現(xiàn)多數(shù)據(jù)配置方法
這篇文章主要介紹了springboot2+mybatis多種方式實現(xiàn)多數(shù)據(jù)配置方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03
Java Swing中的文本框(JTextField)與文本區(qū)(JTextArea)使用實例
這篇文章主要介紹了Java Swing中的文本框(JTextField)與文本區(qū)(JTextArea)使用實例,Swing是一個用于開發(fā)Java應用程序用戶界面的開發(fā)工具包,需要的朋友可以參考下2014-10-10
Java實現(xiàn)優(yōu)雅停止線程的有效方法詳解
這篇文章主要為大家詳細如何安全有效停止 Java 線程的,確保多線程應用程序平穩(wěn)運行并實現(xiàn)最佳資源管理,感興趣的小伙伴可以跟隨小編一起學習一下2023-12-12
SpringSecurity跨域請求偽造(CSRF)的防護實現(xiàn)
本文主要介紹了SpringSecurity跨域請求偽造(CSRF)的防護實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07

