open xml excel read cell value

后端 未结 5 1771
感动是毒
感动是毒 2020-11-27 05:24

I am using the Open XML SDK to open an Excel xlsx file and I try to read the cellvalue on position A1 in each sheet. I use the following code:

using (Spreads         


        
5条回答
  •  一个人的身影
    2020-11-27 06:17

    Amurra's answer seems to go ninety percent of the way, but it may need some nuance.

    1) The function "GetSharedStringItemById" returns a SharedStringItem, not a string, such that the calling code example will not work. To get the actual value as a string, I believe you need to ask for the SharedStringItem's InnerText property, as follows:

    public static string GetSharedStringItemById(WorkbookPart workbookPart, int id)
    {
        return workbookPart.SharedStringTablePart.SharedStringTable.Elements().ElementAt(id).InnerText;
    }
    

    2) The function also (correctly) asks for an int as part of its signature, but the example code call supplies a string, cell.CellValue.Text. It's trivial to convert the string to an int, but needs to be done, as the code as written might to be confusing.

提交回复
热议问题