2013-03-20 18 views
0

在我的MVC3 Razor項目中,我有一個疑問。 情況是這樣的: 1)我有一個前端,我有一個Fileupload和有下一頁導航按鈕。如何獲得MVC3中附加Excel工作表的標題值Razor

@using (Html.BeginForm("ConvertToUniCode", "Import", FormMethod.Post, new {enctype = "multipart/form-data"})) { 

     <table> 
      <tr><td></td><td></td><td></td></tr> 
      <tr> 
       <td> 
        <label>Choose File:<input id="selectfile" type='file' data-val="true" data-val-required="please select a file" name='file' /></label></td> 
       <td> 
        <input type="submit" value="Next" /></td> 
       <td></td> 
      </tr> 

     </table> 
     } 

控制器:

[AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult ConvertToUniCode(HttpPostedFileBase file) 
     { 
     //HttpPostedFile file= new HttpPostedFile(); 
      if(file.ContentLength>0) 
      { 
       string fileName =Path.GetFileName(file.FileName); 
       string fileSavePath = Server.MapPath("~/App_Data/ImportFileHome/"+ fileName); 
       file.SaveAs(fileSavePath); 
       FileStream fs = new FileStream(fileSavePath, FileMode.Open, FileAccess.Read); 


//Here i need to get the header coloumn values. 
      } 
2) In the Second page i have a DropDownListFor control for binding the header values one by one. 
3) Todo that i need to read the excel sheet header coloumn values. 

的Excel工作表是這樣。

------------------------------- 
    Name | Age | Address | ==> Coloum Header 
------------------------------- 
jasper | 26  |India | 

我需要獲取標題值名稱,年齡,地址。 如何做到這一點。

回答

1

您可以使用OLEDB。您可以從this question中收集該數據,該數據使用DataTable方法定義此代碼:

DataTable schemaTable = connection.GetSchema("Columns"); 
// Each row can then get the column via: 
string name = (string)row["COLUMN_NAME"]; 
相關問題