How to Import XLS for NET
- 1). Check to make sure the system supports XLS files. If it does not, the XLS file will need to be converted to a CSV file format.
- 2). Convert the XLS file. Open Excel and then open the XLS file in question. Click on "File" then "Save As." Set the "File Format" as "CSV." Save the file and use the CSV file instead.
- 3). Import the XLS file directly into ASP.NET, C# or SQL.
Copy the following code and replace "C:\\Book2.xls" with the path to the XLS file:
"private void btnImportExcelToGrid_Click(object sender,
System.EventArgs e)
{
String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\Book2.xls;" +
"Extended Properties=Excel 8.0;";
DataSet ds = new DataSet();
//You must use the $ after the object
//you reference in the spreadsheet
OleDbDataAdapter da = new OleDbDataAdapter
("SELECT * FROM [Sheet1$]", strConn);
//da.TableMappings.Add("Table", "ExcelTest");
da.Fill(ds);
DataGrid2.DataSource = ds.Tables[0].DefaultView;
DataGrid2.DataBind();
}"
Source...