Commit b0b6a290 by chenweisong

更新

parent 2fffa518
......@@ -18,80 +18,40 @@ 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) {
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;
XSSFWorkbook book;
XSSFSheet sheet;
JSONArray jsons;
XSSFRow row;
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;
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);
}
sb.append(line);
String[] data = line.toString().split("\\s+");
objects.add(data);
}
} catch (Exception e) {
System.out.println(jsons.toJSONString());
book.close();
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} finally {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static Object convertDatToJson(MultipartFile file, int dataAt) {
......@@ -175,7 +135,7 @@ public class DataFileUtil {
*
* @param file
*/
public static void delteTempFile(File file) {
public static void deleteTempFile(File file) {
if (file != null) {
File del = new File(file.toURI());
del.delete();
......
......@@ -129,6 +129,7 @@ public class TemplateController {
@PostMapping(value = "/excel/getDatData")
public Object getDatData(MultipartFile file, int dataAt) {
CommonValidator.notNull(file, "文件不能为空");
CommonValidator.notLessThan(dataAt, 0, "dataAt不能小于0");
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