vue-element 文件上传
<template>
<div style="padding-left:200px;width:400px" >
<el-upload style="width:100px;"
class="upload-demo"
ref="upload"
:limit="1"
:before-upload="beforeUpload"
:on-success="handleSuccess"
:on-error="handleError"
action="https://test.qunhongyun.com:8500/file/upload" >
<el-button size="primary" type="primary">导入号码</el-button>
</el-upload>
<div style="font-size: 13px;margin-top: 10px;margin-bottom: 10px;" v-if="this.isUploadSuccess===1">
<i class="el-icon-info" style="color: #3bd10a;"></i>
<span style="color: #3bd10a; padding-left:10px">导入成功</span>
</div>
<div style="font-size: 13px;margin-top: 10px;margin-bottom: 10px;" v-if="this.isUploadSuccess===2">
<i class="el-icon-info" style="color: red;"></i>
<span style="color: red; padding-left:10px">导入失败,请重新上传</span>
</div>
</div>
</template>
<script>
export default {
name: 'upload',
data() {
return {
isUploadSuccess:0,
}
},
methods: {
// 上传文件
beforeUpload(file) {
let isLt2M = true
isLt2M = file.size / 1024 / 1024 < 3
if (!isLt2M) {
this.loading = false
this.$message.error('上传文件大小不能超过 3MB!')
this.isUploadSuccess = 2;
return;
}
let fileName = file.name;
let pos = fileName.lastIndexOf(".");
let lastName = fileName.substring(pos, fileName.length);
console.log(lastName,lastName.toLowerCase());
if (
lastName.toLowerCase() !== ".xls"&&
lastName.toLowerCase() !== ".xlsx"
) {
this.$message.error("文件必须为.xls .xlsx类型");
this.$refs.upload.clearFiles()
this.isUploadSuccess = 2;
return false;
}
return isLt2M;
},
handleSuccess(response, file, fileList) {
this.isUploadSuccess = 1;
this.$refs.upload.clearFiles()
this.$message({
message: '上传成功',
type: 'success'
})
console.log(response);
this.formData.fileId = response.content.id;
},
// 监听上传失败
handleError(e, file, fileList) {
const msg = JSON.parse(e.message)
this.isUploadSuccess = 2;
this.$message.error(msg.message);
this.loading = false
},
}
}
</script>
java 从服务器读取excel文件并解析
MAVEN
<!--解析表格用的 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
代码
public static void main(String[] args){
try {
Workbook wb =getWorkbook(path);
Sheet sheet = wb.getSheetAt(0);
int firstRowIndex = sheet.getFirstRowNum() + 1;
int lastRowIndex = sheet.getLastRowNum();
for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {
Row row = sheet.getRow(rIndex);
if (row != null) {
int indexMin=row.getFirstCellNum();
int indexMax=row.getLastCellNum();
for (int i = indexMin; i < indexMax; i++) {
Cell cell=row.getCell(i);
String cellValue = getCellValue(cell);
if(cellValue!=null){
System.out.println(cellValue);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static Workbook getWorkbook(String path) throws Exception{
String type = path.substring(path.lastIndexOf(".") + 1);
Workbook wb;
InputStream input = new URL(path).openStream();
if ("xls".equals(type)) {
wb = new HSSFWorkbook(input);
} else if ("xlsx".equals(type)) {
wb = new XSSFWorkbook(input);
} else {
throw new Exception("文件 类型错误");
}
return wb;
}
public static String getCellValue(Cell cell) {
String cellValue = "";
switch (cell.getCellTypeEnum()) {
case NUMERIC:
if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cellValue = sdf.format(org.apache.poi.ss.usermodel.DateUtil.getJavaDate(cell.getNumericCellValue())).toString();
} else {
DataFormatter dataFormatter = new DataFormatter();
cellValue = dataFormatter.formatCellValue(cell);
}
break;
case STRING:
cellValue = cell.getStringCellValue();
break;
case BOOLEAN:
cellValue = cell.getBooleanCellValue() + "";
break;
case FORMULA:
cellValue = cell.getCellFormula() + "";
break;
case BLANK:
cellValue = "";
break;
case ERROR:
cellValue = "非法字符";
break;
default:
cellValue = "未知类型";
break;
}
return cellValue;
}