在我的MVC3 Razor项目中,我有一个疑问。 情况是这样的: 1)我有一个前端,我有一个Fileupload,并有下一页导航按钮。
@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 |
我需要获取标题值Name,Age,Address。 怎么做。
答案 0 :(得分:1)
您可以使用OLEDB。您可以从使用DataTable方法定义此代码的this question收集此内容:
DataTable schemaTable = connection.GetSchema("Columns");
// Each row can then get the column via:
string name = (string)row["COLUMN_NAME"];