Importieren der Excel -Tabelle in ASP.NET C# MVCC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Importieren der Excel -Tabelle in ASP.NET C# MVC

Post by Anonymous »

Ich habe Tabelle in einem Excel -Blatt namens listProduct.xls. Wenn ich die Excel -Datei importiere, möchte ich, dass sie die Seite der Tabelle in der Erfolgsseite druckte. Die Indexseite funktioniert einwandfrei. Ich kann auf Durchsuchen klicken und kann die Excel -Datei auswählen. Aber wenn ich importiere, gibt mir ein Fehler in meinem Produktcontroller. < /P>

Code: Select all

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

Es gibt mir einen Fehler in dieser Zeile. Der von ihm angezeigte Fehler lautet < /p>


NotSupportedException wurde durch den Benutzercode nicht behandelt. Eine Ausnahme des Typs 'System.NOTSUPPORTEDEXCTECEPS' trat in mscorlib.dll auf, wurde jedoch nicht in Benutzercode < /p>
< /blockquote>

productController.cs

behandeltusing 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
";
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
 listProducts = new List();
for (int row = 3; row 

Index.cshtml

@{
Layout = null;
}






Index


@using (Html.BeginForm("Import", "Product", FormMethod.Post, new { enctype  = "multipart/form-data" }))
{
@Html.Raw(ViewBag.Error)
Excel File 


}


< /code>

Success.cshtml

@{
Layout = null;
}






Success




Name

@foreach (var p in ViewBag.ListProducts)
{

@p.Name

}



Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post