java使用easyexcel通過模板導出excel(基於若依)


1.准備excel模板

 

 

 

 

注:  數據類型為map用{},數據類型為List用{.}

2.在項目中resource目錄下新建目錄static,然后把准備好的模板扔進去

3.maven中添加相關依賴

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.1.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.ehcache</groupId>
                    <artifactId>ehcache</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

4.導出excel並返回文件名稱供前端下載

public AjaxResult test(Map map,List list) throws IOException {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/"+"test.xlsx");
String fileName = UUID.randomUUID().toString() + "_" + "test.xlsx";
String downloadPath = RuoYiConfig.getDownloadPath() + fileName;
OutputStream out=new FileOutputStream(downloadPath);
ExcelWriter excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
excelWriter.fill(list, writeSheet);
excelWriter.fill(map, writeSheet);
excelWriter.finish();
return AjaxResult.success(fileName);
}

 5.結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM