0

在我的 MVC3 Razor 项目中,我有一个疑问。情况是这样的: 1)我有一个前端,我有一个文件上传和下一页导航按钮。

@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    |

我需要获取标题值名称、年龄、地址。怎么做。

4

1 回答 1

1

您可以使用 OLEDB。您可以从使用 DataTable 方法定义此代码的问题中收集此信息:

DataTable schemaTable = connection.GetSchema("Columns");
// Each row can then get the column via:
string name = (string)row["COLUMN_NAME"];
于 2013-03-20T14:17:52.723 回答