I followed this tutorial, but I must have missed something, because I am unable to upload the excel file to the folder in my project, that is acting as a local server.
https://www.youtube.com/watch?v=fP6RLBWf894
Please let me know what can I do to fix this problem.
I am not getting any errors, but the file wont display in grid, and it is not being uploaded to folder.
I also need to be able to select among all the sheets in the workbook, instead of just opening the first sheet.
Thanks.
What I have tried:
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
{
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];
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) { }
}
}
}
}