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>
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
}
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>
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
Ich erstelle eine ASP.NET -Web -API -Anforderung von ASP.NET MVC mit JQuery Code. Die Anfrage reicht zum Endpunkt und speichert die Daten und gibt die JSON -Daten mit einigen Details wieder an die...
Ich erstelle eine ASP.NET -Web -API -Anforderung von ASP.NET MVC mit JQuery Code. Die Anfrage reicht zum Endpunkt und speichert die Daten und gibt die JSON -Daten mit einigen Details wieder an die...
Ich arbeite mit mehreren PDF-Dateien (alle auf Englisch, größtenteils digital). Jedes PDF enthält mehrere Tabellen. Einige haben 5 Tabellen, andere haben 10–20 Tabellen, die auf verschiedene Seiten...
Ich habe ein Problem mit der Modellbindung in meiner ASP.NET Core MVC-Anwendung. Derzeit ist die MVC-Vorlage die Standardvorlage. Wenn ich eine HTTP-POST-Methode schreibe, wird das Feedback eines...
Ich bin neu bei ASP.NET MVC. Ich habe ein einfaches Formular mit einer Schaltfläche zum Senden und einem HTML-Auswahlelement, das mit zwei Elementen gefüllt ist. Wenn das Formular an meinen...