【问题标题】:Importing excel table to ASP.NET C# MVC将 excel 表导入 ASP.NET C# MVC
【发布时间】:2016-12-01 13:38:15
【问题描述】:

我在名为 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解析的函数,然后是一个处理将解析的数据加载到表单中以供显示的函数。其次,使用 Excel 互操作会在故障排除中引起各种头痛,这可能就是 NotSupportedException 的来源。我会切换到 ClosedXMl 或 EPPuls 之类的库来解析 Excel 文件。
  • 我是编程新手,只是想了解我从网站上获得了这段代码。我不确定如何使用 ClosedXMI 或 EPPuls。如果您能抽出一些宝贵的时间来编写或编辑代码的外观,那就太好了

标签: c# asp.net-mvc razor


【解决方案1】:

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

var firstFile=Request.Files[0];

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

【讨论】:

  • 其他类型的excel文件?
  • 他指的是其他类型,.xls .xlsx 等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 2017-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多