Commit 1f53fad8 by chenweisong

更新

parent ae8873ab
......@@ -113,6 +113,18 @@
<!-- <version>8.0.15</version> -->
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15-beta2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
<!-- 引入easypoi -->
<dependency>
<groupId>cn.afterturn</groupId>
......
package com.keymobile.rest.common.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DataFileUtil {
private static final String fileName = "D:\\workspace\\record-demo\\origin\\补录系统excel拆分java代码\\数据和模板文件\\excel拆分导入文件2.txt";
public static void main(String[] args) throws FileNotFoundException {
// XSSFWorkbook book;
// XSSFSheet sheet;
// JSONArray jsons;
// XSSFRow row;
//
// try {
// InputStream is = new FileInputStream(new File("D:\\workspace\\record-demo\\origin\\补录系统excel拆分java代码\\数据和模板文件\\excel拆分导入文件.xlsx"));
//
// book = new XSSFWorkbook(is);
//
// sheet = book.getSheetAt(0);
//
// jsons = new JSONArray();
// for (int i = 1; i < sheet.getLastRowNum(); i++) {
// row = sheet.getRow(i);
// if (row != null) {
// JSONObject json = new JSONObject();
// //对于纯数字内容要做这一操作
// row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
// row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
// row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
// json.put("进程数", row.getCell(0).getStringCellValue());
// json.put("基准板", row.getCell(1).getStringCellValue());
// json.put("优化比", row.getCell(2).getStringCellValue());
// jsons.add(json);
// }
// }
//
// System.out.println(jsons.toJSONString());
// book.close();
// } catch (FileNotFoundException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// } catch (IOException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// }
File file = new File(fileName);
List objects = new ArrayList();
int dataAt = 3;
//读取文件
BufferedReader br = null;
StringBuffer sb = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8")); //这里可以控制编码
sb = new StringBuffer();
String line = null;
int count = 0;
while ((line = br.readLine()) != null) {
count++;
if (count < dataAt) {
continue;
}
sb.append(line);
String[] data = line.toString().split("\\s+");
objects.add(data);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static Object convertDatToJson(MultipartFile file, int dataAt) {
List ArrList = new ArrayList();
//读取文件
BufferedReader br = null;
StringBuffer sb = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(multipartFileToFile(file)), "utf-8")); //这里可以控制编码
sb = new StringBuffer();
String line = null;
int count = 0;
while ((line = br.readLine()) != null) {
count++;
if (count < dataAt) {
continue;
}
sb.append(line);
// 一行的所有数据
String[] lineData = line.toString().split("\\s+");
ArrList.add(lineData);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return JSONObject.toJSON(ArrList);
}
public static int readFileLine(File file) throws FileNotFoundException {
int count = 0;
FileInputStream fis = new FileInputStream(file);
Scanner scanner = new Scanner(fis);
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
return count;
}
public static File multipartFileToFile(MultipartFile file) throws Exception {
File toFile = null;
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
//获取流文件
private static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除本地临时文件
*
* @param file
*/
public static void delteTempFile(File file) {
if (file != null) {
File del = new File(file.toURI());
del.delete();
}
}
}
package com.keymobile.rest.controller;
import com.keymobile.rest.common.utils.DataFileUtil;
import com.keymobile.rest.common.utils.DateUtil;
import com.keymobile.rest.common.validator.CommonValidator;
import com.keymobile.rest.controller.constant.CommonConstant;
......@@ -15,6 +16,7 @@ import org.activiti.engine.*;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.Map;
......@@ -33,7 +35,8 @@ public class TemplateController {
private FeignAuthService feignAuthService;
@Autowired
private ActivityService activityService;
@Autowired
private BreakExcelService breakExcelService;
@Autowired
private SessionService sessionService;
@Autowired
......@@ -119,4 +122,23 @@ public class TemplateController {
return info;
}
@ApiOperation(value = "获取txt或者dat文件的数据")
@ApiImplicitParams({
@ApiImplicitParam(name = "dataAt", required = true, value = "该行起读取数据(表头行数)", dataType = "integer", paramType = "query", defaultValue = "1")
})
@PostMapping(value = "/excel/getDatData")
public Object getDatData(MultipartFile file, int dataAt) {
CommonValidator.notNull(file, "文件不能为空");
return DataFileUtil.convertDatToJson(file, dataAt);
}
@ApiOperation(value = "按列名拆分excel")
@ApiImplicitParams({
@ApiImplicitParam(name = "columnName", required = true, value = "列名称", dataType = "string", paramType = "query")
})
@PostMapping(value = "/excel/breakExcel")
public Object breakExcelByColumn(MultipartFile file, String columnName) {
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment