使用open xml sdk检索单元格值问题

时间:2012-06-08 11:39:20

标签: excel excel-vba vsto openxml-sdk vba

我有一个与名为“Libro1.xlsx”的excel 2007文件相关的以下问题。 在这个文件中,我已经在第一张表格中将单元格A1设置为“hello world”,ID为“rId1”,单元格A2为“500”。

在下面的代码中,您将看到我正在访问此工作表的每一行和单元格。我得到单元格的内容A2 = 500而不是A1单元格的内容(im得到“0”作为值)。

这里是我使用的代码:

' Open the document for editing.
        Dim ficheroexcel As SpreadsheetDocument = SpreadsheetDocument.Open(Server.MapPath("Libro1.xlsx"), True)
        Try
            Dim libroconhojas As WorkbookPart = ficheroexcel.GetPartsOfType(Of WorkbookPart).First()
            Dim hoja1 As WorksheetPart = libroconhojas.GetPartById("rId1")
            Dim hoja2 As WorksheetPart = libroconhojas.GetPartById("rId2")
            Dim hoja3 As WorksheetPart = libroconhojas.GetPartById("rId3")
            'hoja.SingleCellTablePart
            Dim hojadatos1 As SheetData = hoja1.Worksheet.GetFirstChild(Of SheetData)()
            Dim hojadatos2 As SheetData = hoja2.Worksheet.GetFirstChild(Of SheetData)()
            Dim hojadatos3 As SheetData = hoja3.Worksheet.GetFirstChild(Of SheetData)()

            Dim fila As Row
            Dim celda As Cell
            For Each fila In hojadatos1.Elements(Of Row)()
                For Each celda In fila.Elements(Of Cell)()
                    Response.Write("texto:" + celda.InnerText + "</br>")
                Next
            Next
            Dim algo = ""
        Catch ex As Exception

        Finally
            ficheroexcel.Close()
        End Try

您是否有任何关于为什么我没有在A1单元格上设置文本的线索?

1 个答案:

答案 0 :(得分:1)

Excel使用SharedStringTable存储非数字值。检查示例方法

  

string XLGetCellValue(Excel.Cell c,WorkbookPart wbPart)

使用ShareStringTable here