使用Spring boot集成freemarker可以将一些复杂的excel表格导出
下面直接开始使用步骤:
1. 集成freemarker
在pom.xml文件中导入springfreemarker的依赖
<dependencies>
<!--freemarker依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<!--测试类需要用到-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
2. 创建实体类与excel文档模板
将atp.xsl文件另存为xml格式,得到一个atp.xml文件
3. 创建一个测试类,用于测试导出文件
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.junit.Test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExportExcelTest {
@Test
public void run() throws Exception{
Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
configuration.setDefaultEncoding("utf-8");
configuration.setClassForTemplateLoading(ExportExcelTest.class,"/templates/");
Template template = configuration.getTemplate("atp.xml");
Person person1 = new Person();
Person person2 = new Person();
Person person3 = new Person();
person1.setId("1");
person1.setName("pan");
person2.setId("2");
person2.setName("boot");
person3.setId("3");
person3.setName("pan66");
Map<String,Object> map = new HashMap<>();
List<Person> list = new ArrayList<>();
list.add(person1);
list.add(person2);
list.add(person3);
//带有实体类的list集合
map.put("list",list);
List<Map<String,Object>> mapList = new ArrayList<>();
for (int i = 0; i <2; i++) {
Map<String,Object> map1 = new HashMap<>();
map1.put("total","123456"+i);
map1.put("success","80"+i);
mapList.add(map1);
}
//带有map集合的list集合
map.put("mapList",mapList);
File file = new File("src/main/resources/templates/marti.xls");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
template.process(map,bw);
bw.flush();
bw.close();
System.out.println("导出成功");
}
}
4. 对atp.xml文件进行修改,添加替换符
<?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>Pan</Author>
<LastAuthor>Pan</LastAuthor>
<Created>2016-12-02T08:54:00Z</Created>
<LastSaved>2022-08-22T08:06:13Z</LastSaved>
</DocumentProperties>
<CustomDocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<KSOProductBuildVer dt:dt="string">2052-11.1.0.12302</KSOProductBuildVer>
<ICV dt:dt="string">470D5A143AE34423B1D07CFE37DD2552</ICV>
</CustomDocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowWidth>12215</WindowWidth>
<WindowHeight>7835</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="12"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s49">
<Alignment ss:Vertical="Center"/>
<Borders/>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="12"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s50">
<Alignment ss:Vertical="Center"/>
</Style>
<Style ss:ID="s51">
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="8" ss:ExpandedRowCount="6" x:FullColumns="1" x:FullRows="1"
ss:DefaultColumnWidth="54" ss:DefaultRowHeight="15.6">
<Row ss:Index="5">
<Cell>
<Data ss:Type="String">测试1</Data>
</Cell>
<Cell>
<Data ss:Type="String">测试2</Data>
</Cell>
</Row>
<#list mapList as map>
<#if (map_index < 5 )>
<Row>
<Cell>
<Data ss:Type="String">${map["total"]}</Data>
</Cell>
<Cell>
<Data ss:Type="String">${map["success"]}</Data>
</Cell>
</Row>
</#if>
</#list>
<Row ss:Index="11">
<Cell>
<Data ss:Type="String">测试1</Data>
</Cell>
<Cell>
<Data ss:Type="String">测试2</Data>
</Cell>
</Row>
<#list list as list>
<Row>
<Cell>
<Data ss:Type="String">${list.id}</Data>
</Cell>
<Cell>
<Data ss:Type="String">${list.name}</Data>
</Cell>
</Row>
</#list>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.511805555555556"/>
<Footer x:Margin="0.511805555555556"/>
</PageSetup>
<Selected/>
<TopRowVisible>0</TopRowVisible>
<LeftColumnVisible>0</LeftColumnVisible>
<PageBreakZoom>100</PageBreakZoom>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>6</ActiveRow>
<ActiveCol>1</ActiveCol>
<RangeSelection>R7C2</RangeSelection>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet2">
<Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" x:FullRows="1"
ss:DefaultColumnWidth="54" ss:DefaultRowHeight="15.6"/>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.511805555555556"/>
<Footer x:Margin="0.511805555555556"/>
</PageSetup>
<TopRowVisible>0</TopRowVisible>
<LeftColumnVisible>0</LeftColumnVisible>
<PageBreakZoom>100</PageBreakZoom>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet3">
<Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" x:FullRows="1"
ss:DefaultColumnWidth="54" ss:DefaultRowHeight="15.6"/>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.511805555555556"/>
<Footer x:Margin="0.511805555555556"/>
</PageSetup>
<TopRowVisible>0</TopRowVisible>
<LeftColumnVisible>0</LeftColumnVisible>
<PageBreakZoom>100</PageBreakZoom>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
目前自己用到的就是这些,主要是对list集合进行分析