我的 Excelsheet 看起来像这样:
http://cdn.imghack.se/images/cdc24215d3d5079628a58878520c3028.png
这是我的方法中重要的部分:
[HttpPost]
public ActionResult ShowExcelFile(GetExcel model)
{
DataSet result = null;
var file = model.Files[0];
if (file != null && file.ContentLength > 0)
{
// .xlsx
IExcelDataReader reader = ExcelReaderFactory.CreateOpenXmlReader(file.InputStream);
// .xls
//IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(file.InputStream);
reader.IsFirstRowAsColumnNames = true; // if your first row contains column names
result = reader.AsDataSet();
reader.Close();
}
for (int i = 1; i < result.Tables[0].Rows.Count; i++)
{
DataRow data = result.Tables[0].Rows[i];
System.Diagnostics.Debug.Write(data.Table.Rows[i]["Amortization"]);
}
return View("ShowExcelFile");
}
System.Diagnostics.Debug.Write 没有给我任何输出,例如“摊销”不存在。
我的模型:
public class GetExcel
{
public List<HttpPostedFileBase> Files { get; set; }
public GetExcel()
{
Files = new List<HttpPostedFileBase>();
}
}
我的 HTML:
@using (Html.BeginForm("ShowExcelFile", "ShowExcel", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.TextBoxFor(m => m.Files, new { type = "file", name = "Files" })<br />
<input type="submit" value="Upload file" />
}
但是当我使用这个 Excelsheet 时:
http://cdn.imghack.se/images/1fd5dab2a891c3adda8cd33114ef07c1.png
它工作正常,我从输出中的“摊销”列中获取所有值。这两个文件都是 .xlsx
谁能帮我这个?
编辑:我发现了问题(我猜)。有效的工作表只有值,另一张只有公式。是否可以读取值而不是公式?