时间:2021-07-01 10:21:17 帮助过:35人阅读
<INPUT id="File1" type="file" name="File1" runat="server">
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
在代码视图中首先导入OleDb命名空间:
using System.Data.OleDb;
在按钮的单击事件中输入如下代码:
代码如下:
string strPath="c://test//" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
File1.PostedFile.SaveAs(strPath);
string mystring="Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '"+ strPath +"';Extended Properties=Excel 8.0";
OleDbConnection cnnxls = new OleDbConnection (mystring);
OleDbDataAdapter myDa =new OleDbDataAdapter("select * from [Sheet1$]",cnnxls);
DataSet myDs =new DataSet();
myDa.Fill(myDs);
DataGrid1.DataSource=myDs.Tables[0];
DataGrid1.DataBind();
其中C:/test对ASPNET用户要有读写的权限.