利用EXCEL文件批量上传用户信息

这篇博客详细介绍了如何实现批量导入用户信息,包括预备工作中的包引入、建表,提供模板下载,文件上传、下载的实现,以及解析Excel文件并将数据存储到数据库的完整流程。
摘要由CSDN通过智能技术生成

批量导入用户信息

批量导入通常是将指定格式的excel或word文件上传到服务器,服务器读取上传的文件内容,将文件内容全部读取到数据库的过程。
此案例包括:文件下载、文件上传、EXCEL文件解析、将解析后的数据存入数据库。

操作步骤:

一、预备工作

0、引入包

文件上传:commons-fileupload-1.2.2.jar、commons-io-2.1.jar
文件下载:不需要JAR包支持
XLS文件解析:poi-3.7-20101029.jar

1、建表users

create table users(
id int primary key auto_increment,
name varchar(20) not null,
password varchar(32) not null,
sex varchar(2) not null,
birthday date not null,
hobby varchar(50) not null,
telephone varchar(15) not null,
address varchar(50) not null,
type int not null -- 1 admin 2 common user
);
插入测试数据:
insert into users values(1,'admin','202CB962AC59075B964B07152D234B70','男',
'1980-1-1','体育,旅游','13953311099','山东淄博',1);
insert into users values(2,'abc','202CB962AC59075B964B07152D234B70','女',
'1990-1-1','体育,音乐','1895330000','山东济南',2);

效果如下:

2、建立相应的JavaBean:

package cn.sdut.po;
public class User {
   
private int id;
private String name;
private String password;
private String sex;
private String birthday;
private String hobby;
private String telephone;
private String address;
private int type;
public User() {
super();
}
public User(int id, String name, String password, String sex,
String birthday, String hobby, String telephone, String address,
int type) {
super();
this.id = id;
this.name = name;
this.password = password;
this.sex = sex;
this.birthday = birthday;
this.hobby = hobby;
this.telephone = telephone;
this.address = address;
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password
+ ", sex=" + sex + ", birthday=" + birthday + ", hobby="
+ hobby + ", telephone=" + telephone + ", address=" + address
+ ", type=" + type + "]";
}
}

如果要进行批量导入,需要给用户提供一个模板供下载,user.xls(将其放置于WebRoot/excel/user.xls)

要在WebRoot下创建一个目录 excel,存放excel模板user.xls.

文件内容为:

序号 姓名 密码 性别 出生年月 爱好 联系方式 地址    用户类型(1-管理员 2-普通用户)
1 李华 6666 1977-3-3 音乐;美术 196852452 北京朝阳 2

注意:数据类型与数据库中的数据类型相匹配。需要特别注意数据的类型与日期类型的格式,见下图。

<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="ResumeDB" connectionString="Data Source=192.168.1.240;Initial Catalog=ResumeImport;User ID=ResumeImport;Password=ResumeImport2012" providerName="System.Data.SqlClient" /> <!--<add name="ResumeDB" connectionString="Server=DEV2-PC\RICHARD;Database=Resume;Trusted_Connection=True;" />--> </connectionStrings> <appSettings> <!--导入数据源 DB表示是去数据库取数 Directory表示直接从文件夹下导入 Excel表示数据源为Excel--> <add key="ImportSource" value="Directory"/> <!--<add key="UserName" value="baza_TZDZ"/> <add key="PWD" value="12a6eafd1a616e11ce51e419ab1435"/>--> <add key="UserName" value="baza_SZSQHD"/> <add key="PWD" value="52f6eafd9a616e94cd10e419db7818"/> <!--文件大小限制 单位KB--> <add key="LimitSize" value="2000"/> <!--支持上传的文件类型--> <!--<add key="PermitImportFileExtension" value="doc,pdf,txt,html,htm"/>--> <add key="PermitImportFileExtension" value="doc,pdf,txt,html,htm,mht"/> <!--简历的渠道 1:招聘网站,2:猎头招聘,3:员工推荐,4:现场招聘,5:校园招聘,6:媒体广告,7:官网,8:企业人才库,9:其它, 10131:领导推荐,10132:外包,10133:培训机构,10254:职酷 如果不填或填0,则默认使用企业人才库--> <add key="Channel" value="0"/> <!--以下是数据源为Directory的参数 begin--> <add key="FileDirectory" value="F:\中国人才热线下载简历"/> <add key="CurrentlyFilePath" value=""/> <!--以上是数据源为Directory的参数 end--> <!--编码格式 int型 65001为UTF-8 936为默认--> <add key="CodePage" value="936"/> <!--以下是数据源为DB的参数 begin--> <add key="ServiceAddress" value=""/> <add key="TempDirectory" value="d:\ResumeTemp"/> <add key="FileStartID" value="0"/> <add key="FileIDName" value="ReportID"/> <add key="FileDataName" value="WordContent"/> <add key="FileTypeName" value="docname"/> <add key="GetResumeSql" value="select top 50 {0},WordContent,docname from ResumeDocReportTable where {0} > {1} order by {0} "/> <add key="WaitTime" value="10"/> <!--是否保存文件,0表示不保存,1表示保存导入错误的文件,2都保存--> <add key="SaveFile" value="1"/> <add key="FileExtension" value="doc"/> <!--以上是数据源为DB的参数 end--> <!--以下是数据源为Excel的参数 begin--> <add key="ExcelFileDirectory" value="C:\Documents and Settings\kiki\桌面\简历导入工具"/> <add key="ExcelToTxtTemplate" value="C:\Documents and Settings\kiki\桌面\简历导入工具\Template\ExcelToTxtTemplate.txt"/> <add key="ExcelRowKey" value="姓名"/> <add key="ExistKeyCount" value="3"/> <add key="CurrentlyExcelFilePath" value=""/> <!--以上是数据源为Excel的参数 end--> </appSettings> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IResumeImport" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="6553600" maxBufferPoolSize="52428800" maxReceivedMessageSize="6553600" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="3200" maxStringContentLength="819200" maxArrayLength="1638400" maxBytesPerRead="409600" maxNameTableCharCount="1638400" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://115.29.222.109:8008/Baza.Hirede.Services.ResumeImport.ResumeImportService.svc/ResumeImport" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IResumeImport" contract="ResumeImportService.IResumeImport" name="BasicHttpBinding_IResumeImport" /> </client> </system.serviceModel> </configuration> <!--http://weixun.hirede.com/Baza.Hirede.Services.ResumeImport.ResumeImportService.svc/ResumeImport--> <!--http://localhost:8410/ResumeImport--> <!--http://resumeimport.hirede.com/Baza.Hirede.Services.ResumeImport.ResumeImportService.svc-->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值