将excel表导入ASP.NET C#MVC

本文关键字:NET C#MVC ASP 导入 excel | 更新日期: 2023-09-27 17:59:04

我在一个名为ListProduct.xls的excel表中有一个表。当我导入excel文件时,我希望它在成功页面中打印该表。索引页面运行良好,我可以点击浏览,让我选择excel文件。但当我单击导入时,它会在我的产品控制器上给我一个错误。

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

这一行给了我一个错误。它显示的错误是

用户代码未处理NotSupportedException。mscorlib.dll中出现"System.NotSupportedException"类型的异常,但未在用户代码中处理

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");
         }
    }
   }
  }
}

索引.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>

成功.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>

将excel表导入ASP.NET C#MVC

这是一个问题,可能是因为HttpPostedFileBase。试试这个,通过在@Html.BeginForm中保留输入[type='file']来发布文件。在服务器上使用这个访问文件

var firstFile=Request.Files[0];

此外,您需要在客户端和服务器上检查允许的文件类型(在您的情况下,应该是excel文件)