【知识分享】easyExcel导出表格调整表格样式
easyexcel
快速、简洁、解决大文件内存溢出的java处理Excel工具
项目地址:https://gitcode.com/gh_mirrors/ea/easyexcel
免费下载资源
·
easyExcel作为开发人员常用的导出导出的工具,会遇到导出的单元格需要加边框、调整字体大小等需求,下面直接上代码。
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.write.builder.ExcelWriterBuilder; import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; import com.alibaba.excel.write.merge.AbstractMergeStrategy; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteTable; import com.alibaba.excel.write.metadata.fill.FillConfig; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; import org.apache.catalina.connector.Response; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.example.demo2.DemoData; import org.springframework.util.StringUtils; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class EasyExcelMergeDemo { public static void main(String[] args) { // 准备数据 List<DemoData1> data = getData(); // 设置输出文件路径和名称 String fileName = "example.xlsx"; ExcelWriter excelWriter =null; HorizontalCellStyleStrategy horizontalCellStyleStrategy = setConfigure(); //设置请求信息(获取HttpServletResponse,可通过HttpServletResponse给导出数据命名) excelWriter = EasyExcel.write("测试.xlsx").build(); // excelWriter = EasyExcelFactory.getWriter(response.getOutputStream()); WriteSheet writeSheet =EasyExcel.writerSheet(0,"测试.xlsx").registerWriteHandler(horizontalCellStyleStrategy).build(); // 创建 ExcelWriter 对象 // ExcelWriterBuilder writerBuilder = EasyExcel.write(fileName).registerWriteHandler(horizontalCellStyleStrategy); // ExcelWriterSheetBuilder sheetBuilder = writerBuilder.sheet("Sheet1"); //创建头 WriteTable table = new WriteTable(); List<String> tableFirst =new ArrayList<>(); List<String> tableSecond =new ArrayList<>(); tableFirst.add("测试表头1"); tableFirst.add("测试表头1"); tableSecond.add("第二表头1"); tableSecond.add("第二表头2"); List<List<String>> headList = new ArrayList<List<String>>(); if (tableFirst != null && tableFirst.size() > 0) { int size = tableFirst.size(); for (int i = 0; i < size; i++) { List<String> headTitle = new ArrayList<String>(); for (int j = 0; j < tableFirst.size(); j++) { if (j == i) { headTitle.add(tableFirst.get(j)); break; } } if (tableSecond != null && tableSecond.size() > 0) { for (int k = 0; k < tableSecond.size(); k++) { if (k == i) { headTitle.add(tableSecond.get(k)); break; } } headList.add(headTitle); } } } table.setHead(headList); // 写入数据 excelWriter.write(data,writeSheet,table); excelWriter.finish(); // sheetBuilder.doWrite(data); // 输出完成提示 System.out.println("Excel 导出完成!"); } //配置字体,表头背景等 private static HorizontalCellStyleStrategy setConfigure() { // 头的策略 WriteCellStyle headWriteCellStyle = new WriteCellStyle(); // 背景色 headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex()); WriteFont headWriteFont = new WriteFont(); headWriteFont.setFontHeightInPoints((short) 13); headWriteCellStyle.setWriteFont(headWriteFont); headWriteCellStyle.setWrapped(false); // 内容的策略 WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); // 字体策略 WriteFont contentWriteFont = new WriteFont(); // 字体大小 contentWriteFont.setFontHeightInPoints((short) 10); contentWriteFont.setColor(IndexedColors.RED.getIndex()); contentWriteCellStyle.setWriteFont(contentWriteFont); //边框 //导出数据垂直居中 contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //导出数据水平居中 contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); contentWriteCellStyle.setBorderLeft(BorderStyle.THIN); contentWriteCellStyle.setBorderTop(BorderStyle.THIN); contentWriteCellStyle.setBorderRight(BorderStyle.THIN); contentWriteCellStyle.setBorderBottom(BorderStyle.THIN); //设置 自动换行 contentWriteCellStyle.setWrapped(true); //设置 // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现 return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); } private static List<DemoData1> getData() { List<DemoData1> data = new ArrayList<>(); // 添加示例数据 data.add(new DemoData1("张三", 20)); data.add(new DemoData1("李四", 25)); data.add(new DemoData1("王五", 30)); return data; } } class DemoData1 { private String name; private Integer age; public DemoData1(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
GitHub 加速计划 / ea / easyexcel
31.64 K
7.47 K
下载
快速、简洁、解决大文件内存溢出的java处理Excel工具
最近提交(Master分支:3 个月前 )
c42183df
Bugfix 3 个月前
efa7dff6 * 重新加回 `commons-io`
3 个月前
更多推荐
已为社区贡献10条内容
所有评论(0)