@ApiOperation(value = "上传文件解析", httpMethod = "POST", consumes = "application/json",
produces = "application/json")
@RequestMapping(value = "/simpledata/parseFile", method = RequestMethod.POST)
@ApiImplicitParams({@ApiImplicitParam(name = "file", value = "上传文件", required = true, dataType = "MultipartFile",
paramType = "query")})
@ApiResponse(code = 200, message = "success", response = Response.class)
@ResponseBody
public Response<JSONObject> parseFile(@RequestParam(value = "file") MultipartFile file) {
Response<JSONObject> response = new Response<>();
try {
String[] split = file.getOriginalFilename().split("\\.");
String format = split[split.length - 1];
if ("csv".equals(format)) {
JSONObject resultCsv = ExcelUtils.readTableCsv(file.getInputStream());
response.setData(resultCsv);
} else if ("xlsx".equals(format)) {
// excel 2007之后版本
JSONObject resultXlsx = ExcelUtils.readTableExcel(file.getInputStream(), 0);
response.setData(resultXlsx);
} else if (("xls").equals(format)) {
// excel 2007之前版本
JSONObject resultXls = ExcelUtils.readTableExcel(file.getInputStream(), 1);
response.setData(resultXls);
}
} catch (Exception e) {
response.setStatus(ExceptionType.UPLOAD_FILE_FAIL_ERROR);
}
return response;
}
/**
* 读取excel文件的表头信息
* @param inputStream
* @return
* @throws IOException
*/
public static List<String> resolverExcel(InputStream inputStream) throws IOException {
List<String> arrList = new ArrayList<>();
Workbook wb = new XSSFWorkbook(inputStream);
Sheet sheet = wb.getSheetAt(0);
int i = 0;
for (Row row : sheet) {
i++;
// 同理行中的单元格也可以用foreach遍历
for (Cell cell : row) {
arrList.add(cell.getStringCellValue());
}
if (i == 1) {
break;
}
}
return arrList;
}
/**
* 读取excel文件的信息,并按照格式返回前端
*
* @param inputStream
* @return
* @throws IOException
*/
public static JSONObject readTableExcel(InputStream inputStream, int flag) throws IOException {
Workbook wb;
if(flag == 0){
wb = new XSSFWorkbook(inputStream);
}else{
wb = new HSSFWorkbook(inputStream);
}
Sheet sheet = wb.getSheetAt(0);
JSONObject tabelJsonObject = new JSONObject();
tabelJsonObject.put("message","");
int i = 0;
JSONArray headArr = new JSONArray();
JSONArray tableBodyArr = new JSONArray();
// 存列名
String[] columnName = new String[301];
// 表所有数据(每一行)
for (Row row : sheet) {
i++;
// 取非空的列
int physicalNumberOfCells = row.getPhysicalNumberOfCells();
// 取所有列
int lastCellNum = row.getLastCellNum();
if(lastCellNum > 300){
tabelJsonObject.put("message","文件导入支持csv、xls、xlsx格式文件,最多不超过300列,50行,文件大小不超过3M!");
return tabelJsonObject;
}
if(i == 1 && physicalNumberOfCells != lastCellNum){
tabelJsonObject.put("message","导入文件存在标题头为空!");
return tabelJsonObject;
}
// 同理行中的单元格也可以用foreach遍历
int m = 0;
JSONObject tableBodyJsonObject = new JSONObject();
// 每行数据的单元格
for (Cell cell : row) {
m++;
int columnIndex;
if(flag == 0){
XSSFCell xSSFCell = (XSSFCell)cell;
columnIndex = xSSFCell.getColumnIndex();
}else{
HSSFCell hSSFCell = (HSSFCell) cell;
columnIndex = hSSFCell.getColumnIndex();
}
cell.setCellType(CellType.STRING);
String stringCellValue = cell.getStringCellValue();
if (i == 1) {
columnName[m - 1] = stringCellValue;
JSONObject headJsonObject = new JSONObject();
headJsonObject.put("title", stringCellValue);
headJsonObject.put("dataIndex", stringCellValue);
headJsonObject.put("key", m);
headJsonObject.put("editable", true);
headArr.add(headJsonObject);
} else {
tableBodyJsonObject.put("key", i);
tableBodyJsonObject.put(columnName[columnIndex], stringCellValue);
}
}
if (i != 1) {
tableBodyArr.add(tableBodyJsonObject);
}
// 如果表格列数大于50行,只取前50行
if(51 == i){
tabelJsonObject.put("message","条数大于50条,截取前50条展示!");
break;
}
}
tabelJsonObject.put("columns", headArr);
tabelJsonObject.put("dataSource", tableBodyArr);
return tabelJsonObject;
}
/**
* 读取csv格式文件的信息,并按照格式返回前端
*
* @param inputStream
* @return
* @throws IOException
*/
public static JSONObject readTableCsv(InputStream inputStream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String cvsSplitBy = ",";
JSONObject tabelJsonObject = new JSONObject();
tabelJsonObject.put("message","");
int i = 0;
JSONArray headArr = new JSONArray();
JSONArray tableBodyArr = new JSONArray();
// 存列名
String[] columnName = new String[301];
// 表所有数据(每一行)
while ((line = br.readLine()) != null) {
// 双引号内的逗号不分割 双引号外的逗号进行分割
String[] country = line.trim().split(",(?=([^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)",-1);
if(country.length > 300){
tabelJsonObject.put("message","文件导入支持csv、xls、xlsx格式文件,最多不超过300列,50行,文件大小不超过3M!");
return tabelJsonObject;
}
i++;
// 同理行中的单元格也可以用foreach遍历
int m = 0;
int n = 0;
JSONObject tableBodyJsonObject = new JSONObject();
// 每行数据的单元格
for (String stringCellValue : country) {
if(stringCellValue.indexOf('\"') >= 0){
String[] split = stringCellValue.split("\"");
int length = split.length;
if(length > 1){
stringCellValue = split[1];
}else if(length == 1){
stringCellValue = split[0];
}
}else if(stringCellValue.indexOf('\'') >= 0){
String[] split = stringCellValue.split("\'");
int length = split.length;
if(length > 1){
stringCellValue = split[1];
}else if(length == 1){
stringCellValue = split[0];
}
}
m++;
n++;
if (i == 1) {
if(("").equals(stringCellValue)){
tabelJsonObject.put("message","导入文件存在标题头为空!");
return tabelJsonObject;
}
columnName[m - 1] = stringCellValue;
JSONObject headJsonObject = new JSONObject();
headJsonObject.put("title", stringCellValue);
headJsonObject.put("dataIndex", stringCellValue);
headJsonObject.put("key", m);
headJsonObject.put("editable", true);
headArr.add(headJsonObject);
} else {
tableBodyJsonObject.put("key", i);
tableBodyJsonObject.put(columnName[n - 1], stringCellValue);
}
}
if (i != 1) {
tableBodyArr.add(tableBodyJsonObject);
}
// 如果表格列数大于50行,只取前50行
if(51 == i){
tabelJsonObject.put("message","条数大于50条,截取前50条展示!");
break;
}
}
tabelJsonObject.put("columns", headArr);
tabelJsonObject.put("dataSource", tableBodyArr);
return tabelJsonObject;
}
excel格式,csv格式文件解析
最新推荐文章于 2024-05-14 10:51:48 发布