最近项目有个需求把原来的数据(原来数据可导出excel)批量导入到新系统中,
我用了两种方法
1 servlet---->ServletFileUpload
需要 commons-fileupload.jar 上传,commons-io.jar,jxl.jar 解析excel,mysql-connector-java-5.1.6-bin.jar,数据库
String uploadPath=this.getServletContext().getRealPath("/"); //获取服务器根目录
Matcher m = p.matcher(name);
boolean result = m.find();
if (result) {
String fileType=m.group(1);
if (m.group(1).endsWith(".xls")==false) {
throw new IOException(name + ": wrong type");
}
}
我只是针对.xls文件类型
2 struts1 ServletFileUpload或者formbean
struts1 中 获取path request.getSession().getServletContext().getRealPath("/") ;或者 servlet.getServletContext().getRealPath("/");
Workbook workbook = Workbook.getWorkbook(new File(path));
Sheet sheet = workbook.getSheet(0);
Client user = new Client();
for(int j=2; j<sheet.getRows()-1; j++)
{
Cell cell = sheet.getCell(13,j);
user.setPassword(cell.getContents());
cell = sheet.getCell(31,j);
user.setClientname(cell.getContents());
cell = sheet.getCell(28,j);
user.setSex(cell.getContents());
}
this.service.insertClient(user);
页面设置
formbean
<html:form action="file.do" enctype="multipart/form-data" >
<table height="52" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="center">
<td width="160">Excel路径:</td>
<td width="198"> <html:file property="file"/> </td>
</tr>
<tr align="right">
<td height="26" colspan="4"> <html:submit>导入到数据库</html:submit> </td>
</tr>
</table>
</html:form>
<form name="filedo" id="filedo" method="post" ENCTYPE="multipart/form-data" action="insert.do">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
选择excel文件:<input name="x" size="40" type="file"/> <input name="upload" type="submit" value="导入到数据库"/>
</td>
</tr>
</table>
</form>
struts的拦截器机制RequestProccessor自己处理了文件上传,
如果是struts1,去掉formbean中的file对应属性,
如果是struts2,把
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
里面的/*改成/*.action然后用普通的servlet代替action做上传处理。
分享到:
相关推荐
java实现Excel数据导入到数据库,如果数据库中存在就更新。 数据库数据导入到Excel表中。
Excel导入,将数据导入到数据库.pdf
java web Excel导入数据库java web Excel导入数据库 java web Excel导入数据库java web Excel导入数据库
javaexcel导入数据库.pdf
javaweb做的Excel导入到数据库,包含:源码,SQL文件,使用说明!
excel导入数据到数据库的步骤过程,希望对大家有用。
Excel导入到Mysql数据库源代码,快速、方便的将Excel表格数据导入到Mysql数据库
java实现Excel数据导入到mysql数据库,如果数据库中存在就更新,数据库数据导入到Excel表中。
VB6操作Excel导入到SQL2000数据库,Excel必须保存成2003的格式
C#将数据导入excel和Excel数据导入数据库使用的是SQL Server数据库
EXCEL表导入EDB数据库.rar EXCEL表导入EDB数据库.rar EXCEL表导入EDB数据库.rar EXCEL表导入EDB数据库.rar EXCEL表导入EDB数据库.rar EXCEL表导入EDB数据库.rar
spring boot+mybatis plus+easy poi实现数据库导出成excel和excel导入到数据库
SpringBootMybatis+poi+Thymeleaf实现excel文件数据导入到数据库以及从数据库将数据导出成excel
Excel导入数据,使用Interop.Excel.dll类库批量导入数据到数据库。服务器:输入服务器IP或者本地电脑全称,数据库:要导入数据表的库名,账号:数据库账号,密码:数据密码,点击页面保存配置可将页面配置保存以方便...
将excel的内容导入到数据库 java excel 数据库导入
一个完整的例子,有源程序和编译好的可执行文件,展示了从EXCEL导入数据到数据库中。可以借鉴学习,适合初学者
在VS2010中打开网站,浏览ReadExcel.aspx页面,这里选择你要导入的excel表,并且还可以为导入数据库中的表命名,点击Upload完成上传。注意:需要修改ReadExcel.aspx.cs页面中CreateTable中的连接字符串。
vb.net将如何将excel中的数据导入到数据库
vb access数据库与excel数据互相导入的源代码,很经典的啊