1.導(dǎo)入依賴(lài)
<!-- poi依賴(lài)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<!-- poi對(duì)于excel 2007的支持依賴(lài)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<!-- poi對(duì)于excel 2007的支持依賴(lài)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>
2.代碼實(shí)現(xiàn)
package com.example.exceldemo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;
import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class EasyExcelTest {
public static void main(String[] args) throws Exception {
InputStream fis = null;
OutputStream outputStream= null;
Workbook wb = null;
try {
String fileName = "測(cè)試單位"+ UUID.randomUUID() + ".xlsx";
String filePath = "D:\\report\\"+fileName;
//1.獲取數(shù)據(jù)
List<ExcelVo> list = new ArrayList<>();
list.add(new ExcelVo(1,"1221","黃山"));
list.add(new ExcelVo(2,"3333","河水"));
list.add(new ExcelVo(3,"6666","青石"));
//2.獲取模板
fis = new ClassPathResource("templates/report_template.xlsx").getInputStream();
File file=new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
outputStream = new FileOutputStream(file);
//3.根據(jù)模板創(chuàng)建工作簿
wb = new XSSFWorkbook(fis);
//4.讀取工作表
Sheet sheet = wb.getSheetAt(0);
CellStyle styles[] = new CellStyle[row.getLastCellNum()];
//5.抽取第2行的公共樣式 , 因?yàn)榈谝恍?為標(biāo)題 第2行是數(shù)據(jù) 下標(biāo)為1
Row row = sheet.getRow(1);
CellStyle styles[] = new CellStyle[row.getLastCellNum()];
Cell cell = null;
for (int i = 0; i < row.getLastCellNum(); i++) {
cell = row.getCell(i);
styles[i] = cell.getCellStyle();
}
//5.構(gòu)造單元格
int rowIndex=1;
//方式一 手動(dòng)
for (ExcelVo vo2:list) {
//創(chuàng)建每一行,同excel的第二行開(kāi)始
row= sheet.createRow(rowIndex++);
//第一列
int i=0;
cell = row.createCell(i++);
cell.setCellStyle(styles[0]);
cell.setCellValue(vo2.getId());//寫(xiě)入數(shù)據(jù) 序號(hào)
//第二列
cell = row.createCell(i++);
cell.setCellStyle(styles[0]);
cell.setCellValue(vo2.getCode());
//第三列(注:最后一列不用i++,前面的必須i++)
cell = row.createCell(i);
cell.setCellStyle(styles[0]);
cell.setCellValue(vo2.getName());
}
//方式二 動(dòng)態(tài)
for (ExcelVo t:list) {
//創(chuàng)建每一行,同excel的第二行開(kāi)始
row = sheet.createRow(rowIndex++);
Field[] fields=t.getClass().getDeclaredFields();//獲取filed
for (int i = 0; i < fields.length; i++) {
Field field=fields[i];
field.setAccessible(true);//設(shè)置私有屬性可以訪問(wèn)
Object val = field.get(t);//獲取值
if (val==null)val="";
cell = row.createCell(i);
cell.setCellStyle(styles[0]);//設(shè)置單元格樣式
cell.setCellValue(val.toString());//寫(xiě)入數(shù)據(jù)
}
}
wb.write(outputStream);//寫(xiě)入數(shù)據(jù)
} catch (Exception e) {
throw new Exception(e);
} finally {
fis.close();
wb.close();
outputStream.close();
}
}
}
3.模板
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-689903.html
參考:https://blog.csdn.net/weixin_45742032/article/details/119593288?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-119593288-blog-86538258.235%5Ev38%5Epc_relevant_anti_t3_base&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-119593288-blog-86538258.235%5Ev38%5Epc_relevant_anti_t3_base&utm_relevant_index=2文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-689903.html
到了這里,關(guān)于easyexcel poi根據(jù)模板導(dǎo)出Excel的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!