我是靠谱客的博主 欢喜纸飞机,最近开发中收集的这篇文章主要介绍Java 创建一个excel,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.10</version>
        </dependency>
public  void createReportFile() {
        List<List<String>>head = new ArrayList<>();
        List<List<String>> data = getDefaultData();
        //表头
        String[] strColumns = new String[]{"序号","AAAA","BBB","CCC","DDD"};
        for (int i = 0; i <strColumns.length ; i++) {
            List<String> column = new ArrayList<>();
            column.add(strColumns[i]);
            head.add(column);
        }
        OutputStream out;
        try {
            out = new FileOutputStream("./a/b/c/"+"test.xlsx");
            EasyExcelFactory.write(out).registerWriteHandler(new ExcelWidthStyleStrategy())
                                .head(head).sheet("测试").doWrite(data);
            out.flush();
        } catch (Exception e) {
            Log.debug("createExecl false"+ e);
        }
    }
//设置自定义列宽
public class ExcelWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {
    @Override
    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
        Sheet sheet = writeSheetHolder.getSheet();
        sheet.setColumnWidth(0, 2000);
        sheet.setColumnWidth(1, 8000);
        sheet.setColumnWidth(2, 2000);
        sheet.setColumnWidth(3, 3000);
        sheet.setColumnWidth(4, 60000);
    }
}
//填充数据
 public List<List<String>> getDefaultData() {
        List<List<String>> defaultData = new ArrayList<>();
        List<String> oneLine = new ArrayList<>();
        List<String> twoLine = new ArrayList<>();
        List<String> threeLine = new ArrayList<>();
        Collections.addAll(oneLine,"1","测试","测试");
        Collections.addAll(twoLine,"2","哈哈","嗯嗯");
        Collections.addAll(threeLine,"3","哈哈","嗯嗯");    
        Collections.addAll(defaultData,oneLine,twoLine,threeLine,fourLine,fiveLine,sixLine,sevenLine,eightLine);
        return defaultData;
    }

创建多个sheet

    public static ExcelWriter excelWriter;
     try {
            excelWriter = EasyExcel.write(out).registerWriteHandler(new ExcelWidthStyleStrategy()).build();
            WriteSheet summarySheet= EasyExcel.writerSheet(sheetIndex, "汇总").head(summaryhead).build();
            excelWriter.write(summaryreport,summarySheet);
            WriteSheet summarySheet2= EasyExcel.writerSheet(sheetIndex, "汇总2").head(summaryhead).build();
            excelWriter.write(summaryreport2,summarySheet2);
             }catch (Exception e){
            throw new Exception(e);
        }finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }


最后

以上就是欢喜纸飞机为你收集整理的Java 创建一个excel的全部内容,希望文章能够帮你解决Java 创建一个excel所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部