成免費(fèi)crm是什么鄭州網(wǎng)站優(yōu)化
EasyExcel導(dǎo)入/導(dǎo)出Excel文件簡(jiǎn)單寫(xiě)法
1、導(dǎo)入依賴(lài)
2、創(chuàng)建簡(jiǎn)單導(dǎo)入、導(dǎo)出demo
3、創(chuàng)建類(lèi) 繼承AnalysisEventListener(導(dǎo)入Excel監(jiān)聽(tīng)解析表格數(shù)據(jù))
4、創(chuàng)建類(lèi) 基于注解 自定義Excel導(dǎo)出模版
1、導(dǎo)入EasyExcel依賴(lài)
?? ??? ?
?? ??? ?<!--導(dǎo)入EasyExcel依賴(lài)-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.alibaba</groupId>
? ? ? ? ? ? <artifactId>easyexcel</artifactId>
? ? ? ? ? ? <version>3.0.5</version>
? ? ? ? </dependency>
2、創(chuàng)建簡(jiǎn)單導(dǎo)入、導(dǎo)出demo
import com.alibaba.excel.EasyExcel;
import com.google.common.collect.Lists;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
?* @ClassName ExcelTest
?* @Descripition easyExcel 導(dǎo)入/導(dǎo)出
?* @Author admin
?* @Date 2023/10/9 13:56
?*/
public class ExcelTest {
? ? public static void main(String[] args) {
? ? ? ? ExcelTest excelTest = new ExcelTest();
? ? ? ? // easyExcel 導(dǎo)入Excel表格簡(jiǎn)單 : Demo
? ? ? ? excelTest.excelReadUtil();
? ? ? ? // easyExcel 導(dǎo)出Excel表格簡(jiǎn)單 : Demo
? ? ? ? excelTest.excelWriteUtil();
? ? }
? ? /**
? ? ?* easyExcel 導(dǎo)入Excel表格簡(jiǎn)單 : Demo
? ? ?*/
? ? public void excelReadUtil() {
? ? ? ? // Excel文件地址(自定義個(gè)excel文件夾)
? ? ? ? String fileName = "D:\\test\\test001\\desktop\\excel_test.xlsx";
? ? ? ? //記錄開(kāi)始讀取Excel時(shí)間,也是導(dǎo)入程序開(kāi)始時(shí)間
? ? ? ? long startReadTime = System.currentTimeMillis();
? ? ? ? System.out.println("------開(kāi)始讀取Excel的Sheet時(shí)間(包括導(dǎo)入數(shù)據(jù)過(guò)程):" + startReadTime + "ms------");
? ? ? ? //讀取所有Sheet的數(shù)據(jù).每次讀完一個(gè)Sheet就會(huì)調(diào)用這個(gè)方法
? ? ? ? /*List<Object> list = EasyExcel.read(fileName, new CustomListener()).headRowNumber(1).doReadAllSync();
? ? ? ? for (Object o : list) {
? ? ? ? ? ? System.out.println(o);
? ? ? ? }*/
? ? ? ? // EasyExcel.read(fileName).doReadAll();
? ? ? ? // 多sheet頁(yè)讀取(所有的sheet頁(yè))
? ? ? ? // EasyExcel.read(fileName, new CustomListener()).doReadAll();
? ? ? ? // 讀取第一個(gè)sheet頁(yè)
? ? ? ? EasyExcel.read(fileName, new CustomListener()).sheet().doRead();
? ? ? ? long endReadTime = System.currentTimeMillis();
? ? ? ? System.out.println("------結(jié)束讀取Excel的Sheet時(shí)間(包括導(dǎo)入數(shù)據(jù)過(guò)程):" + endReadTime + "ms------");
? ? ? ? System.out.println("------讀取Excel的Sheet時(shí)間(包括導(dǎo)入數(shù)據(jù))共計(jì)耗時(shí):" + (endReadTime - startReadTime) + "ms------");
? ? }
Excel導(dǎo)入截圖:
控制臺(tái)輸出截圖:
? ? /**
? ? ?* easyExcel 導(dǎo)出Excel表格簡(jiǎn)單 : Demo
? ? ?*/
? ? public void excelWriteUtil() {
? ? ? ? //1、創(chuàng)建一個(gè)文件對(duì)象
? ? ? ? File excelFile = new File("D:\\test\\test001\\desktop\\excel_test1001.xlsx");
? ? ? ? //2、判斷文件是否存在,不存在則創(chuàng)建一個(gè)Excel文件
? ? ? ? if (!excelFile.exists()) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? excelFile.createNewFile();//創(chuàng)建一個(gè)新的文件
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //3、指定需要那個(gè)class去寫(xiě)。然后寫(xiě)到第一個(gè)sheet,名字為模版,然后文件流會(huì)自動(dòng)關(guān)閉
? ? ? ? EasyExcel.write(excelFile, ExcelTemplate.class).sheet("訂單模版").doWrite(queryToExcel());
? ? }
? ? /**
? ? ?* 1、查詢(xún)數(shù)據(jù)庫(kù)獲取導(dǎo)出的數(shù)據(jù)集合
? ? ?* 2、集合轉(zhuǎn)換導(dǎo)出模版實(shí)體集合:ExcelTemplate
? ? ?*
? ? ?* @return
? ? ?*/
? ? public List<ExcelTemplate> queryToExcel() {
? ? ? ? // 1、查詢(xún)數(shù)據(jù)庫(kù)獲取導(dǎo)出的數(shù)據(jù)集合
? ? ? ? // 模擬業(yè)務(wù)代碼,獲取數(shù)據(jù)集
? ? ? ? List<ExcelTemplate> orders = Lists.newArrayList();
? ? ? ? for (int i = 0; i < 50; i++) {
? ? ? ? ? ? ExcelTemplate template1 = new ExcelTemplate(1000L + i, "訂單01" + i, "商品名稱(chēng)" + i, "sk1001" + i, 36L + i, 50L + i, "收件人" + i, "北京科技園" + i);
? ? ? ? ? ? orders.add(template1);
? ? ? ? }
? ? ? ? // 2、集合轉(zhuǎn)換導(dǎo)出模版實(shí)體集合:ExcelTemplate
? ? ? ? List<ExcelTemplate> excels = new ArrayList<>();
? ? ? ? //遍歷數(shù)據(jù)集,導(dǎo)出Excel
? ? ? ? for (int i = 0; i < orders.size(); i++) {
? ? ? ? ? ? ExcelTemplate order = orders.get(i);
? ? ? ? ? ? ExcelTemplate data = new ExcelTemplate();
? ? ? ? ? ? data.setOrderNum(order.getOrderNum());
? ? ? ? ? ? data.setOrderName(order.getOrderName());
? ? ? ? ? ? data.setGoodsName(order.getGoodsName());
? ? ? ? ? ? data.setGoodsNum(order.getGoodsNum());
? ? ? ? ? ? data.setPrice(order.getPrice());
? ? ? ? ? ? data.setNum(order.getNum());
? ? ? ? ? ? data.setUserName(order.getUserName());
? ? ? ? ? ? data.setAddress(order.getAddress());
? ? ? ? ? ? excels.add(data);
? ? ? ? }
? ? ? ? return excels;
? ? }
}
Excel導(dǎo)出截圖:
3、創(chuàng)建類(lèi) 繼承AnalysisEventListener(導(dǎo)入Excel監(jiān)聽(tīng)解析表格數(shù)據(jù))
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.assertj.core.util.Lists;
import java.util.List;
import java.util.Map;
/**
?* @ClassName CustomListener
?* @Descripition 導(dǎo)入Excel監(jiān)聽(tīng)解析表格數(shù)據(jù)
?* @Author admin
?* @Date 2023/10/9 15:40
?*/
@Slf4j
public class CustomListener extends AnalysisEventListener {
? ? // 處理數(shù)據(jù): 分批導(dǎo)入閾值
? ? private static final Long size = 8L;
? ? // private List<Map<String, String>> list = Lists.newArrayList();
? ? private List<Object> list = Lists.newArrayList();
? ? // 每解析一行數(shù)據(jù),該方法會(huì)被調(diào)用一次
? ? @Override
? ? public void invoke(Object o, AnalysisContext analysisContext) {
? ? ? ? log.info("Excel每解析一行數(shù)據(jù),該方法(invoke)會(huì)被調(diào)用一次 data : {}", JSON.toJSONString(o));
? ? ? ? // int size = analysisContext.readRowHolder().getCellMap().entrySet().size();
? ? ? ? // System.out.println(size);
? ? ? ? list.add(o);
? ? ? ? if(list.size() >= CustomListener.size){
? ? ? ? ? ? for (Object o1 : list) {
? ? ? ? ? ? ? ? System.out.println(o1);
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println("處理數(shù)據(jù): 分批導(dǎo)入閾值: " + size);
? ? ? ? ? ? System.out.println();
? ? ? ? ? ? System.out.println();
? ? ? ? ? ? list.clear();
? ? ? ? }
? ? }
? ? // 全部解析完成被調(diào)用
? ? @Override
? ? public void doAfterAllAnalysed(AnalysisContext analysisContext) {
? ? ? ? System.out.println("最后剩余數(shù)據(jù);不夠分割 長(zhǎng)度 : " + list.size());
? ? ? ? if(CollectionUtils.isNotEmpty(list)){
? ? ? ? ? ? for (Object o : list) {
? ? ? ? ? ? ? ? System.out.println(o);
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println();
? ? ? ? ? ? list.clear();
? ? ? ? }
? ? ? ? System.out.println("解析數(shù)據(jù)完成!!!");
? ? ? ? // System.out.println("解析數(shù)據(jù)完成!!!");
? ? }
}
4、創(chuàng)建類(lèi) 基于注解 自定義Excel導(dǎo)出模版
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import com.alibaba.excel.enums.poi.BorderStyleEnum;
import com.alibaba.excel.enums.poi.FillPatternTypeEnum;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
/**
?* @ClassName ExcelTemplate
?* @Descripition 導(dǎo)出模版
?* @Author admin
?* @Date 2023/10/9 18:14
?*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@HeadRowHeight(value = 50) // 頭部行高
@ContentRowHeight(value = 25) // 內(nèi)容行高
@ColumnWidth(value = 25) // 列寬
// 頭背景設(shè)置成紅色 IndexedColors.RED.getIndex()
// @HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 10)
// 頭字體設(shè)置成20, 字體默認(rèn)宋體
@HeadFontStyle(fontName = "仿宋", fontHeightInPoints = 20)
// 頭部邊框?qū)嵕€
@HeadStyle(borderTop = BorderStyleEnum.THICK,
? ? ? ? borderBottom = BorderStyleEnum.THICK,
? ? ? ? borderLeft = BorderStyleEnum.THICK,
? ? ? ? borderRight = BorderStyleEnum.THICK)
// 內(nèi)容的背景設(shè)置成綠色 ?IndexedColors.GREEN.getIndex()
// @ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 17)
// 內(nèi)容字體設(shè)置成20, 字體默認(rèn)宋體
@ContentFontStyle(fontName = "楷體", fontHeightInPoints = 20)
// Excel設(shè)置內(nèi)容字體是否水平居中、邊框?qū)嵕€
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER,
? ? ? ? borderTop = BorderStyleEnum.MEDIUM,
? ? ? ? borderBottom = BorderStyleEnum.MEDIUM,
? ? ? ? borderLeft = BorderStyleEnum.DOUBLE,
? ? ? ? borderRight = BorderStyleEnum.DOUBLE)
public class ExcelTemplate {
? ? // 字符串的頭背景設(shè)置成粉紅 IndexedColors.PINK.getIndex()
? ? // @HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 14)
? ? // 字符串的頭字體設(shè)置成20
? ? // @HeadFontStyle(fontHeightInPoints = 20)
? ? // 字符串的內(nèi)容背景設(shè)置成天藍(lán) IndexedColors.SKY_BLUE.getIndex()
? ? // @ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)
? ? // 字符串的內(nèi)容字體設(shè)置成20,默認(rèn)宋體
? ? // @ContentFontStyle(fontName = "宋體", fontHeightInPoints = 20)
? ? // 設(shè)置是否水平居中
? ? // @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.LEFT)
? ? @ExcelProperty("訂單編碼")
? ? private Long orderNum;
? ? @ExcelProperty("訂單名稱(chēng)")
? ? private String orderName;
? ? @ExcelProperty("商品名稱(chēng)")
? ? private String goodsName;
? ? @ExcelProperty("商品編碼")
? ? private String goodsNum;
? ? @ExcelProperty("價(jià)格")
? ? private Long price;
? ? @ExcelProperty("數(shù)量")
? ? private Long num;
? ? @ExcelProperty("用戶(hù)名稱(chēng)")
? ? private String userName;
? ? @ColumnWidth(value = 80) // 列寬
? ? @ExcelProperty("收貨地址")
? ? private String address;
}
?