Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Walter.Wen
SpringBootFileUploadDownload
提交
06985916
S
SpringBootFileUploadDownload
项目概览
Walter.Wen
/
SpringBootFileUploadDownload
通知
5
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
1
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringBootFileUploadDownload
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
06985916
编写于
4月 02, 2020
作者:
W
wenguang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Excel文件写入测试用例添加
上级
e0f1d65d
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
110 addition
and
8 deletion
+110
-8
pom.xml
pom.xml
+12
-0
src/main/java/com/we/controller/ExcelController.java
src/main/java/com/we/controller/ExcelController.java
+2
-2
src/main/java/com/we/pojo/Student.java
src/main/java/com/we/pojo/Student.java
+4
-5
src/main/resources/application.yml
src/main/resources/application.yml
+7
-0
src/test/java/com/we/WriteTest.java
src/test/java/com/we/WriteTest.java
+85
-1
未找到文件。
pom.xml
浏览文件 @
06985916
...
...
@@ -91,6 +91,18 @@
<version>
1.2.67
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
<version>
2.1.3.RELEASE
</version>
</dependency>
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
slf4j-log4j12
</artifactId>
<version>
1.7.6
</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
src/main/java/com/we/controller/ExcelController.java
浏览文件 @
06985916
...
...
@@ -36,7 +36,7 @@ public class ExcelController {
*/
@RequestMapping
(
"/exportExcel"
)
public
String
exportExcel
()
{
List
stuList
=
studentService
.
selectStuList
();
List
<
Student
>
stuList
=
studentService
.
selectStuList
();
String
path
=
"F:/Desktop/"
;
String
fileName
=
path
+
"学生信息表格"
+
System
.
currentTimeMillis
()
+
".xlsx"
;
EasyExcel
.
write
(
fileName
,
Student
.
class
).
sheet
(
"学生信息表格"
).
doWrite
(
stuList
);
...
...
@@ -50,7 +50,7 @@ public class ExcelController {
*/
@RequestMapping
(
"/downloadExcel"
)
public
void
downloadExcel
(
HttpServletResponse
response
)
throws
IOException
{
List
stuList
=
studentService
.
selectStuList
();
List
<
Student
>
stuList
=
studentService
.
selectStuList
();
response
.
setContentType
(
"application/vnd.ms-excel"
);
response
.
setCharacterEncoding
(
"utf-8"
);
// 这里URLEncoder.encode可以防止中文乱码
...
...
src/main/java/com/we/pojo/Student.java
浏览文件 @
06985916
package
com.we.pojo
;
import
com.alibaba.excel.annotation.ExcelProperty
;
import
com.alibaba.excel.metadata.BaseRowModel
;
import
lombok.Data
;
import
lombok.Getter
;
import
lombok.Setter
;
...
...
@@ -11,12 +10,12 @@ import lombok.ToString;
@Getter
@Setter
@ToString
public
class
Student
extends
BaseRowModel
{
@ExcelProperty
(
value
=
"学号"
,
index
=
0
)
public
class
Student
{
@ExcelProperty
(
value
=
{
"基础信息"
,
"学号"
}
,
index
=
0
)
private
String
stuid
;
@ExcelProperty
(
value
=
"班级"
,
index
=
1
)
@ExcelProperty
(
value
=
{
"基础信息"
,
"班级"
}
,
index
=
1
)
private
String
stuclass
;
@ExcelProperty
(
value
=
"姓名"
,
index
=
2
)
@ExcelProperty
(
value
=
{
"基础信息"
,
"姓名"
}
,
index
=
2
)
private
String
stuname
;
@ExcelProperty
(
value
=
"年龄"
,
index
=
3
)
private
String
stuage
;
...
...
src/main/resources/application.yml
浏览文件 @
06985916
...
...
@@ -6,6 +6,13 @@ spring:
username
:
root
password
:
1234
driver-class-name
:
com.mysql.cj.jdbc.Driver
redis
:
host
:
127.0.0.1
port
:
6379
password
:
#连接超时时间(毫秒)
timeout
:
30000
thymeleaf
:
prefix
:
classpath:/templates/
suffix
:
.html
...
...
src/test/java/com/we/WriteTest.java
浏览文件 @
06985916
package
com.we
;
import
com.alibaba.excel.EasyExcel
;
import
com.alibaba.excel.ExcelWriter
;
import
com.alibaba.excel.write.metadata.WriteSheet
;
import
com.we.pojo.Student
;
import
com.we.service.IStudentService
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Set
;
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
classes
=
MainApplication
.
class
,
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
DEFINED_PORT
)
@SpringBootTest
//
(classes = MainApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public
class
WriteTest
{
public
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
WriteTest
.
class
);
@Autowired
private
IStudentService
studentService
;
...
...
@@ -20,8 +30,82 @@ public class WriteTest {
@Test
public
void
selectAll
()
{
List
<
Student
>
stus
=
studentService
.
selectStuList
();
log
.
info
(
"查询结果:"
);
System
.
out
.
println
(
stus
.
get
(
0
));
}
@Test
public
void
simpleWrite
()
{
List
<
Student
>
stuList
=
studentService
.
selectStuList
();
String
path
=
"F:/Desktop/"
;
String
fileName
=
path
+
"学生信息表格"
+
System
.
currentTimeMillis
()
+
".xlsx"
;
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel
.
write
(
fileName
,
Student
.
class
).
sheet
(
"学生信息"
).
doWrite
(
stuList
);
}
/**
* 指定不导出的列
*/
@Test
public
void
excludeOrIncludeWriter
()
{
List
<
Student
>
stuList
=
studentService
.
selectStuList
();
String
path
=
"F:/Desktop/"
;
String
fileName
=
path
+
"学生信息表格"
+
System
.
currentTimeMillis
()
+
".xlsx"
;
Set
<
String
>
excludeColumnFiledNames
=
new
HashSet
<>();
excludeColumnFiledNames
.
add
(
"stuage"
);
EasyExcel
.
write
(
fileName
,
Student
.
class
).
excludeColumnFiledNames
(
excludeColumnFiledNames
).
sheet
(
"学生信息"
).
doWrite
(
stuList
);
}
/**
* 同一个对象,写入不同的sheet
*/
@Test
public
void
repeatedWrite1
()
{
List
<
Student
>
stuList
=
studentService
.
selectStuList
();
String
path
=
"F:/Desktop/"
;
String
fileName
=
path
+
"学生信息表格"
+
System
.
currentTimeMillis
()
+
".xlsx"
;
ExcelWriter
excelWriter
=
EasyExcel
.
write
(
fileName
,
Student
.
class
).
build
();
for
(
int
i
=
0
;
i
<
2
;
i
++)
{
WriteSheet
writeSheet
=
EasyExcel
.
writerSheet
(
i
,
"信息"
+
i
).
build
();
excelWriter
.
write
(
stuList
,
writeSheet
);
}
excelWriter
.
finish
();
}
/**
* 不同的对象,不同的sheet
*/
@Test
public
void
repeatedWrite2
()
{
List
<
Student
>
stuList
=
studentService
.
selectStuList
();
String
path
=
"F:/Desktop/"
;
String
fileName
=
path
+
"学生信息表格"
+
System
.
currentTimeMillis
()
+
".xlsx"
;
ExcelWriter
excelWriter
=
EasyExcel
.
write
(
fileName
).
build
();
for
(
int
i
=
0
;
i
<
2
;
i
++)
{
// Student.class 可以每次都变
if
(
i
==
0
)
{
WriteSheet
writeSheet
=
EasyExcel
.
writerSheet
(
i
,
"信息"
+
i
).
head
(
Student
.
class
).
build
();
excelWriter
.
write
(
stuList
,
writeSheet
);
}
if
(
i
==
1
)
{
Set
<
String
>
excludeColumnFiledNames
=
new
HashSet
<>();
excludeColumnFiledNames
.
add
(
"stuage"
);
WriteSheet
writeSheet
=
EasyExcel
.
writerSheet
(
i
,
"信息"
+
i
).
excludeColumnFiledNames
(
excludeColumnFiledNames
).
head
(
Student
.
class
).
build
();
excelWriter
.
write
(
stuList
,
writeSheet
);
}
}
excelWriter
.
finish
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录