Ich habe Tabelle in einem Excel-Blatt namens ListProduct.xls. Wenn ich die Excel-Datei importiere, möchte ich, dass die Tabelle auf der Erfolgsseite gedruckt wird. Die Indexseite funktioniert einwandfrei. Ich kann auf Durchsuchen klicken und die Excel-Datei auswählen. Aber wenn ich auf Import klicke, erhalte ich einen Fehler auf meinem Produkt-Controller.Importieren von Excel-Tabelle in ASP.NET C# MVC
string path = Server.MapPath("~/Content/" + excelfile.FileName);
Es gibt mir einen Fehler in dieser Zeile. Der angezeigte Fehler ist
NotSupportedException wurde vom Benutzercode nicht behandelt. Eine Ausnahme vom Typ 'System.NotSupportedException' ist in mscorlib.dll aufgetreten, wurde aber in Benutzercode
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 nicht behandelt
@{
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>
Sie benötigen zurücktreten und diese Aufgaben in einzelne Funktionen aufteilen. Vielleicht eine Funktion, die den Upload übernimmt, eine Funktion, die das Excel-Parsing behandelt, und dann etwas, das das Laden der geparsten Daten in das Formular zur Anzeige übernimmt. Zweitens wird die Verwendung von Excel Interop bei der Fehlersuche allerhand Kopfschmerzen bereiten und das ist wahrscheinlich der Ursprung der NotSupportedException. Ich würde zu einer Bibliothek wie ClosedXMl oder EPPuls wechseln, um die Excel-Datei zu analysieren. –
Ich bin neu in der Programmierung und versuche nur zu lernen, ich habe diesen Code von der Website. Ich bin nicht sicher, wie man ClosedXMI oder EPPuls benutzt. Wenn Sie etwas von Ihrer wertvollen Zeit nehmen und schreiben oder bearbeiten könnten, wie der Code aussehen soll, wäre das großartig – LohithT