提交 06985916 编写于 作者: W wenguang

Excel文件写入测试用例添加

上级 e0f1d65d
......@@ -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
......@@ -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可以防止中文乱码
......
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;
......
......@@ -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
......
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.
先完成此消息的编辑!
想要评论请 注册