020. asp.net访问Excel文件
<asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Width="471px"> <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> </asp:GridView>
protected void BtnOk_Click(object sender, EventArgs e) { try { string strConn; strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("学生成绩.xls") + ";" + "Extended Properties=Excel 8.0;"; OleDbDataAdapter cmd = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn); DataSet ds = new DataSet(); cmd.Fill(ds, "Excelstudent"); this.GridView1.DataSource = ds.Tables["Excelstudent"].DefaultView; this.GridView1.DataBind(); Response.Write("<script language=javascript>alert('恭喜您!Excel文件访问成功!')</script>"); } catch { Response.Write("<script language=javascript>alert('很遗憾!Excel文件访问失败!')</script>"); } }