protected void btnUpload_Click(object sender, EventArgs e) { string FilePath = ConfigurationManager.AppSettings["FilePath"].ToString(); string filename = string.Empty; if (FUpload.HasFile) { try { string[] allowedFile = { ".xls", ".xlsx" }; string fileExtension = System.IO.Path.GetExtension(FUpload.PostedFile.FileName); bool isValidFile = allowedFile.Contains(fileExtension); if (!isValidFile) { ErrorMessage.ForeColor = System.Drawing.Color.Red; ErrorMessage.Text = "Please Upload only excel file"; } else { filename = Path.GetFileName(Server.MapPath(FUpload.FileName)); FUpload.SaveAs(Server.MapPath(FilePath) + filename); string filePath = Server.MapPath(FilePath) + filename; OleDbConnection connection = null; if (fileExtension == ".xls") { connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + "; Extended Properties='Excel 8.0;HDR=YES;'"); } else if (fileExtension == ".xslx") { connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + filePath + ";Extended Properties='Excel 12.0 Xml;HDR=YES;'"); } connection.Open(); DataTable data = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); string getExcelSheetName = data.Rows[0]["Table_Name"].ToString(); OleDbCommand excelCommand = new OleDbCommand(@"SELECT * FROM[ getExcelSheetName$]", connection); OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(excelCommand); DataSet ExcelDataSet = new DataSet(); ExcelAdapter.Fill(ExcelDataSet); connection.Close(); GridView1.DataSource = ExcelDataSet; GridView1.DataBind(); } } catch (Exception ex) { } } else { ErrorMessage.Text = " Please select a file to upload"; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)