日期:2014-05-16  浏览次数:20632 次

java 将Excel(CSV)导入数据库(上传、读取文件)

??? 最近由于项目的需要,必须使用Excel导入数据。

??? 经过一翻查找,确定了思路: 首先从客户端将文件上传到服务器,再有服务器读取,最后通过方法写入数据库。(更早的时候尝试过,在客户端获取文件的绝对路径,传传到 服务器端直接读取写入数据库。事实证明这种做法是不可取的。首先必须得很纠结得去获取路径,最关键是服务器是无法直接去操作客户端的文件。)在这之前的一个Flex项目中曾经写过WORD导出数据,那个时候也是分为两步,首先将文件导出到服务器端得文?件夹,再通过Flex所有的下载组件下载到客户端。

?? 上传时使用插件:commons-fileupload-1.2.jar

?? ?读取Excel文件使用插件:jxl.jar

?? 上传页面:

?? <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
?<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
??? <base href="<%=basePath%>">
?? <script type="text/javascript">
???????? $(function() {
????$('#custom_file_upload').uploadify({
????? 'uploader'?????? : 'map/js/jquery.uploadify-v2.1.4/uploadify.swf',
????? 'script'???????? : 'servlet/PlateTypeServlet',//所调用的servlet
????? 'cancelImg'????? : 'map/js/jquery.uploadify-v2.1.4/cancel.png',
????//? 'folder'???????? : '/uploads',
?????? //'fileDataName'?? : 'uploadify',
????? 'multi'????????? : false,
????? 'auto'?????????? : false,
????? 'fileExt'??????? : '*.jpg;*.csv;*.xls',
????? 'fileDesc'?????? : 'Image Files (.csv,.xls)',
????? 'queueID'??????? : 'custom-queue',
????? 'queueSizeLimit' : 1,
????? 'simUploadLimit' : 1,
????? 'buttonText': 'browse',
?????? 'removeCompleted': true,
?????? 'sizeLimit': 512000,
????? 'onSelectOnce'?? : function(event,data) {
????????? $('#status-message').text(data.filesSelected + ' 份文件等待上传……');
??????? },
??????? //'onComplete' : function(evt, queueID, fileObj, response, data){
?????? // ?$("#backImage").append("<div>"+response+"</div>");
?????? // },
????? 'onAllComplete'? : function(event,data) {
????????? $('#status-message').text(data.filesUploaded + ' 份文件已经上传, ' + data.errors + ' 错误.');
????????? getPlatec(1);
??????? }
????});?
???});
??? </script>
?<style type="text/css">
? #plan_ly_custom-queue {
?? border: 1px solid #E5E5E5;
?? height: 20px;
?margin-bottom: 10px;
?? width: 280px;
?}
</style>
? </head>
<body>
<form id="plateinfo" method="post" enctype="multipart/form-data">
<table width="600" border="0" align="center" cellpadding="0" cellspacing="1" class="f12">
?
? <tr>
??? <td>
????? <table width="95%" border="0" align="center" cellpadding="5" cellspacing="0" class="f12">
??????? <tr>
????????? <td width="90" align="left" valign="bottom">EXCEL 内部数据样式:</td>
????????
??????? </tr>
?????
????? </table>
????? <table id="" width="93%" border="0" id="unit2" align="center" cellpadding="3" cellspacing="1" bgcolor="#B7BABC" class="f12">????
????????
?????? <tr>
????????? <td align="center" bgcolor="#FFE6BF" class="f12"><strong>类型编码</strong></td>?????
????????? <td align="center" bgcolor="#FFE6BF" class="f12"><strong>车牌类别</strong></td>
????????? <td align="center" bgcolor="#FFE6BF" class="f12"><strong>类型备注</strong>
??????? </td>
??????? </tr>
??????????
??? ??<tr>???
??????????? <td align="center" bgcolor="#FFFFFF" class="" style="cursor:pointer"? onclick="" title="">
????????????? <input name="" type="text"? value="" size="22" />
??????????? </td>
??????????? <td align="center" bgcolor="#FFFFFF" class="" title="">
??????????? <input name="" type="text"? value="" size="22" />? </td>
??????????? <td align="center" bgcolor="#FFFFFF">
??????????? <input name="" type="text"? v