导出模版

This commit is contained in:
chenjianxing 2020-04-15 18:32:57 +08:00
parent 98379ac9e1
commit d65c129f3a
9 changed files with 105 additions and 19 deletions

View File

@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@RequestMapping("/test/case")
@ -76,5 +77,10 @@ public class TestCaseController {
return testCaseService.testCaseImport(file, projectId);
}
@GetMapping("/export/template")
public void testCaseTemplateExport(HttpServletResponse response){
testCaseService.testCaseTemplateExport(response);
}
}

View File

@ -1,24 +1,29 @@
package io.metersphere.excel.domain;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
@ColumnWidth(15)
public class TestCaseExcelData {
@NotBlank
@Length(max=1000)
@ExcelProperty("所属模块")
@Pattern(regexp = "^(?!.*//).*$", message = "格式不正确")
private String nodePath;
@NotBlank
@Length(max=50)
@ExcelProperty("用例名称")
private String name;
@NotBlank
@Length(max=1000)
@ExcelProperty("所属模块")
@ColumnWidth(30)
@Pattern(regexp = "^(?!.*//).*$", message = "格式不正确")
private String nodePath;
@NotBlank
@ExcelProperty("用例类型")
@Pattern(regexp = "(^functional$)|(^performance$)|(^api$)", message = "必须为functional、performance、api")
@ -38,18 +43,22 @@ public class TestCaseExcelData {
@Pattern(regexp = "(^manual$)|(^auto$)", message = "必须为manual、auto")
private String method;
@ColumnWidth(50)
@ExcelProperty("前置条件")
@Length(min=0, max=1000)
private String prerequisite;
@ColumnWidth(50)
@ExcelProperty("备注")
@Length(max=1000)
private String remark;
@ColumnWidth(50)
@ExcelProperty("步骤描述")
@Length(max=1000)
private String stepDesc;
@ColumnWidth(50)
@ExcelProperty("预期结果")
@Length(max=1000)
private String stepResult;

View File

@ -6,7 +6,7 @@ import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.util.StringUtils;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.excel.util.ExcelValidateHelper;
import io.metersphere.excel.utils.ExcelValidateHelper;
import io.metersphere.excel.domain.ExcelErrData;
import java.lang.reflect.Field;

View File

@ -0,0 +1,30 @@
package io.metersphere.excel.utils;
import com.alibaba.excel.EasyExcel;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.exception.ExcelException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
public class EasyExcelUtil {
public static void export(HttpServletResponse response, Class clazz, List data, String fileName, String sheetName) {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
try {
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(data);
} catch (UnsupportedEncodingException e) {
LogUtil.error(e.getMessage(), e);
throw new ExcelException("不支持UTF-8编码");
} catch (IOException e) {
LogUtil.error(e.getMessage(), e);
throw new ExcelException("IO异常");
}
}
}

View File

@ -1,4 +1,4 @@
package io.metersphere.excel.util;
package io.metersphere.excel.utils;
import com.alibaba.excel.annotation.ExcelProperty;

View File

@ -3,15 +3,15 @@ package io.metersphere.exception;
/**
* @author jianxing.chen
*/
public class ExcelImportException extends RuntimeException {
public class ExcelException extends RuntimeException {
private static final long serialVersionUID = 1L;
public ExcelImportException(String message, Exception e){
public ExcelException(String message, Exception e){
super(message, e);
}
public ExcelImportException(String message){
public ExcelException(String message){
super(message);
}

View File

@ -9,7 +9,7 @@ import io.metersphere.base.mapper.TestPlanTestCaseMapper;
import io.metersphere.commons.constants.TestCaseConstants;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.dto.TestCaseNodeDTO;
import io.metersphere.exception.ExcelImportException;
import io.metersphere.exception.ExcelException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -213,7 +213,7 @@ public class TestCaseNodeService {
nodePaths.forEach(path -> {
if (path == null) {
throw new ExcelImportException("所属模块不能为空!");
throw new ExcelException("所属模块不能为空!");
}
List<String> nodeNameList = new ArrayList<>(Arrays.asList(path.split("/")));
Iterator<String> pathIterator = nodeNameList.iterator();
@ -222,7 +222,7 @@ public class TestCaseNodeService {
String rootNodeName = null;
if (nodeNameList.size() <= 1) {
throw new ExcelImportException("创建模块失败:" + path);
throw new ExcelException("创建模块失败:" + path);
} else {
pathIterator.next();
pathIterator.remove();

View File

@ -1,7 +1,9 @@
package io.metersphere.service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.*;
@ -13,6 +15,7 @@ import io.metersphere.excel.domain.ExcelResponse;
import io.metersphere.excel.domain.TestCaseExcelData;
import io.metersphere.excel.listener.EasyExcelListener;
import io.metersphere.excel.listener.TestCaseDataListener;
import io.metersphere.excel.utils.EasyExcelUtil;
import io.metersphere.user.SessionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
@ -23,11 +26,10 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
@Service
@ -216,4 +218,42 @@ public class TestCaseService {
}
sqlSession.flushStatements();
}
public void testCaseTemplateExport(HttpServletResponse response) {
EasyExcelUtil.export(response, TestCaseExcelData.class, generateExportTemplate(), "测试用例模版", "模版");
}
private List<TestCaseExcelData> generateExportTemplate() {
List<TestCaseExcelData> list = new ArrayList<TestCaseExcelData>();
StringBuilder path = new StringBuilder("");
List<String> types = Arrays.asList("functional", "performance", "api");
List<String> methods = Arrays.asList("manual", "auto");
for (int i = 1; i <= 5; i++) {
TestCaseExcelData data = new TestCaseExcelData();
data.setName("测试用例" + i);
path.append("/" + "模块" + i);
data.setNodePath(path.toString());
data.setPriority("P" + i%4);
data.setType(types.get(i%3));
data.setMethod(methods.get(i%2));
data.setPrerequisite("前置条件选填");
data.setStepDesc("1. 每个步骤以换行分隔\n2. 步骤前可标序号\n3. 测试步骤和结果选填");
data.setStepResult("1. 每条结果以换行分隔\n2. 结果前可标序号\n3. 测试步骤和结果选填");
data.setMaintainer("admin");
data.setRemark("备注选填");
list.add(data);
}
list.add(new TestCaseExcelData());
TestCaseExcelData explain = new TestCaseExcelData();
explain.setName("同一项目下测试用例名称不能重复!");
explain.setNodePath("模块名称请按照'/模块1/模块2'的格式书写; 错误格式示例:('/', '/tes//test'); 若无该模块,则自动创建模块");
explain.setType("用例类型必须为functional、performance、api");
explain.setMethod("测试方式必须为manual、auto");
explain.setPriority("优先级必须为P0、P1、P2、P3");
explain.setMaintainer("维护人必须为该工作空间相关人员");
list.add(explain);
return list;
}
}

View File

@ -10,7 +10,8 @@
@close="init">
<el-row>
<el-link type="primary" class="download-template">下载模版</el-link>
<el-link type="primary" class="download-template"
href="/test/case/export/template">下载模版</el-link>
</el-row>
<el-row>