Read column values with Excel Data Reader not working (MVC)

Apr 8, 2014 at 8:43 AM
My Excelsheet looks like this:

http://cdn.imghack.se/images/cdc24215d3d5079628a58878520c3028.png


Here is the part of my method that matters:
[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 gives me no output, like "Amortization" doesn't exist.

My model:
public class GetExcel
    {
        public List<HttpPostedFileBase> Files { get; set; }

        public GetExcel()
        {
            Files = new List<HttpPostedFileBase>();
        }
    }
My 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" />
        }
But when I use this Excelsheet:

http://cdn.imghack.se/images/1fd5dab2a891c3adda8cd33114ef07c1.png

It works fine and I get all values from column "Amortization" in output. Both files are .xlsx

Can anyone help me with this?
Developer
Apr 9, 2014 at 9:36 AM
If you download the latest code. There is a test app, which will open any excel sheet you give it. Try both sheets with this and see if they work the same.

If they don't then either you could try stepping through the code to see where the difference is, or perhaps unzip the xlsx's and inspect the contents.
or you can upload copies of the files here in an issue, and I may be able to take a look at them at some point. However, I am extremely busy so I wouldn't be able to look at anything soon.
Apr 27, 2014 at 5:37 PM
Edited Apr 27, 2014 at 5:40 PM
Hi krillezzz
I have just been digging about in your code. I think the issue is that you set data to be a single row
                DataRow data = result.Tables[0].Rows[i];
                System.Diagnostics.Debug.Write(data.Table.Rows[i]["Amortization"]);
Then access a Table within that row and rows within that table. I have tried
                    string val = data[1].ToString();
and that works.
                   string nval = data["Title"].ToString();
Also works