陈老老老板 本文专栏: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
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-freemarkerartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
说明:在domain下建立实体类Student
@Data
public class Student {
private Integer id;
private String name;
}
说明:在Service下创建StudentService,在Impl下创建StudentServiceImpl
public interface StudentService {
//生成数据方法
public List<Student> initFillData();
//生成Excel方法
public void parse(String templateDir, String templateName, String excelPath, Map<String, Object> 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<String,Object> data) throws IOException, TemplateException {
//初始化工作
Configuration cfg = new Configuration();
//设置默认编码格式为UTF-8
cfg.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{
//填充数据至Excel
writer = new OutputStreamWriter(new FileOutputStream(excelPath),"UTF-8");
template.process(data, writer);
writer.flush();
}finally{
writer.close();
}
}
// 生成数据方式
public List<Student> initFillData() {
ArrayList<Student> fillDatas = new ArrayList<Student>();
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 {
@Autowired
private StudentService studentService;
@Test
public void excelTest(){
List<Student> students = studentService.initFillData();
int totalCount = students.size();
Map<String,Object> data = new HashMap<String, Object>();
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 是表示多少条进行分页。
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Apache POIAuthor>
<LastAuthor>chenqingtaoLastAuthor>
<Created>2020-03-16T08:20:00ZCreated>
<LastSaved>2022-11-03T07:31:58ZLastSaved>
DocumentProperties>
<CustomDocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<ICV dt:dt="string">9DC2DE9132B4460BB7A5E14BF585E55CICV>
<KSOProductBuildVer dt:dt="string">2052-11.1.0.12650KSOProductBuildVer>
CustomDocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowWidth>24225WindowWidth>
<WindowHeight>12540WindowHeight>
<ProtectStructure>FalseProtectStructure>
<ProtectWindows>FalseProtectWindows>
ExcelWorkbook>
<Styles>