使用poi实现excel导入导出

本文介绍了如何在Java项目中使用Apache POI库处理Excel文件的导入与导出。首先,在Maven中添加了POI依赖。接着,创建了`stuList.jsp`页面。然后,定义了两个工具类:`ExcelBean`用于数据封装,`ExcelUtil`实现了读取和写入Excel的核心功能。此外,还提供了一个辅助工具类。在控制层,导入操作接收文件,导出操作则通过`workBook`响应。在Service层,实现了数据的读取、保存和查询。最后,DAO层和Mapper接口及XML文件完成了数据持久化操作。
摘要由CSDN通过智能技术生成

在maven中添加poi依赖:

<!-- poi依赖 -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.14</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml-schemas</artifactId>
			<version>3.14</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-scratchpad</artifactId>
			<version>3.14</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-examples</artifactId>
			<version>3.14</version>
		</dependency>

编写页面stuList.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery/jquery-1.10.2.js"></script>
<script type="text/javascript">
  $(function(){
	  $("#imp").hide();
	 $("#in").bind("click",function(){
		 $("#excel").click();
	 }); 
	 $("#excel").bind("change",function(){
		 $("#submitFormDate").click();
	 });
	 
	 $("#out").bind("click",function(){
		window.location.href="exportData.action";
	 }); 
  });
  
</script>
</head>
<body>
学生信息<br>
<input type="button" id="in" value="导入"/>  <input type="button" id="out" value="导出"/>
<table width="60%" border="1px">
<thead>
<tr>
<th>Id</th>
<th>姓名</th>
<th>性别</th>
<th>出生年月</th>
<th>电话</th>
<th>毕业院校</th>
</tr>
</thead>
<tbody>
<c:forEach items="${stuList }" var="stu">
<tr>
<td>${stu.id }</td>
<td>${stu.name }</td>
<td>${stu.gender }</td>
<td>${stu.birthday }</td>
<td>${stu.tele }</td>
<td>${stu.collage }</td>
</tr>
</c:forEach>
</tbody>
</table>
<div id="imp">
<form action="importData.action" method="post" enctype="multipart/form-data">
<input  type="file" id="excel" name="excelFile"/>
<input type="submit" id="submitFormDate"/>
</form>
</div>
</body>
</html>

编写两个工具类,一个是ExcelBean:

package com.sl.linguan.util;

import org.apache.poi.xssf.usermodel.XSSFCellStyle;

//创建一个封装表格数据的bean
public class ExcelBean {
	
	private String headTextName; //表头(标题)名
	private String propertyName; //列名
	private Integer cols;//单元格数
	private XSSFCellStyle  cellStyle;
	public ExcelBean() {
		super();
	}
	public ExcelBean(String headTextName, String propertyName) {
		super();
		this.headTextName = headTextName;
		this.propertyName = propertyName;
	}
	public ExcelBean(String headTextName, String propertyName, Integer cols) {
		super();
		this.headTextName = headTextName;
		this.propertyName = propertyName;
		this.cols = cols;
	}
	public String getHeadTextName() {
		return headTextName;
	}
	public void setHeadTextName(String headTextName) {
		this.headTextName = headTextName;
	}
	public String getPropertyName() {
		return propertyName;
	}
	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}
	public Integer getCols() {
		return cols;
	}
	public void setCols(Integer cols) {
		this.cols = cols;
	}
	public XSSFCellStyle getCellStyle() {
		return cellStyle;
	}
	public void setCellStyle(X
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值