Collect from 企业网站模板

poi导入图片1

第一次登录送5个金币,绑定QQ送5个金币。金币可以用来观看视频和下载资源哦!

 

#10MB表示限制上传大小
spring.http.multipart.maxFileSize = -1
spring.http.multipart.maxRequestSize = -1

需要导入的依赖如下:

	<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi</artifactId>
		    <version>3.13</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-ooxml</artifactId>
		    <version>3.13</version>
		</dependency>
	<dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.9</version>
    </dependency>

实体需要加入的字段,数据库不需要,传参使用

	private byte[] byte_;
	private String format;

跳转导入的excel.jsp

@RequestMapping(value = "/toimportExcel" )
	public String toimportExcel() { 
		return "stu_importExcel";
	}

excel.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html>
<html dir="ltr" lang="en">

<head>
<%@ include file="common.jsp"%>
</head>

<body>
	<!-- ============================================================== -->
	<!-- Preloader - style you can find in spinners.css -->
	<!-- ============================================================== -->
	<div class="preloader">
		<div class="lds-ripple">
			<div class="lds-pos"></div>
			<div class="lds-pos"></div>
		</div>
	</div>
	<!-- ============================================================== -->
	<!-- Main wrapper - style you can find in pages.scss -->
	<!-- ============================================================== -->
	<div id="main-wrapper" data-navbarbg="skin6" data-theme="light"
		data-layout="vertical" data-sidebartype="full"
		data-boxed-layout="full">
		<!-- ============================================================== -->
		<!-- Topbar header - style you can find in pages.scss -->
		<!-- ============================================================== -->
		<%@ include file="leftmenu.jsp"%>
		<!-- ============================================================== -->
		<!-- ============================================================== -->
		<!-- Page wrapper  -->
		<!-- ============================================================== -->
		<div class="page-wrapper">
			<!-- ============================================================== -->
			<!-- Bread crumb and right sidebar toggle -->
			<!-- ============================================================== -->

			<!-- ============================================================== -->
			<!-- End Bread crumb and right sidebar toggle -->
			<!-- ============================================================== -->
			<!-- ============================================================== -->
			<!-- Container fluid  -->
			<!-- ============================================================== -->
			<div class="container-fluid">
				<!-- ============================================================== -->
				<!-- Start Page Content -->
				<!-- ============================================================== -->
				<!-- Row -->
				<div class="row">
					<div class="col-12">
						<div class="card">
							<div class="card-body">
								<h4 class="card-title">导入学生信息
									<small>
										&nbsp;<a id="seemuban" href="javascript:;">[查看Excel模板]</a>
									&nbsp;
									<a href="${ctxf}/assets/images/stu_excel.xls" download="学生信息模板.xls">[下载Excel模板]</a>
									</small>
								</h4>
								<form class="form-horizontal form-material" action="${ctx }/stu/importExcel" method="post"  enctype="multipart/form-data" >


									<div class="form-group">
										<label class="col-md-12">上传Excel:</label>
										<div class="col-md-12">
											<input type="file"    name="file"    required="required"  class="form-control form-control-line" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel" >
										</div>

									</div>
									

									 
									<div class="form-group">
										<div class="col-sm-12">
										    <p style="color:red;">${msg }</p>
											<button class="btn btn-success" type="submit">确定导入</button>
										</div>
									</div>
								</form>
								<c:if test="${not empty failli }"><p style="color:red;">以下数据导入失败( ${fn:length(failli)}  条 ):学号重复</p></c:if>
								<c:forEach items="${failli }" var="t">
									<table class="table table-striped">
									<tr>
										<td scope="row">${t.stu_no }</td>
										<td>${t.realname}</td>
										<td>${t.sex}</td>
										<td>${t.sys_name}</td>
										<td>${t.major_name}</td>
									</tr>

								</c:forEach>
									</table>

								<c:if test="${not empty succli}"><p style="color:green;">导入成功 ${fn:length(succli)} 条数据:<button id="delall" class="btn btn-danger" type="button">全部删除</button></p></c:if>
								<c:forEach items="${succli }" var="t">
									<table class="table table-striped">
									<tr data-id='${t.id}' class="suids">
										<td><img src="${ctx }/${t.photo}" style="width:118px;"></td>
										<td scope="row">${t.stu_no }</td>
										<td>${t.realname}</td>
										<td>${t.sex}</td>
										<td>${t.sys_name}</td>
										<td>${t.major_name}</td>
									</tr>

								</c:forEach>
									</table>
							</div>

						</div>

					</div>

				</div>
				<!-- Row -->
				<!-- ============================================================== -->
				<!-- End PAge Content -->
				<!-- ============================================================== -->
				<!-- ============================================================== -->
				<!-- Right sidebar -->
				<!-- ============================================================== -->
				<!-- .right-sidebar -->
				<!-- ============================================================== -->
				<!-- End Right sidebar -->
				<!-- ============================================================== -->
			</div>
			<!-- ============================================================== -->
			<!-- End Container fluid  -->
			<!-- ============================================================== -->
			<!-- ============================================================== -->
		</div>
		<!-- ============================================================== -->
		<!-- End Page wrapper  -->
		<!-- ============================================================== -->
	</div>


	<!-- 模态框(Modal) -->
	<div class="modal fade" id="mubanModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title" id="myModalLabel">查看excel模板</h4>
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>

				</div>
				<div class="modal-body">
					<img src="${ctxf}/assets/images/stu_excel.jpg" style="width: 100%;" >
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>

				</div>
			</div><!-- /.modal-content -->
		</div>
	</div>
	<!-- /.modal -->
