protected void Button1_Click(object sender, EventArgs e) { try { if (FileUpload1.HasFile) { string strFilepath = Path.GetFullPath(FileUpload1.PostedFile.FileName); string filename =(FileUpload1.PostedFile.FileName); if (filename != "") { res = ImportExcel2007(filename); if (res != null) { gvErrors.DataSource = res; gvErrors.DataBind(); } } } } catch (Exception ex) { throw ex; } } private DataSet ImportExcel2007(String strFilePath) { //if (!File.Exists(strFilePath)) return false; String strExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + strFilePath + ";" + "Extended Properties='Excel 8.0;HDR=No'"; OleDbConnection connExcel = new OleDbConnection(strExcelConn); OleDbCommand cmdExcel = new OleDbCommand(); DataSet ds = new DataSet(); try { cmdExcel.Connection = connExcel; //Check if the Sheet Exists connExcel.Open(); DataTable dtExcelSchema; //Get the Schema of the WorkBook dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); connExcel.Close(); //Read Data from Sheet1 connExcel.Open(); OleDbDataAdapter da = new OleDbDataAdapter(); Error Comes here ( No row at postion[0]) --> string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); cmdExcel.CommandText = "SELECT * From [" + SheetName + "]"; //Range Query //cmdExcel.CommandText = "SELECT * From [" + SheetName + "A3:B5]"; da.SelectCommand = cmdExcel; da.Fill(ds); connExcel.Close(); } catch(Exception ex) { throw ex; } finally { cmdExcel.Dispose(); connExcel.Dispose(); } return ds; }
if (FileUpload1.HasFile) { FileName = Path.GetFileName(FileUpload1.PostedFile.FileName); string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName); string FolderPath = ConfigurationManager.AppSettings["DMSFolder"]; FilePath = Server.MapPath(FolderPath + FileName); FileUpload1.SaveAs(FilePath); }
Server.MapPath("~/App_Data/")
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)