Java导入excel表格

Java导入Excel表格信息

前端代码:
声明:是使用vue框架的
Html部分:

<input type=“file” id=“excel"ref=“head” accept=”.xls,.xlsx" @change=“checkType()” multiple/>
<input id=“saveZipButton” type=“button” style=“width: 60px;height: 35px;” value=“上传” @click = “uploadExcel”/>

JS部分:
checkType() {
let vm = this;
let allImgExt = ‘.xls|.xlsx|’;
let filePath = this.$refs.head.value;

let extName = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
if (allImgExt.indexOf(extName + ‘|’) === -1) {
alert(“请选择正确的Excel文件”);
this.KaTeX parse error: Expected 'EOF', got '}' at position 37: … return; }̲ let file = t…refs.head.files[0];
vm.readExcel(file);//读取该excel文件,在页面上进行预览
vm.excelContent = [];
vm.excelTitle = [];
},
uploadExcel() {
let formData = new FormData();
let file = this.$refs.head.files[0];
if(file == “” || file == undefined){
alert(“请选择要导入的Excel文件”);
return;
}
console.log("file = ", file)
formData.append(‘file’, file);
//调用函数,把formData当做参数上传给后台的attchment微服务中的表格中
this.ctApi.post({
url: uploadExcel,
data: formData,
success: (data) => {
console.log(“data”, data)
}

后端代码:
public void updateImg(@RequestBody MultipartFile file) {
try{
Workbook workbook = getWorkBook(file);//获取工作簿workbook
Sheet sheetAt = workbook.getSheetAt(0);//根据工作簿获取整张excel表的信息
for(int j=0; j < sheetAt.getRow(i).getLastCellNum(); j++){//循环每一行
Cell cell = sheetAt.getRow(i).getCell(j);//获取每一个单元格的值
String value = getValue(cell);//把单元格的值转成字符串
System.out.print(value+" ");
}
System.out.println();

}catch(Exception e){
e.printStackTrace();
}
}
//获得相对应excel版本的工作簿
public static Workbook getWorkBook(MultipartFile file) {
//获得文件名
String fileName = file.getOriginalFilename();
//创建Workbook工作薄对象,表示整个excel
Workbook workbook = null;
Sheet sheet = null;
try {
//获取excel文件的io流
InputStream is = file.getInputStream();
//根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象
if(fileName.endsWith(“xls”)){
//2003
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(is);
workbook = new HSSFWorkbook(poifsFileSystem);
sheet = workbook.getSheetAt(0);
}else if(fileName.endsWith(“xlsx”)){
//2007 及2007以上
workbook = new XSSFWorkbook(is);
sheet = workbook.getSheetAt(0);
}
} catch (IOException e) {
e.printStackTrace();
}
return workbook;
}

//把excel每个单元格的数值都转换成字符串
public static String getValue(Cell cell){
if(cell.getCellTypeEnum() == org.apache.poi.ss.usermodel.CellType.BOOLEAN){
return String.valueOf(cell.getBooleanCellValue());
}else if(cell.getCellTypeEnum() == org.apache.poi.ss.usermodel.CellType.NUMERIC){
String value = “”;
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date d = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
value = sdf.format(d);
}else{
double temp = cell.getNumericCellValue();
//value = new BigDecimal(temp).toString();
value = String.valueOf(temp);
}
return value;
}else if (cell.getCellTypeEnum() == org.apache.poi.ss.usermodel.CellType.STRING){
return String.valueOf(cell.getStringCellValue());
}else{
return String.valueOf(cell.getStringCellValue());
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值