3

I am building a quick proof of concept project to show the ability to parse an excel file. Based on Microsoft documentation (How to parse and read a large spreadsheet document), it seems the sdk of choice is Open Xml.

The proof of concept gives a basic form for a user to upload a file (.xlsx). The controller reads the file and spits back the content. I am struggling to grab the value of the cell, instead, it seems like I am only able to get some sort of identifier or reference to the text. Here is my code with some examples

View

@using(Html.BeginForm("Index", "Home", FormMethod.Post, new{ enctype="multipart/form-data" } ))
{
    <input type="file" name="file"/>
    <input type="submit"/>
}
<br/>
@Html.Raw(ViewBag.Text)

Action

[HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        ViewBag.Text = "";
        using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(file.InputStream, false))
        {
            WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
            WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
            SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
            string text;
            foreach (Row r in sheetData.Elements<Row>())
            {
                foreach (Cell c in r.Elements<Cell>())
                {
                    text = c.CellValue.Text;
                    ViewBag.Text += text + ", ";
                }
                ViewBag.Text += "<br />";
            }
        }

        return this.View();
    }

Excel File

| Hello | World |
| World | Hello |

Output

0, 1,
1, 0,

As you can see, the 0 represents "Hello" and the 1 represents "World". I've tested this with a larger data set and have confirmed that identical words have the same value when printed to the screen.

The example is pretty much copy/pasted from the MS website. I've tried accessing other c.CellValue properties, such as InnerText and InnerXml only to get the same results. What am I doing wrong? Is Open XML is good SDK to use for this purpose?

Jeff
  • 2,283
  • 9
  • 32
  • 50

1 Answers1

0

Use this method to get the exact value of the cell instead of numbers:

private string ReadExcelCell(Cell cell, WorkbookPart workbookPart)
{
    var cellValue = cell.CellValue;
    var text = (cellValue == null) ? cell.InnerText : cellValue.Text;
    if ((cell.DataType != null) && (cell.DataType == CellValues.SharedString))
    {
        text = workbookPart.SharedStringTablePart.SharedStringTable
            .Elements<SharedStringItem>().ElementAt(
                Convert.ToInt32(cell.CellValue.Text)).InnerText;
    }

    return (text ?? string.Empty).Trim();
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Hưng Trần
  • 51
  • 1
  • 1