既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
<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;
}
项目样图:这里把目录也给大家看一下
![在这里插入图片描述](https://img-blog.csdnimg.cn/0c3dfdee983a437280c65b5bdfea79a9.png)
### 实现类
**说明:**在Service下创建StudentService,在Impl下创建StudentServiceImpl
public interface StudentService {
//生成数据方法
public List initFillData();
//生成Excel方法
public void parse(String templateDir, String templateName, String excelPath, Map<String, Object> data)throws IOException, TemplateException;
}
项目样图:
![在这里插入图片描述](https://img-blog.csdnimg.cn/225814791c3740ce918b15f2dca7ca90.png)
@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 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;
}
}
项目样图:
![在这里插入图片描述](https://img-blog.csdnimg.cn/14be5f4a5a604e30bc30ca5bb625898a.png)
## 测试类
**说明:**要注意模板位置与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();
}
}
}
项目样图:
![在这里插入图片描述](https://img-blog.csdnimg.cn/49628f11fd074ebda66bb1e871994b35.png)
### Freemarker模板
**说明:**最重要的部分,可能会有些看不懂所以得了解Freemarker的语法才可以。excelCapacity 是表示多少条进行分页。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?mso-application progid="Excel.Sheet"?>
Apache POI
chenqingtao
2020-03-16T08:20:00Z
2022-11-03T07:31:58Z
9DC2DE9132B4460BB7A5E14BF585E55C
2052-11.1.0.12650
24225
12540
False
False
<Style ss:ID="s49"/>
</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”)}”>
学生姓名 学生ID <#assign fromIndex = excelCapacity \* pageSize /> <#if fromIndex gte totalCount> <#assign fromIndex = totalCount /><#list studentList[num…<fromIndex] as student>
s
t
u
d
e
n
t
.
n
a
m
e
<
/
D
a
t
a
>
<
/
C
e
l
l
>
<
C
e
l
l
>
<
D
a
t
a
s
s
:
T
y
p
e
=
"
N
u
m
b
e
r
"
>
{student.name}</Data> </Cell> <Cell> <Data ss:Type="Number">
student.name</Data></Cell><Cell><Datass:Type="Number">{student.id}
<#assign num=num+1 />
<#if num == fromIndex >
<#break>
</#if>
</#list>
<#assign num = fromIndex />
</Table>