|
@@ -1,176 +0,0 @@
|
|
|
-package com.mooctest.service;
|
|
|
-
|
|
|
-import com.fasterxml.jackson.annotation.ObjectIdGenerators;
|
|
|
-import com.mongodb.MongoClient;
|
|
|
-import com.mongodb.MongoCredential;
|
|
|
-import com.mongodb.ServerAddress;
|
|
|
-import com.mongodb.client.MongoCollection;
|
|
|
-import com.mongodb.client.MongoDatabase;
|
|
|
-import com.mooctest.dao2.BugDao;
|
|
|
-import com.mooctest.data.BugDTO;
|
|
|
-import com.mooctest.model.Bug;
|
|
|
-import com.mooctest.model.BugData;
|
|
|
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
-import org.apache.poi.ss.usermodel.Cell;
|
|
|
-import org.apache.poi.ss.usermodel.Row;
|
|
|
-import org.apache.poi.ss.usermodel.Sheet;
|
|
|
-import org.apache.poi.ss.usermodel.Workbook;
|
|
|
-import org.bson.Document;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
-
|
|
|
-import java.io.*;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-@Service
|
|
|
-public class BugExcelInputService {
|
|
|
-
|
|
|
- public String saveMultipartFile(MultipartFile file, String fileSaveName){
|
|
|
- OutputStream os = null;
|
|
|
- InputStream inputStream = null;
|
|
|
- String fileName = fileSaveName;
|
|
|
- String fileFullPath = null;
|
|
|
-
|
|
|
- try {
|
|
|
- inputStream = file.getInputStream();
|
|
|
- fileName = file.getOriginalFilename();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- try {
|
|
|
- String path = System.getProperty("user.dir");
|
|
|
- // 2、保存到临时文件
|
|
|
- // 1K的数据缓冲
|
|
|
- byte[] bs = new byte[1024];
|
|
|
- // 读取到的数据长度
|
|
|
- int len;
|
|
|
- // 输出的文件流保存到本地文件
|
|
|
- File tempFile = new File(path);
|
|
|
- if (!tempFile.exists()) {
|
|
|
- tempFile.mkdirs();
|
|
|
- }
|
|
|
- fileFullPath = tempFile.getPath() + File.separator + fileName;
|
|
|
- os = new FileOutputStream(fileFullPath);
|
|
|
- // 开始读取
|
|
|
- while ((len = inputStream.read(bs)) != -1) {
|
|
|
- os.write(bs, 0, len);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- // 完毕,关闭所有链接
|
|
|
- try {
|
|
|
- os.close();
|
|
|
- inputStream.close();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- return fileFullPath;
|
|
|
- }
|
|
|
-
|
|
|
- public void excelToMongo(Integer PORT, String IP, String DATABASE, String USERNAME, String PASSWORD, String COLLECTION, String ADDRESS) {
|
|
|
- try {
|
|
|
- // 输入文件
|
|
|
- FileInputStream inputStream = new FileInputStream(ADDRESS);
|
|
|
- // 根据输入流导入Excel产生Workbook对象
|
|
|
- Workbook workbook = null;
|
|
|
- try {
|
|
|
- workbook = new HSSFWorkbook(inputStream);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- // IP,端口
|
|
|
- ServerAddress serverAddress = new ServerAddress(IP, PORT);
|
|
|
- List<ServerAddress> address = new ArrayList<ServerAddress>();
|
|
|
- address.add(serverAddress);
|
|
|
- // 用户名,数据库,密码
|
|
|
- MongoCredential credential = MongoCredential.createCredential(USERNAME, DATABASE, PASSWORD.toCharArray());
|
|
|
- List<MongoCredential> credentials = new ArrayList<MongoCredential>();
|
|
|
- credentials.add(credential);
|
|
|
- // 通过验证获取连接
|
|
|
- MongoClient mongoClient = new MongoClient(address, credentials);
|
|
|
- // 连接到数据库
|
|
|
- MongoDatabase mongoDatabase = mongoClient.getDatabase(DATABASE);
|
|
|
-
|
|
|
-
|
|
|
- // 连接文档
|
|
|
- MongoCollection<Document> collection = mongoDatabase.getCollection(COLLECTION);
|
|
|
-// MongoCollection<Document> task = mongoDatabase.getCollection("Task");
|
|
|
-
|
|
|
- System.out.println("连接成功");
|
|
|
-
|
|
|
- List<Document> documents = new ArrayList<Document>();
|
|
|
- List<String> fieldList = new ArrayList<String>();
|
|
|
- // 获取Excel文档中第一个表单
|
|
|
- Sheet sheet = workbook.getSheetAt(0);
|
|
|
- // 获取表单第一行作为表头
|
|
|
- Row topRow = sheet.getRow(0);
|
|
|
- for (Cell cell : topRow) {
|
|
|
- fieldList.add(cell.toString());
|
|
|
- }
|
|
|
-
|
|
|
- System.out.println("文件列表" + fieldList);
|
|
|
- // 获得表单的行数
|
|
|
- int rows = sheet.getLastRowNum() + 1;
|
|
|
- // 获得每行的列数
|
|
|
- int colunms = fieldList.size();
|
|
|
- // 从第二行开始遍历表格
|
|
|
- for (int i = 1; i < rows; i++) {
|
|
|
- Row row = sheet.getRow(i);
|
|
|
- Document document = new Document();
|
|
|
-// Document taskDocument = new Document();
|
|
|
- int exam_id = 0;
|
|
|
- int case_id = 0;
|
|
|
- String exam_case_id;
|
|
|
- long data = System.currentTimeMillis();
|
|
|
- // 每行从第一行开始遍历列
|
|
|
- for (int j = 0; j < colunms; j++) {
|
|
|
- Cell cell = row.getCell(j);
|
|
|
- if (j == 0){
|
|
|
- exam_id = new Double(Double.parseDouble(cell.toString())).intValue();
|
|
|
- exam_id = exam_id + 10000000;
|
|
|
- document.append(fieldList.get(j), exam_id);
|
|
|
-// taskDocument.append(fieldList.get(j), exam_id);
|
|
|
-
|
|
|
- }else if(j == 1){
|
|
|
- case_id = new Double(Double.parseDouble(cell.toString())).intValue();
|
|
|
- case_id = case_id + 10000000;
|
|
|
- document.append(fieldList.get(j), case_id);
|
|
|
-// taskDocument.append(fieldList.get(j), case_id);
|
|
|
-
|
|
|
-
|
|
|
- }else if (j == 4 || j == 5){
|
|
|
- int num = new Double(Double.parseDouble(cell.toString())).intValue();
|
|
|
- document.append(fieldList.get(j), num);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }else{
|
|
|
- document.append(fieldList.get(j), cell.toString());
|
|
|
- }
|
|
|
- }
|
|
|
- exam_case_id = exam_id + "-" + case_id;
|
|
|
- document.append("exam_case_id", exam_case_id);
|
|
|
- document.append("create_time_millis", data);
|
|
|
-
|
|
|
- documents.add(document);
|
|
|
- }
|
|
|
- System.out.println("documents" + documents);
|
|
|
- collection.insertMany(documents);
|
|
|
- System.out.println("插入成功");
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
- System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-}
|