Click here to Skip to main content
15,886,833 members

How do I upload an excel file to ASP.NET

Member 10850253 asked:

Open original thread
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
                    {
                        //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) { }
            }
        }
    }
}
Tags: ASP.NET, Microsoft Excel

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900