java 导入Excel文件

本文介绍了一种使用Java处理Excel文件的方法,通过遍历行和列来读取数据,并对特定列的数据进行有效性检查。此外,还实现了将验证后的数据保存到数据库的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录一下。

jsp页面:


java代码

/**
	 * 获取行和列的数据
	 * @param sheet
	 * @return
	 */
	public String getRowAndCell(Sheet sheet) {
		List<NewExpert> newExperts = new ArrayList<>();
		String msg = null;
        // 获得数据的总行数从0开始 
        int totalRows = sheet.getLastRowNum();
        System.out.println("总行数==" + totalRows);
        // 循环输出表格中的内容,首先循环取出行,再根据行循环取出列  
        for (int i = 1; i <= totalRows; i++) { 
        	Row row = sheet.getRow(i); //取出一行数据放入row
            NewExpert newExpert = new NewExpert();
			int lastCellIndex = row.getLastCellNum();
			for (int j = 0; j < lastCellIndex; j++) { 
				try {
					Cell cell = row.getCell(j);//获得一列的数据
					switch (j) {
					    case 0:
					    	if (null != cell.getStringCellValue()) {
					    		
					    	} else {
					    		return msg = "第" + i + "行第" + j+1 + "列数据有误!";
					    	}
							break;
						case 1:
							if (null != cell.getStringCellValue()) {
								
					    	} else {
					    		return msg = "第" + i + "行第" + j+1 + "列数据有误!";
					    	}
							break;
						case 2:
							if (null != cell.getStringCellValue()) {
								
					    	} else {
					    		return msg = "第" + i + "行第" + j+1 + "列数据有误!";
					    	}
							break;
						case 3:
							if (null != cell.getStringCellValue()) {
								
					    	} else {
					    		return msg = "第" + i + "行第" + j+1 + "列数据有误!";
					    	}
							break;
						case 4:
							if (null != cell.getStringCellValue()) {
								
					    	} else {
					    		return msg = "第" + i + "行第" + j+1 + "列数据有误!";
					    	}
							break;
						case 5:
							if (null != cell.getStringCellValue()) {
								
					    	} else {
					    		return msg = "第" + i + "行第" + j+1 + "列数据有误!";
					    	}
							break;
						case 6:
							if (null != cell.getStringCellValue()) {
								
					    	} else {
					    		return msg = "第" + i + "行第" + j+1 + "列数据有误!";
					    	}
							break;
						case 7:
							if (null != cell.getStringCellValue()) {
								
					    	} else {
					    		return msg = "第" + i + "行第" + j+1 + "列数据有误!";
					    	}
							break;
						case 8:
							if(null != cell.getDateCellValue()){
								
							}
							break;
						case 9:
							if(null != cell.getStringCellValue()){
								
							}
							break;
						case 10:
							if(null != cell.getDateCellValue()){
								
							}
							break;
						case 11:
							if(null != cell.getStringCellValue()){
								
							}
							break;
						case 12:
							if(null != cell.getStringCellValue()){
								
							}
							break;
						case 13:
							if(null != cell.getStringCellValue()){
								
							}
							break;
						case 14:
							if(null != cell.getStringCellValue()){
								
							}
							break;
						//具体有多少列就处理多少列吧
						default:
							break;
					}
				} catch (Exception e) {
					e.printStackTrace();
					return msg = "导入失败,第" + i + "行第" + (j+1) + "列数据有误! ";
				}
            
			} //列循环结束
			
            newExperts.add(newExpert);
            
        } //行end
        
        try {
			for (NewExpert expert : newExperts) {  //这里遍历保存数据
				this.insert(expert); 
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			return msg = e.getMessage();
		}
        msg = "导入成功!";
        return msg;
	}


导出模板



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值