ExcelInputService.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package com.mooctest.service;
  2. import com.mooctest.dao.ExtendBugDao;
  3. import com.mooctest.dao.TaskDao;
  4. import com.mooctest.event.EventUtil;
  5. import com.mooctest.model.ExtendBug;
  6. import com.mooctest.model.Task;
  7. import com.mooctest.util.ImportDataMap;
  8. import org.apache.commons.io.IOUtils;
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  10. import org.apache.poi.ss.usermodel.Cell;
  11. import org.apache.poi.ss.usermodel.Row;
  12. import org.apache.poi.ss.usermodel.Sheet;
  13. import org.apache.poi.ss.usermodel.Workbook;
  14. import org.bson.Document;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.web.multipart.MultipartFile;
  20. import java.io.*;
  21. import java.util.*;
  22. @Service
  23. public class ExcelInputService {
  24. private static Logger logger = LoggerFactory.getLogger(EventUtil.class);
  25. @Autowired
  26. TaskDao taskDao;
  27. @Autowired
  28. ExtendBugDao extendBugDao;
  29. @Autowired
  30. ImportDataMap importDataMap;
  31. // 接收前端文件,并将MultipartFile文件转换成File文件,并返回文件路径
  32. public String saveMultipartFile(MultipartFile file, String fileSaveName){
  33. OutputStream os = null;
  34. String fileFullPath = null;
  35. String fileName = file.getOriginalFilename();
  36. try (
  37. InputStream inputStream = file.getInputStream();
  38. ) {
  39. String path = System.getProperty("user.dir");
  40. // 2、保存到临时文件
  41. // 1K的数据缓冲
  42. byte[] bs = new byte[1024];
  43. // 读取到的数据长度
  44. int len;
  45. // 输出的文件流保存到本地文件
  46. File tempFile = new File(path);
  47. if (!tempFile.exists()) {
  48. tempFile.mkdirs();
  49. }
  50. fileFullPath = tempFile.getPath() + File.separator + fileName;
  51. os = new FileOutputStream(fileFullPath);
  52. // 开始读取
  53. while ((len = inputStream.read(bs)) != -1) {
  54. os.write(bs, 0, len);
  55. }
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }finally {
  61. IOUtils.closeQuietly(os);
  62. }
  63. return fileFullPath;
  64. }
  65. // 解析File文件,并将为数据库表中各字段赋值,并将数据数据插入数据库中
  66. public Map<String, Boolean> excelToMongo(String address, String fileSaveName, Integer comeFrom) {
  67. Integer orderId=UUID.randomUUID().toString().hashCode();
  68. orderId = orderId < 0 ? -orderId : orderId;
  69. Long task_id = Long.valueOf(orderId);
  70. Long case_id = task_id;
  71. String task_name = fileSaveName;
  72. List<String> fieldList = null;
  73. Task task = new Task(task_id, case_id, task_name);
  74. taskDao.save(task);
  75. Map<String, Boolean> insert_map = new HashMap<>();
  76. Map<String, String> typeMap = importDataMap.data.get(comeFrom);
  77. String appTitle = typeMap.get("title");
  78. String appBugCategor = typeMap.get("bug_category");
  79. String appSeverity = typeMap.get("severity");
  80. String appRecurrent= typeMap.get("recurrent");
  81. String appDescription= typeMap.get("description");
  82. // InputStream in = ExcelInputService.class.getClassLoader().getResourceAsStream(comeFrom+"-application.properties");
  83. // //读取文件
  84. // Properties properties=new Properties();
  85. // try {
  86. // properties.load(in);
  87. // } catch (IOException e) {
  88. // e.printStackTrace();
  89. // }
  90. // String appTitle = null;
  91. // String appBugCategor = null;
  92. // String appSeverity = null;
  93. // String appRecurrent= null;
  94. // String appDescription= null;
  95. // Map<String,String> dataMap = ImportDateMap.data.get("1");
  96. // dataMap.get("tit")
  97. // try {
  98. // appTitle = new String(properties.getProperty("title").getBytes("ISO8859-1"), "GBK");
  99. // appBugCategor =new String(properties.getProperty("bug_category").getBytes("ISO8859-1"), "GBK");
  100. // appSeverity = new String(properties.getProperty("severity").getBytes("ISO8859-1"), "GBK");
  101. // appRecurrent= new String(properties.getProperty("recurrent").getBytes("ISO8859-1"), "GBK");
  102. // appDescription= new String(properties.getProperty("description").getBytes("ISO8859-1"), "GBK");
  103. // } catch (UnsupportedEncodingException e) {
  104. // e.printStackTrace();
  105. // }
  106. //
  107. // System.out.println(appTitle);
  108. try {
  109. // 输入文件
  110. FileInputStream inputStream = new FileInputStream(address);
  111. // 根据输入流导入Excel产生Workbook对象
  112. Workbook workbook = new HSSFWorkbook(inputStream);
  113. fieldList = new ArrayList<String>();
  114. // 获取Excel文档中第一个表单
  115. Sheet sheet = workbook.getSheetAt(0);
  116. // 获取表单第一行作为表头
  117. Row topRow = sheet.getRow(0);
  118. if (topRow == null) {
  119. insert_map.put("file_is_null", true);
  120. insert_map.put("insert_success", false);
  121. return insert_map;
  122. } else {
  123. insert_map.put("file_is_null", false);
  124. }
  125. System.out.println("ok~~");
  126. for (Cell cell : topRow) {
  127. fieldList.add(cell.toString());
  128. }
  129. logger.info("文件列表" + fieldList);
  130. System.out.println("文件列表" + fieldList);
  131. // 获得表单的行数
  132. int rows = sheet.getLastRowNum() + 1;
  133. // 获得每行的列数
  134. int colunms = fieldList.size();
  135. // 从第二行开始遍历表格
  136. for (int i = 1; i < rows; i++) {
  137. ExtendBug extendBug = new ExtendBug();
  138. Row row = sheet.getRow(i);
  139. String exam_case_id = task_id+"-"+ case_id;
  140. long create_time_millis = System.currentTimeMillis();
  141. extendBug.setBugCategory("空");
  142. extendBug.setSeverity((short) 0);
  143. extendBug.setRecurrent((short) 0);
  144. extendBug.setTitle("空");
  145. extendBug.setDescription("空");
  146. extendBug.setExamId(task_id+"");
  147. extendBug.setCaseId(case_id+"");
  148. extendBug.setExamCaseId(exam_case_id);
  149. extendBug.setCreateTimeMillis(create_time_millis+"");
  150. // 每行从第一列开始遍历列
  151. /**
  152. * 表格中字段的顺序必须是title、bug_category、severity、recurrent、description
  153. */
  154. for (int j = 0; j < colunms; j++) {
  155. Cell cell = row.getCell(j);
  156. String topName = fieldList.get(j);
  157. // if(topName.equals("title") || topName.equals("题目")){
  158. if(topName.equals(appTitle)){
  159. String title;
  160. if(cell == null || cell.toString().equals("")){
  161. title = "空";
  162. }else{
  163. title = cell.toString();
  164. }
  165. extendBug.setTitle(title);
  166. // }else if(topName.equals("bug_category") || topName.equals("分类")){
  167. }else if(topName.equals(appBugCategor)){
  168. String bug_category;
  169. if(cell == null || cell.toString().equals("")){
  170. bug_category = "空";
  171. }else{
  172. bug_category = cell.toString();
  173. }
  174. extendBug.setBugCategory(bug_category);
  175. // }else if(topName.equals("severity") || topName.equals("严重等级")){
  176. }else if(topName.equals(appSeverity)){
  177. int num;
  178. if(cell == null || cell.toString().equals("")){
  179. num = 0;
  180. }else{
  181. num = new Double(Double.parseDouble(cell.toString())).intValue();
  182. }
  183. short severity = (short)num;
  184. extendBug.setSeverity(severity);
  185. // }else if(topName.equals("recurrent") || topName.equals("复现程度")){
  186. }else if(topName.equals(appRecurrent)){
  187. int num;
  188. if(cell == null || cell.toString().equals("")){
  189. num = 0;
  190. }else{
  191. num = new Double(Double.parseDouble(cell.toString())).intValue();
  192. }
  193. short recurrent = (short)num;
  194. extendBug.setRecurrent(recurrent);
  195. }else if(topName.equals(appDescription)){
  196. String description;
  197. if(cell == null || cell.toString().equals("")){
  198. description = "空";
  199. }else{
  200. description = cell.toString();
  201. }
  202. extendBug.setDescription(description);
  203. }
  204. }
  205. System.out.println(extendBug);
  206. extendBugDao.save(extendBug);
  207. }
  208. logger.info("文件导入成功!");
  209. // 导入成功后将本地文件夹删除
  210. File file = new File(address);
  211. file.delete();
  212. insert_map.put("insert_success", true);
  213. return insert_map;
  214. } catch (FileNotFoundException e) {
  215. logger.info(e.getMessage());
  216. insert_map.put("insert_success", false);
  217. return insert_map;
  218. } catch (IOException e) {
  219. logger.info(e.getMessage());
  220. insert_map.put("file_is_null", true);
  221. insert_map.put("insert_success", false);
  222. return insert_map;
  223. }
  224. }
  225. }