Java利用Easyexcel導(dǎo)出excel表格的示例代碼
1.導(dǎo)入 EasyExcel Maven包
<!--easyexcel 導(dǎo)出excel依賴-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.7</version>
</dependency>
2.配置
配置表格表頭樣式,以及內(nèi)容的寫入方式
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import java.util.List;
public class EasyExcelWriteHandler implements SheetWriteHandler {
/**
* 表頭數(shù)據(jù)
*/
private final List<String> headsDatas;
/**
* 內(nèi)容數(shù)據(jù)
*/
private final List<List<String>> bodyDatas;
/**
* 表頭樣式
*/
private CellStyle cellStyle;
/**
* 內(nèi)容樣式
*/
private CellStyle cellStyleHeader;
public EasyExcelWriteHandler(List<String> headsDatas, List<List<String>> bodyDatas) {
this.headsDatas = headsDatas;
this.bodyDatas = bodyDatas;
}
@Override
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
}
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
Workbook workbook = writeWorkbookHolder.getWorkbook();
Sheet sheet = workbook.getSheetAt(0);
// 表頭樣式
if (cellStyleHeader == null) {
cellStyleHeader = workbook.createCellStyle();
cellStyleHeader.setAlignment(HorizontalAlignment.CENTER);
cellStyleHeader.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyleHeader.setFillForegroundColor(IndexedColors.WHITE.getIndex());
Font font = workbook.createFont();
// 字體大小
font.setFontHeightInPoints((short) 16);
// 字體
font.setFontName("微軟雅黑");
//加粗
font.setBold(true);
cellStyleHeader.setFont(font);
}
// 內(nèi)容樣式
if (cellStyle == null) {
cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
Font font = workbook.createFont();
font.setFontName("微軟雅黑");
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
}
//表頭內(nèi)容
Row row = sheet.getRow(0);
if (row == null) {
row = sheet.createRow(0);
}
//遍歷寫入表頭
for (int rowInt = 0; rowInt < headsDatas.size(); rowInt++) {
//獲取列內(nèi)容
String cellValue = headsDatas.get(rowInt);
//根據(jù)表頭內(nèi)容設(shè)置表頭列寬(自適應(yīng)表頭寬度)
sheet.setColumnWidth(rowInt, cellValue.length() * 875);
Cell cell = row.getCell(rowInt);
if (cell == null) {
cell = row.createCell(rowInt);
}
cell.setCellStyle(cellStyleHeader);
cell.setCellValue(cellValue);
//遍歷寫入內(nèi)容
if (rowInt < bodyDatas.size()) {
for (int bodyRowInt = 0; bodyRowInt < bodyDatas.get(rowInt).size(); bodyRowInt++) {
Row row0 = sheet.getRow(bodyRowInt + 1);
if (row0 == null) {
row0 = sheet.createRow(bodyRowInt + 1);
}
Cell cell0 = row0.getCell(rowInt);
if (cell0 == null) {
cell0 = row0.createCell(rowInt);
}
cell0.setCellStyle(cellStyle);
cell0.setCellValue(bodyDatas.get(rowInt).get(bodyRowInt));
}
}
}
}
}
3.輸出Excel到前端
public static void easyUtil1(ExcelBody excelBody) throws IOException {
//轉(zhuǎn)換內(nèi)容數(shù)據(jù)
List<List<String>> list2 = new ArrayList<>();
for (List<Object> objects : excelBody.getBodyDatas()) {
List<String> strs = new ArrayList<>();
for (Object object : objects) {
strs.add(String.valueOf(object));
}
list2.add(strs);
}
//設(shè)置表名
String simpleDateFormat = new String((new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())).getBytes(), "UTF-8");
//設(shè)置字符編碼
response.setCharacterEncoding("utf-8");
//設(shè)置內(nèi)容類型
response.setContentType("application/vnd.ms-excel");
//設(shè)置標(biāo)題
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(excelBody.getFileName() + simpleDateFormat + ".xlsx", "utf-8"));
//核心代碼
//方式一:
//EasyExcel.write(response.getOutputStream()).head(list2).sheet("sheet1").doWrite(list2);
//方式二:
ExcelWriterBuilder writerBuilder = EasyExcel.write(response.getOutputStream());
writerBuilder.registerWriteHandler(new EasyExcelWriteHandler(excelBody.getHeads(), list2));
ExcelWriter excelWriter = writerBuilder.build();
ExcelWriterSheetBuilder writerSheetBuilder = EasyExcel.writerSheet("Sheet1");
excelWriter.write(new ArrayList<>(), writerSheetBuilder.build());
excelWriter.finish();
}
@Data
public class ExcelBody {
/**
* 文件名
*/
private String fileName;
/**
* 表頭
*/
private List<String> heads;
/**
* 數(shù)據(jù)體
*/
private List<List<Object>> bodyDatas;
}
到此這篇關(guān)于Java利用Easyexcel導(dǎo)出excel表格的示例代碼的文章就介紹到這了,更多相關(guān)Java Easyexcel導(dǎo)出excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java利用easyexcel實(shí)現(xiàn)導(dǎo)入與導(dǎo)出功能
- Java利用EasyExcel讀取寫入Excel詳情
- Java簡(jiǎn)單使用EasyExcel操作讀寫excel的步驟與要點(diǎn)
- Java 中EasyExcel的使用方式
- java EasyExcel面向Excel文檔讀寫邏輯示例詳解
- Java?easyExcel的復(fù)雜表頭多級(jí)表頭導(dǎo)入
- Java利用EasyExcel實(shí)現(xiàn)合并單元格
- Java?easyexcel使用教程之導(dǎo)出篇
- Java中Easyexcel?實(shí)現(xiàn)批量插入圖片功能
- Java使用easyExcel實(shí)現(xiàn)導(dǎo)入功能
相關(guān)文章
Java實(shí)現(xiàn)拖拽文件上傳dropzone.js的簡(jiǎn)單使用示例代碼
本篇文章主要介紹了Java實(shí)現(xiàn)拖拽文件上傳dropzone.js的簡(jiǎn)單使用示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
手動(dòng)實(shí)現(xiàn)將本地jar添加到Maven倉(cāng)庫(kù)
這篇文章主要介紹了手動(dòng)實(shí)現(xiàn)將本地jar添加到Maven倉(cāng)庫(kù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
SpringBoot中使用spring-retry 解決失敗重試調(diào)用
本文主要介紹了SpringBoot中使用spring-retry 解決失敗重試調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Java深入講解instanceof關(guān)鍵字的使用
instanceof 是 Java 的一個(gè)二元操作符,類似于 ==,>,< 等操作符。instanceof 是 Java 的保留關(guān)鍵字。它的作用是測(cè)試它左邊的對(duì)象是否是它右邊的類的實(shí)例,返回 boolean 的數(shù)據(jù)類型2022-05-05
詳解Java數(shù)據(jù)庫(kù)連接JDBC基礎(chǔ)知識(shí)(操作數(shù)據(jù)庫(kù):增刪改查)
這篇文章主要介紹了詳解Java數(shù)據(jù)庫(kù)連接JDBC基礎(chǔ)知識(shí)(操作數(shù)據(jù)庫(kù):增刪改查),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01

