2016-07-27 9 views
1

私はListProduct.xlsというExcelシートにテーブルを持っています。 Excelファイルをインポートすると、成功ページのテーブルを印刷します。インデックスページはうまく動作します。ブラウズをクリックすると、Excelファイルを選択できます。しかし、インポートをクリックすると、私のProduct Controllerにエラーが表示されます。Excel C#MVCへのエクセルテーブルのインポート

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

この行にエラーが表示されます。表示されるエラーは、

です。NotSupportedExceptionはユーザーコードで処理されませんでした。タイプ 'System.NotSupportedException' の例外がのMscorlib.dllで発生したが、ユーザーコードで

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を扱っていませんでした

@{ 
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> 
+1

これらのタスクを個々の機能に分離して分離することができます。たぶん、アップロードを処理する関数、Excelの解析を処理する関数、そして解析されたデータをフォームにロードして表示する処理を行う関数です。次に、Excel Interopを使用すると、トラブルシューティングであらゆる種類の頭痛を引き起こします。これはおそらく、NotSupportedExceptionがどこから来ているのでしょうか。私は、Excelファイルを解析するためにClosedXMlやEPPulsのようなライブラリに切り替えるでしょう。 –

+0

私はプログラミングには新しく、ちょうど私がウェブサイトからこのコードを手に入れようとしています。 ClosedXMIやEPPulsの使い方が分かりません。あなたの貴重な時間を費やしてコードを書いたり編集したりして、それが偉大なものになるようにすれば – LohithT

答えて

0

そのaおそらくHttpPostedFileBaseのための問題です。これを試して、input @ type = 'file']を@ Html.BeginFormに保存してファイルを投稿してください。あなたが必要とする、これを使用して、サーバーへのアクセス上のファイル、さらに

var firstFile=Request.Files[0]; 

あなたは(あなたのケースで、それはExcelファイルでなければなりません)できるように、サーバー上のファイルの種類をクライアントにチェックだけでなく、する必要が

+0

それでも、私には同じエラーが出る – LohithT

+0

他のタイプのファイルを選択しようとしましたか? – Alex

+0

他のタイプのExcelファイル? – LohithT

関連する問題