</body>
<script type="text/javascript">
$('#delall').click(function(){
	var arr=$('.suids');
	console.log(arr)
	var ids="";
	for(var i=0;i<arr.length;i++){
		 var id=$(arr[i]).attr('data-id');
		 ids+=id+"_";
	}

	window.location="${ctx}/stu/delall?ids="+ids;
})

	$('#seemuban').click(function(){
		$('#mubanModal').modal('show');
	})

</script>
</html>

前台读取excel,通过工具类存入数据到数据库,主要是读取图片保存到数据库

 @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public String importExcel(
			@RequestParam(value = "file", required = true) MultipartFile file,
			HttpServletRequest request, Model m ) { 

		// 创建处理EXCEL的类
		ReadStuExcel readExcel = new ReadStuExcel();
		// 解析excel,获取上传的事件单
		List<EtStu> sli = readExcel.getExcelInfo(file);

		if (sli == null || sli.size() == 0) {
			m.addAttribute("msg",     " 数据导入失败,请检查你的学号是否存在");
			return "forward:/stu/toimportExcel";

		}
		System.out.println("======sli.size()==============>>>" + sli.size());

		List<EtStu> succli = new ArrayList<EtStu>();
		List<EtStu> failli = new ArrayList<EtStu>();
		// 至此已经将excel中的数据转换到list里面了,接下来就可以操作list,可以进行保存到数据库,或者其他操作,

		for (EtStu stu : sli) {
			System.out.println(stu.toString());


  			  EtStu oldu=etStuService.getByStuNo(stu.getStu_no());

  			  if(oldu==null) {
					EtSystem sys=etSystemService.getBySysName(stu.getSys_name());
					if(sys==null) {
						sys=new EtSystem();
						sys.setSys_name(stu.getSys_name());
						etSystemService.insert(sys);
					}
					stu.setSys_id(sys.getId()+"");
					EtMajor ma=etMajorService.getByMajorName(stu.getMajor_name());
				  if(ma==null) {
					  ma=new EtMajor();
					  ma.setMajor_name(stu.getMajor_name());
					  ma.setSystem_id(sys.getId()+"");
					  etMajorService.insert(ma);
				  }
				  stu.setMajor_id(ma.getId()+"");
				  if (stu.getByte_() != null) {
					  String img = UploadFile.upimgFromExcel(request,
							  stu.getByte_(), stu.getFormat(),
							  Sys.Upimg.photo);
					  stu.setPhoto(img);
				  }

				  etStuService.insert(stu);
				  succli.add(stu);

				}else{
					failli.add(stu);
				}


		}// for


		m.addAttribute("succli", succli);
		m.addAttribute("failli", failli);

		return "forward:/stu/toimportExcel";
	}

全部删除控制器

@RequestMapping(value = "/delall" )
	public String delall(String ids,HttpServletRequest request ) {
		String[] idarr=ids.split("_");
		for(String id:idarr){


			if(id.trim().length()>0){
				EtStu ol=etStuService.getById(id);

				if(ol!=null&&ol.getPhoto()!=null){
					UploadFile.deleteFile(request, ol.getPhoto());
				}

				etStuService.delete(id);
			}

		}
		return "redirect:/stu/toimportExcel";
	}

导入图片2  https://s.jf3q.com/question/detail.action?question.id=244

在此处输入评论

允许提问者通过qq联系我

    现在绑定QQ

点击加载更多...