赞
踩
https://gitee.com/DanShenGuiZu/learnDemo/tree/master/easyExcel_learn
EasyExcel支持调整行高、列宽、背景色、字体大小等内容,但是控制方式与使用原生POI无异,比较繁琐,不建议使用。
可以使用模板填充的方式,向预设样式的表格中直接写入数据,写入数据的时候会保持原有样式。
为了节省内存,所以没有采用把整个文档在内存中组织好之后再整体写入到文件的做法,而是采用的是一行一行写入的方式,不能实现删除和移动行,也不支持备注写入。
多组数据写入的时候,如果需要新增行,只能在最后一行增加,不能在中间位置添加。
Excel表格中用{} 来表示包裹要填充的变量,如果单元格文本中本来就有{
、}
左右大括号,需要在括号前面使用斜杠转义\{
、\}
。
代码中被填充数据的实体对象的成员变量名或被填充map集合的key需要和Excel中被{}包裹的变量名称一致。
编写封装填充数据的类或选用Map
package fei.zhou.easyexcel_learn.business.demo3;
import lombok.Data;
@Data
public class FillData {
private String name;
private int age;
}
package fei.zhou.easyexcel_learn.business.demo3; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.write.builder.ExcelWriterBuilder; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; public class Test { public static void main(String[] args) throws FileNotFoundException { // 加载模板 String filePath = "D:\\java\\workSpace-learn\\learnDemo\\easyExcel_learn\\src\\main\\java\\fei\\zhou\\easyexcel_learn\\business\\demo3\\template01.xlsx"; InputStream templateFile = new FileInputStream(filePath); // 写入文件 String targetFileName = "单组数据填充.xlsx"; // 准备对象数据填充 FillData fillData = new FillData(); fillData.setName("王五"); fillData.setAge(10); // 生成工作簿对象 ExcelWriterBuilder workBookWriter = EasyExcel.write(targetFileName).withTemplate(templateFile); // 获取工作表并填充 workBookWriter.sheet().doFill(fillData); // 使用Map数据填充 // HashMap<String, String> mapFillData = new HashMap<>(); // mapFillData.put("name", "王五Map"); // mapFillData.put("age", "11"); // 获取第一个工作表填充并自动关闭流 // workBookWriter.sheet().doFill(mapFillData); } }
Excel表格中用{.}
来表示包裹要填充的变量,如果单元格文本中本来就有{
、}
左右大括号,需要在括号前面使用斜杠转义\{
、\}
。
代码中被填充数据的实体对象的成员变量名或被填充map集合的key需要和Excel中被{}包裹的变量名称一致。
编写封装填充数据的类或选用Map
package fei.zhou.easyexcel_learn.business.demo4;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class FillData {
private String name;
private int age;
}
package fei.zhou.easyexcel_learn.business.demo4; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.write.builder.ExcelWriterBuilder; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class Test { public static void main(String[] args) throws FileNotFoundException { // 加载模板 String filePath = "D:\\java\\workSpace-learn\\learnDemo\\easyExcel_learn\\src\\main\\java\\fei\\zhou\\easyexcel_learn\\business\\demo4\\template02.xlsx"; InputStream templateFile = new FileInputStream(filePath); // 写入文件 String targetFileName = "多组数据填充.xlsx"; // 准备对象数据填充 List<FillData> fillDatas = new ArrayList(); fillDatas.add(new FillData("王五1", 11)); fillDatas.add(new FillData("王五2", 12)); // 生成工作簿对象 ExcelWriterBuilder workBookWriter = EasyExcel.write(targetFileName).withTemplate(templateFile); // 获取第一个工作表填充并自动关闭流 workBookWriter.sheet().doFill(fillDatas); } }
FillConfig
对象设置换行。package fei.zhou.easyexcel_learn.business.demo5;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class FillData {
private String name;
private int age;
}
package fei.zhou.easyexcel_learn.business.demo5; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.fill.FillConfig; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class Test { public static void main(String[] args) throws FileNotFoundException { // 加载模板 String filePath = "D:\\java\\workSpace-learn\\learnDemo\\easyExcel_learn\\src\\main\\java\\fei\\zhou\\easyexcel_learn\\business\\demo5\\template03.xlsx"; InputStream templateFile = new FileInputStream(filePath); // 写入文件 String targetFileName = "组合数据填充.xlsx"; // 准备填充数据 List<FillData> fillDatas = new ArrayList(); fillDatas.add(new FillData("王五1", 11)); fillDatas.add(new FillData("王五2", 12)); // 生成工作簿对象 ExcelWriter excelWriter = EasyExcel.write(targetFileName).withTemplate(templateFile).build(); // 生成工作表对象 WriteSheet writeSheet = EasyExcel.writerSheet().build(); // 组合填充时,因为多组填充的数据量不确定,需要在多组填充完之后另起一行 FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build(); // 填充并换行 excelWriter.fill(fillDatas, fillConfig, writeSheet); // 准备填充数据 HashMap<String, String> otherData = new HashMap<>(); otherData.put("date", "2020-03-14"); otherData.put("total", "100"); // 填充 excelWriter.fill(otherData, writeSheet); // 关闭 excelWriter.finish(); } }
水平填充和多组填充模板一样,不一样的地方在于,填充时需要通过FillConfig
对象设置水平填充。
@Data
@AllArgsConstructor
public class FillData {
private String name;
private int age;
}
package fei.zhou.easyexcel_learn.business.demo6; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.enums.WriteDirectionEnum; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.fill.FillConfig; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class Test { public static void main(String[] args) throws FileNotFoundException { // 加载模板 String filePath = "D:\\java\\workSpace-learn\\learnDemo\\easyExcel_learn\\src\\main\\java\\fei\\zhou\\easyexcel_learn\\business\\demo6\\template04.xlsx"; InputStream templateFile = new FileInputStream(filePath); // 写入文件 String targetFileName = "水平数据填充.xlsx"; // 准备填充数据 List<FillData> fillDatas = new ArrayList(); fillDatas.add(new FillData("王五1", 11)); fillDatas.add(new FillData("王五2", 12)); // 生成工作簿对象 ExcelWriter excelWriter = EasyExcel.write(targetFileName).withTemplate(templateFile).build(); // 生成工作表对象 WriteSheet writeSheet = EasyExcel.writerSheet().build(); // 组合填充时,因为多组填充的数据量不确定,需要在多组填充完之后另起一行 FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); // 填充 excelWriter.fill(fillDatas, fillConfig, writeSheet); HashMap<String, String> otherData = new HashMap<>(); otherData.put("date", "2020-03-14"); otherData.put("total", "100"); excelWriter.fill(otherData, writeSheet); // 关闭 excelWriter.finish(); } }
package fei.zhou.easyexcel_learn.business.demo7; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; @Data @NoArgsConstructor @AllArgsConstructor public class Student { private String name; private String gender; private Date birthday; private String id; }
package fei.zhou.easyexcel_learn.business.demo7; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.enums.WriteDirectionEnum; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.fill.FillConfig; import fei.zhou.easyexcel_learn.business.demo6.FillData; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; public class Test { public static void main(String[] args) throws FileNotFoundException { // 加载模板 String filePath = "D:\\java\\workSpace-learn\\learnDemo\\easyExcel_learn\\src\\main\\java\\fei\\zhou\\easyexcel_learn\\business\\demo7\\template05.xlsx"; InputStream templateFile = new FileInputStream(filePath); // 写入文件 String targetFile = "运营数据统计.xlsx"; // 写入workbook对象 ExcelWriter workBook = EasyExcel.write(targetFile, FillData.class).withTemplate(templateFile).build(); WriteSheet sheet = EasyExcel.writerSheet().build(); // 填充配置,开启组合填充换行 //FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build(); // ****** 准备数据 ******* // 日期 HashMap<String, String> dateMap = new HashMap<String, String>(); dateMap.put("date", "2020-03-16"); // 总会员数 HashMap<String, String> totalCountMap = new HashMap<String, String>(); dateMap.put("totalCount", "1000"); // 新增员数 HashMap<String, String> increaseCountMap = new HashMap<String, String>(); dateMap.put("increaseCount", "100"); // 本周新增会员数 HashMap<String, String> increaseCountWeekMap = new HashMap<String, String>(); dateMap.put("increaseCountWeek", "50"); // 本月新增会员数 HashMap<String, String> increaseCountMonthMap = new HashMap<String, String>(); dateMap.put("increaseCountMonth", "100"); // 新增会员数据 List<Student> students = new ArrayList<>(); students.add(new Student("张三1", "男", new Date(), "1")); students.add(new Student("张三2", "男", new Date(), "2")); // **** 准备数据结束**** // 写入统计数据 workBook.fill(dateMap, sheet); workBook.fill(totalCountMap, sheet); workBook.fill(increaseCountMap, sheet); workBook.fill(increaseCountWeekMap, sheet); workBook.fill(increaseCountMonthMap, sheet); // 写入新增会员 workBook.fill(students, sheet); workBook.finish(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。