Jump to content

Importing Excel sheets into ASP.NET


Lee-yoshi

Recommended Posts

I'm trying to import an Excel spreadsheet into ASP.NET for use with updating a mySQL database. The only problem is i've scoured the internet and not been able to find out how to read an excel file in ASP.NET successfully.Here's my current code (That doesn't work!):

<%@ Page Language="C#" %><%@ Import Namespace="System.Data.OleDb" %><%@ Import Namespace="System.Data" %><%@ Import Namespace="System" %><script language="C#" runat="server">public void Page_Load(object sender, EventArgs e){	string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath( Sample1.xls ) + ";Extended Properties=Excel 8.0";		// Create the connection object 		OleDbConnection oledbConn = new OleDbConnection(connString);			// Open connection		oledbConn.Open(); 		// Create OleDbCommand object and select data from worksheet Sheet1		OleDbCommand cmd = new OleDbCommand("SELECT * FROM [StockList$]", 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); 		// Bind the data to the GridView		DataGrid1.DataSource = ds;		DataGrid1.DataBind();}</script><html><head></head><body><p><asp:Label id=Label1 runat="server">SpreadSheetContents:</asp:Label></p><asp:DataGrid id=DataGrid1 runat="server"/>\</body></html>

What am i missing here?Any suggestions greatly appreciated.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...