我是靠谱客的博主 欢喜纸飞机,这篇文章主要介绍Java 创建一个excel,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.10</version> </dependency>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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); } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
//设置自定义列宽 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); } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
//填充数据 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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部