JAVA问题解决方案02.Freemarker导出Excel超出上限分表解决方案
Posted 陈老老老板
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA问题解决方案02.Freemarker导出Excel超出上限分表解决方案相关的知识,希望对你有一定的参考价值。
陈老老老板🦸 👨💻本文专栏: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
pom.xml文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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模板
说明:最重要的部分,可能会有些看不懂所以得了解Freemarker的语法才可以。excelCapacity 是表示多少条进行分页。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Excel.Sheet"?>
<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 POI</Author>
<LastAuthor>chenqingtao</LastAuthor>
<Created>2020-03-16T08:20:00Z</Created>
<LastSaved>2022-11-03T07:31:58Z</LastSaved>
</DocumentProperties>
<CustomDocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<ICV dt:dt="string">9DC2DE9132B4460BB7A5E14BF585E55C</ICV>
<KSOProductBuildVer dt:dt="string">2052-11.1.0.12650</KSOProductBuildVer>
</CustomDocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowWidth>24225</WindowWidth>
<WindowHeight>12540</WindowHeight>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Center"/>
<Borders/>
<Font ss:FontName="等线" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s49"/>
<Style ss:ID="s50">
<Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="14" ss:Bold="1"/>
<Interior ss:Color="#C0C0C0" ss:Pattern="Solid"/>
</Style>
</Styles>
<#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>
<Worksheet ss:Name="表名称$pageSize?string("number")">
<Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1" x:FullRows="1"
ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
<Column ss:Index="1" ss:StyleID="Default" ss:AutoFitWidth="0" ss:Width="120" ss:Span="1"/>
<Row ss:Height="30">
<Cell ss:StyleID="s50">
<Data ss:Type="String">学生姓名</Data>
</Cell>
<Cell ss:StyleID="s50">
<Data ss:Type="String">学生ID</Data>
</Cell>
</Row>
<#assign fromIndex = excelCapacity * pageSize />
<#if fromIndex gte totalCount>
<#assign fromIndex = totalCount />
</#if>
<#list studentList[num..<fromIndex] as student>
<Row>
<Cell>
<Data ss:Type="String">$student.name</Data>
</Cell>
<Cell>
<Data ss:Type="Number">$student.id</Data>
</Cell>
</Row>
<#assign num=num+1 />
<#if num == fromIndex >
<#break>
</#if>
</#list>
<#assign num = fromIndex />
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Left="0.7" x:Right="0.7" x:Top="0.75" x:Bottom="0.75"/>
</PageSetup>
<Selected/>
<TopRowVisible>0</TopRowVisible>
<LeftColumnVisible>0</LeftColumnVisible>
<PageBreakZoom>100</PageBreakZoom>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>7</ActiveRow>
<ActiveCol>3</ActiveCol>
<RangeSelection>R8C4</RangeSelection>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<#assign forCount = forCount +1/>
<#if forCount == pages>
<#break>
</#if>
</#list>
<#else>
<Worksheet ss:Name="0">
<Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1" x:FullRows="1"
ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
<Column ss:Index="1" ss:StyleID="Default" ss:AutoFitWidth="0" ss:Width="120" ss:Span="1"/>
<Row ss:Height="30">
<Cell ss:StyleID="s50">
<Data ss:Type="String">学生姓名Java web中不同浏览器间导出Excel文件名称乱码问题解决方案
解决java poi导出excel2003不能超过65536行的问题
JAVA问题解决方案01.EasyExcel导出数据超过Excel单表上限解决方案
JAVA POI XSSFWorkbook导出扩展名为xlsx的Excel,附带weblogic 项目导出Excel文件错误的解决方案