本文思路是这样的 EXCEL获取之后保存在服务器上 然后再通过保存的路径读取EXCEL到datatable中 达成导入数据库的目的
这里是 前台页面body里的代码
<body>
<form name="form1" runat="server" >
<TABLE class="tableCss" style=" margin-top:10px;" cellSpacing=1 cellPadding=0 width="85%" align=center border=0 >
<TBODY>
<tr><td style=" height:30px; text-align:left; color:Red;">选择EXCEL文件上传(文件后缀名必须为 .xls )</td></tr>
<TR>
<TD class="row_search" style=" margin-top:10px;" height="35" valign="top">
<div style=" display:none">
<asp:Button ID="readFromDB" runat="server" Text="从数据库读取数据" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" Font-Names="Arial" Font-Size="12px" GridLines="Horizontal" RowStyle-HorizontalAlign="Center" Width="98%">
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" /> <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#F7F7F7" /> </asp:GridView> </div>
<input type="file" name="upload" id="upload" runat="server" />
<asp:Button ID="Import" runat="server" OnClick="Import_Click" Text="导入" />
</TD>
</TR>
</TBODY></TABLE>
<table class="tableCss" cellspacing="1" cellpadding="0" width="85%" align="center" border="0" >
<tr><td class="highRow" colspan="15" style=" text-align:left;">
</td></tr>
<tr>
<td class="highRow">序列号</td>
<td class="highRow">申请时间</td>
<td class="highRow">姓名</td>
<td class="highRow">用户类型</td>
<td class="highRow">地址</td>
<td class="highRow">职业</td>
<td class="highRow">电话</td>
</tr>
<!--数据显示区域-->
<asp:repeater id="Repeater1" runat="server" DataSource="<%# m_seckillList %>">
<ItemTemplate>
<tr style=" text-align:center;">
<td class="row"> <%# Container.ItemIndex+1%></td>
<td class="row"> <%# ((Seckill)Container.DataItem).AddTime%></td>
<td class="row"> <%# ((Seckill)Container.DataItem).Name%></td>
<td class="row"> <%# ((Seckill)Container.DataItem).IsPc == 1 ? "PC端" : ((Seckill)Container.DataItem).IsPc == 2 ? "手机端" : "EXCEL上传"%></td>
<td class="row"> <%# ((Seckill)Container.DataItem).Address%></td>
<td class="row"> <%# ((Seckill)Container.DataItem).Occupation%></td>
<td class="row"> <%# ((Seckill)Container.DataItem).Phone%></td>
</tr>
</ItemTemplate>
</asp:repeater>
<tr><td class="row" colspan="15">
<%# this.GetPagingHtml() %>
</td></tr>
</table>
</form>
<br />
</body>
底下是后台代码 因为用的是拖出来的 botton 代码就不在page_load()里面写了道理是一样的 上传控件使用的 input file
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Dejun.DataProvider;
using Dejun.DataProvider.Table;
using Dejun.DataProvider.Sql2005;
using System.IO;<