陈老老老板🦸 👨💻本文专栏:Java问题解决方案(都是一些常见的问题解决方案)
👨💻本文简述:本文讲一下有关Freemarker导出Excel分表的解决方案,超级详细。
👨💻上一篇文章:01.EasyExcel导出数据超过Excel单表上限解决方案
👨💻有任何问题,都可以私聊我,我能帮得上的一定帮忙,感谢大佬们支持。
🦹如果喜欢可以投个票吗?在文章最后,感谢感谢!
说明:
逻辑与EasyExcel其实差不多那个更简单一点,这个主要是Freemarker语法更复杂。
思考逻辑:
1.了解一下Excel单表最多存储多少行数据(可以存储1048576条数据,1024的平方,2的20次方)。
2.知道最多多少行就能以这个数为条件,如果超过则进行分表。
3.分表的同时需要对数据进行分割,才能不超过最大限度。
4.对Freemarker的语法要有充分的了解。(在方案之后会展示)
注: 这就是简单的demo,有关于自己项目中的逻辑与不同的自己加。前面是Freemarker生成Excel
项目运行环境:
idea2020.2
jdk1.8
springboot 2.7.5
org.springframework.boot spring-boot-starter-freemarker org.springframework.boot spring-boot-starter-web org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
说明:在domain下建立实体类Student
@Data
public class Student {private Integer id;private String name;
}
项目样图:这里把目录也给大家看一下
说明:在Service下创建StudentService,在Impl下创建StudentServiceImpl
public interface StudentService {//生成数据方法public List initFillData();//生成Excel方法public void parse(String templateDir, String templateName, String excelPath, Map data)throws IOException, TemplateException;
}
项目样图:
@Service
public class StudentServiceImpl implements StudentService{/*** 解析模板生成Excel* @param templateDir 模板目录* @param templateName 模板名称* @param excelPath 生成的Excel文件路径* @param data 数据参数*/public void parse(String templateDir,String templateName,String excelPath,Map data) throws IOException, TemplateException {//初始化工作Configuration cfg = new Configuration();//设置默认编码格式为UTF-8cfg.setDefaultEncoding("UTF-8");//全局数字格式cfg.setNumberFormat("0.00");//设置模板文件位置cfg.setDirectoryForTemplateLoading(new File(templateDir));cfg.setObjectWrapper(new DefaultObjectWrapper());//加载模板Template template = cfg.getTemplate(templateName,"utf-8");OutputStreamWriter writer = null;try{//填充数据至Excelwriter = new OutputStreamWriter(new FileOutputStream(excelPath),"UTF-8");template.process(data, writer);writer.flush();}finally{writer.close();}}
// 生成数据方式public List initFillData() {ArrayList fillDatas = new ArrayList();for (int i = 1; i < 51; i++) {Student fillData = new Student();fillData.setId(i);fillData.setName("0123456789="+ i);fillDatas.add(fillData);}return fillDatas;}}
项目样图:
说明:要注意模板位置与excel生成位置,自己更改或先创建目录。
@SpringBootTest
class FreemarkerApplicationTests {@Autowiredprivate StudentService studentService;@Testpublic void excelTest(){List students = studentService.initFillData();int totalCount = students.size();Map data = new HashMap();data.put("studentList", students);data.put("totalCount",totalCount);try {//这里前面是模板位置,后面是生成的excel位置studentService.parse("D:\\study\\projecct\\freemarker\\src\\main\\resources\\templates\\", "Freemark.xml","E:\\excel\\excelTest30.xls", data);} catch (IOException e) {e.printStackTrace();} catch (TemplateException e) {e.printStackTrace();}}}
项目样图:
说明:最重要的部分,可能会有些看不懂所以得了解Freemarker的语法才可以。excelCapacity 是表示多少条进行分页。
Apache POI chenqingtao 2020-03-16T08:20:00Z 2022-11-03T07:31:58Z 9DC2DE9132B4460BB7A5E14BF585E55C 2052-11.1.0.12650 24225 12540 False False <#assign num = 0 /><#assign forCount = 0/><#assign excelCapacity = 10/><#assign pages = 0/><#if totalCount % excelCapacity == 0><#assign pages = totalCount / excelCapacity /><#else><#assign pages = (totalCount / excelCapacity) +1 />#if><#if totalCount gt excelCapacity>
<#list 1..pages as pageSize>
学生姓名 | 学生ID |
<#assign fromIndex = excelCapacity * pageSize />
<#if fromIndex gte totalCount>
<#assign fromIndex = totalCount />
#if><#list studentList[num..
${student.name}
|
${student.id}
|
<#assign num=num+1 />
<#if num == fromIndex >
<#break>#if>#list>
<#assign num = fromIndex />
0
0
100
3 7 3 R8C4
False
False
<#assign forCount = forCount +1/>
<#if forCount == pages>
<#break>#if>#list><#else>
学生姓名 | 学生ID |
<#list studentList as student>${student.name} | ${student.id} |
#list>
0
0
100
3 7 3 R8C4
False
False
#if>
在模板中可以定义三种类型的变量:
'‘简单’'变量: 它能从模板中的任何位置来访问,或者从使用 include 指令引入的模板访问。可以使用 assign 指令来创建或替换这些变量。因为宏和方法只是变量,那么 macro 指令 和 function 指令 也可以用来设置变量,就像 assign 那样。
局部变量:它们只能被设置在 宏定义体内, 而且只在宏内可见。一个局部变量的生命周期只是宏的调用过程。可以使用 local指令 在宏定义体内创建或替换局部变量。
循环变量:循环变量是由如 list 指令自动创建的,而且它们只在指令的开始和结束标记内有效。宏 的参数是局部变量而不是循环变量。
全局变量:这是一个高级话题了, 并且这种变量最好别用。即便它们属于不同的命名空间, 全局变量也被所有模板共享,因为它们是被 import进来的, 不同于 include 进来的。那么它们的可见度就像数据模型那样。 全局变量通过 global指令来定义。
示例:使用 assign 创建和替换变量:
<#assign x = 1> <#-- create variable x -->
${x}
<#assign x = x + 3> <#-- replace variable x -->
${x}
你可以使用 if, elseif 和 else 指令来条件判断是否越过模板的一个部分。 condition 必须计算成布尔值, 否则错误将会中止模板处理。elseif 和 else 必须出现在 if 内部 (也就是,在 if 的开始标签和结束标签之间)。 if 中可以包含任意数量的 elseif(包括0个) 而且结束时 else 是可选的。比如:
只有 if 没有 elseif 和 else:
<#if x == 1>x is 1
#if>
只有 if 没有 elseif 但是有 else:
<#if x == 1>x is 1
<#else>x is not 1
#if>
有 if 和两个 elseif 但是没有 else:
<#if x == 1>x is 1
<#elseif x == 2>x is 2
<#elseif x == 3>x is 3
#if>
有 if 和三个 elseif 还有 else:
<#if x == 1>x is 1
<#elseif x == 2>x is 2
<#elseif x == 3>x is 3
<#elseif x == 4>x is 4
<#else>x is not 1 nor 2 nor 3 nor 4
#if>
总结:这是简单的demo,没有关于项目逻辑的,有关模板自己仿照更改就可以了。希望对您有帮助,感谢阅读
结束语:裸体一旦成为艺术,便是最圣洁的。道德一旦沦为虚伪,便是最下流的。
勇敢去做你认为正确的事,不要被世俗的流言蜚语所困扰。