'Importing excel table to ASP.NET C# MVC

I have table in a excel sheet called ListProduct.xls. When I import the excel file I want it to print the table in success page. The index page works fine I can click on browse and lets me pick the excel file. But when I click import it gives me an error on my Product controller.

string path = Server.MapPath("~/Content/" + excelfile.FileName);

It gives me an error in this line. The error it shows is

NotSupportedException was unhandled by user code. An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code

ProductController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using ImportExcelFileInASPNETMVC.Models;

namespace ImportExcelFileInASPNETMVC.Controllers
{
public class ProductController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Import(HttpPostedFileBase excelfile)
    {
        if (excelfile == null || excelfile.ContentLength == 0)
        {
            ViewBag.Error = "Please select a excel file<br>";
            return View("Index");
        }
        else
        {
            if (excelfile.FileName.EndsWith("xls") || excelfile.FileName.EndsWith("xlsx"))
            {
                string path = Server.MapPath("~/Content/" + excelfile.FileName);
                if (System.IO.File.Exists(path))
                    System.IO.File.Delete(path);
                excelfile.SaveAs(path);
                // Read data from excel file
                Excel.Application application = new Excel.Application();
                Excel.Workbook workbook = application.Workbooks.Open(path);
                Excel.Worksheet worksheet = workbook.ActiveSheet;
                Excel.Range range = worksheet.UsedRange;
                List<Product> listProducts = new List<Product>();
                for (int row = 3; row <= range.Rows.Count; row++)
                {
                    Product p = new Product();
                    p.Name = ((Excel.Range)range.Cells[row, 2]).Text;
                    listProducts.Add(p);
                }
                ViewBag.ListProducts = listProducts;
                return View("Success");
            }
            else
         {
            ViewBag.Error = "File type is incorrect<br>";
            return View("Index");
         }

    }
   }
  }
}

Index.cshtml

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Import", "Product", FormMethod.Post, new { enctype  = "multipart/form-data" }))
{
    @Html.Raw(ViewBag.Error)
    <span>Excel File </span><input type="file" name="excelfile" />
    <br />
    <input type="submit" value="Import" />
}
</body>
</html>

Success.cshtml

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Success</title>
</head>
<body>
<table cellpadding="2" cellspacing="2" border="1">
    <tr>
        <th>Name</th>
    </tr>
    @foreach (var p in ViewBag.ListProducts)
    {
        <tr>
            <td>@p.Name</td>
        </tr>
    }
</table>
</body>
</html>


Solution 1:[1]

its a problem probably because of HttpPostedFileBase. Try this, post the file by keeping input[type='file'] in your @Html.BeginForm. And on server access the file using this,

var firstFile=Request.Files[0];

In addition you need to check on client as well as on server the type of file to allow (in your case it should be excel file)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user786