Commit b0b6a290 by chenweisong

更新

parent 2fffa518
...@@ -18,80 +18,40 @@ import java.util.regex.Pattern; ...@@ -18,80 +18,40 @@ import java.util.regex.Pattern;
public class DataFileUtil { public class DataFileUtil {
private static final String fileName = "D:\\workspace\\record-demo\\origin\\补录系统excel拆分java代码\\数据和模板文件\\excel拆分导入文件2.txt"; public static void main(String[] args) {
XSSFWorkbook book;
public static void main(String[] args) throws FileNotFoundException { XSSFSheet sheet;
JSONArray jsons;
// XSSFWorkbook book; XSSFRow row;
// 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 { try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8")); //这里可以控制编码 InputStream is = new FileInputStream(new File("D:\\workspace\\record-demo\\origin\\补录系统excel拆分java代码\\数据和模板文件\\excel拆分导入文件.xlsx"));
sb = new StringBuffer(); book = new XSSFWorkbook(is);
String line = null; sheet = book.getSheetAt(0);
int count = 0; jsons = new JSONArray();
while ((line = br.readLine()) != null) { for (int i = 1; i < sheet.getLastRowNum(); i++) {
count++; row = sheet.getRow(i);
if (count < dataAt) { if (row != null) {
continue; 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);
} }
sb.append(line);
String[] data = line.toString().split("\\s+");
objects.add(data);
} }
System.out.println(jsons.toJSONString());
} catch (Exception e) { book.close();
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace(); e.printStackTrace();
} finally {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
} }
} }
public static Object convertDatToJson(MultipartFile file, int dataAt) { public static Object convertDatToJson(MultipartFile file, int dataAt) {
...@@ -175,7 +135,7 @@ public class DataFileUtil { ...@@ -175,7 +135,7 @@ public class DataFileUtil {
* *
* @param file * @param file
*/ */
public static void delteTempFile(File file) { public static void deleteTempFile(File file) {
if (file != null) { if (file != null) {
File del = new File(file.toURI()); File del = new File(file.toURI());
del.delete(); del.delete();
......
...@@ -129,6 +129,7 @@ public class TemplateController { ...@@ -129,6 +129,7 @@ public class TemplateController {
@PostMapping(value = "/excel/getDatData") @PostMapping(value = "/excel/getDatData")
public Object getDatData(MultipartFile file, int dataAt) { public Object getDatData(MultipartFile file, int dataAt) {
CommonValidator.notNull(file, "文件不能为空"); CommonValidator.notNull(file, "文件不能为空");
CommonValidator.notLessThan(dataAt, 0, "dataAt不能小于0");
return DataFileUtil.convertDatToJson(file, dataAt); return DataFileUtil.convertDatToJson(file, dataAt);
} }
......
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