Excel阅读器ASP.NET

我在ASP.NET视图中声明了一个DataGrid,我想生成一些C#代码,用Excel电子表格(.xlsx)填充所述DataGrid。 这是我有的代码:

<asp:DataGrid id="DataGrid1" runat="server"/> <script language="C#" runat="server"> protected void Page_Load(object sender, EventArgs e) { string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\FileName.xlsx;Extended Properties=""Excel 12.0;HDR=YES;"""; // Create the connection object OleDbConnection oledbConn = new OleDbConnection(connString); try { // Open connection oledbConn.Open(); // Create OleDbCommand object and select data from worksheet Sheet1 OleDbCommand cmd = new OleDbCommand("SELECT * FROM [sheetname$]", oledbConn); // Create new OleDbDataAdapter OleDbDataAdapter oleda = new OleDbDataAdapter(); oleda.SelectCommand = cmd; // Create a DataSet which will hold the data extracted from the worksheet. DataSet ds = new DataSet(); // Fill the DataSet from the data extracted from the worksheet. oleda.Fill(ds, "Something"); // Bind the data to the GridView DataGrid1.DataSource = ds.Tables[0].DefaultView; DataGrid1.DataBind(); } catch { } finally { // Close connection oledbConn.Close(); } } </script> 

当我运行这个网站的时候,什么也没有发生。 是什么赋予了?

旧的JET驱动程序还不支持XLSX格式,只支持BIFF(XLS)格式。

你需要像这样的连接string:

  Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\FileName.xlsx;Extended Properties="Excel 12.0;HDR=Yes;IMEX=2" 

您还需要下载并安装在您的Web服务器上:

http://www.microsoft.com/downloads/details.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=en