using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.OleDb; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.OleDb; using System.Configuration; using System.IO; using System.Data; namespace Interfaz { public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string filePath = ConfigurationManager.AppSettings["FilePath"].ToString(); string filename = string.Empty; if (FileUpload1.HasFile) { try { string[] allowFile = {".xls",".xlsx" }; string FileExt = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName); bool isValidFile = allowFile.Contains(FileExt); if (!isValidFile) { lblMsg.ForeColor = System.Drawing.Color.Red; lblMsg.Text = "Porfavor solo suba archivos de Excel"; } else { //Upload only less or equal to 1 mb /*int FileSize = FileUpload1.PostedFile.ContentLength; if (FileSize<=1048576) { }*/ filename = Path.GetFileName(Server.MapPath(FileUpload1.FileName)); FileUpload1.SaveAs(Server.MapPath(filePath) + filename); OleDbConnection con = null; if (FileExt==".xls") { grid.Visible = true; con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filePath+";Extended Properties=Excel 8.0;"); } if (FileExt == ".xlsx") { grid.Visible = true; con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;"); } con.Open(); DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null); String[] sheetNames = new String[dt.Rows.Count]; /*int i = 0; foreach (DataRow row in dt.Rows) { cb.Items.Add(row["TABLE_NAME"].ToString()); i++; }*/ OleDbCommand ExcelCommand = new OleDbCommand(@"SELECT * FROM ["+sheetNames+@"]",con); OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand); DataSet ExcelDataSet = new DataSet(); con.Close(); grid.DataSource = ExcelDataSet; grid.DataBind(); } } catch (Exception ex) { } } } } }
catch (Exception ex) { throw ex; }
ConfigurationManager.AppSettings["FilePath"]
Server.MapPath
AppSettings
FileUpload1.PostedFile.FileName
FileUpload.FileName
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)