Loading color scheme

How to get Excel Cell text in OpenXML SDK 2.5?

Getting cell text in OpenXML SDK 2.5 is quite tricky because the cell text can be either stored in the cell itself, or in a shared string table. In the latter case, the cell value is just an index to the shared string table. Also, some files have data stored in CellValue, other files have data in the InnerText fields. To address all these variations, I wrote the tiny function to get the cell text in any case:


        private string GetCellText(Cell c, string[] saSST)
        {
            string val = "";

            if ((c.DataType != null) && (c.DataType == CellValues.SharedString))
            {
                int ssid;
                if (int.TryParse(c.CellValue.Text, out ssid))
                {
                    if (saSST != null && ssid >= 0 && ssid < saSST.Length)
                    {
                        val = saSST[ssid];
                    }
                }
            }
            else if ((c.DataType != null) && c.DataType == CellValues.InlineString)
            {
                val = c.InnerText;
            }
            else if (c.CellValue != null)
            {
                val = c.CellValue.Text;
            }

            if (val == null)
                val = "";

            return val;

        }

 

 

Get all interesting articles to your inbox
Please wait