Excel 使用OpenXml确定xlsx中的单元格格式(日期、货币、自定义等)

Excel 使用OpenXml确定xlsx中的单元格格式(日期、货币、自定义等),excel,format,openxml,Excel,Format,Openxml,我正在使用OpenXml解析xlsx文件中的文本 直接的文本和数字按预期进行提取,但任何具有单元格格式的内容,如日期、货币或自定义格式都不会按照Excel中显示的文本进行提取。如果我可以很容易地在代码中识别单元格格式,然后采取相关的操作,这不会是一个问题,但是我在任何地方都找不到它 Example.xlsx文件具有: A1单元-日期格式*14/03/2001 将文本“14/11/2018”输入单元格A1显示为“14/11/2018” 局部变量: c、 CellValue=“43418” c、

我正在使用
OpenXml
解析
xlsx
文件中的文本

直接的文本和数字按预期进行提取,但任何具有单元格格式的内容,如
日期
货币
自定义
格式
都不会按照Excel中显示的文本进行提取。如果我可以很容易地在代码中识别单元格格式,然后采取相关的操作,这不会是一个问题,但是我在任何地方都找不到它

Example.xlsx文件具有:


A1单元-日期格式*14/03/2001

将文本“14/11/2018”输入单元格A1显示为“14/11/2018”

局部变量:

c、 CellValue=“43418”
c、 CellValue.InnerText=“43418”
c、 Prefix=“x”
c、 StyleIndex=“2”
c、 无数据类型
c、 无任何内容

所需值=
'14/11/2018'


单元格A2-货币格式(符号,小数点后2位)

将文本“2000”输入单元格A2显示为“2000.00英镑”

局部变量:

c、 CellValue=“2000”
c、 CellValue.InnerText=“2000”
c、 Prefix=“x”
c、 StyleIndex=“3”
c、 无数据类型
c、 无任何内容

所需值=
“£2000.00”


单元格A3-自定义格式“ABC-”@

将文本“p-100”输入单元格A3显示为“ABC-p-100”

局部变量:

OpenXml Cell.CellValue=“p-100”
OpenXml Cell.CellValue.InnerText=“P-100”
OpenXml Cell.Prefix=“x”
OpenXml Cell.StyleIndex=“1”
OpenXml Cell.DataType=“s”
c、 无任何内容

所需值=
“ABC-p-1000”


这是我的密码:

Public Shared Sub parseXLS(strFileName As String, sbTxtFromFile As StringBuilder)
    Call fncParseXLSXorXLSM(strFileName, sbTxtFromFile)
End Sub

Public Shared Function fncParseXLSXorXLSM(strFileName As String, sbTxtFromFile As StringBuilder) As StringBuilder

        sbTxtFromFile.Length = 0
        Dim intFirst As Integer = 1

        Try
            Using spreadsheetDocument__1 As SpreadsheetDocument = SpreadsheetDocument.Open(strFileName, False)
                Dim workbookPart As WorkbookPart = spreadsheetDocument__1.WorkbookPart
                Dim sharedStringItemsArray As SharedStringItem() = workbookPart.SharedStringTablePart.SharedStringTable.Elements(Of SharedStringItem)().ToArray()
                Dim sheets As DocumentFormat.OpenXml.Spreadsheet.Sheets = spreadsheetDocument__1.WorkbookPart.Workbook.Sheets

                ' For each sheet, display the sheet information.
                For Each sheet As DocumentFormat.OpenXml.OpenXmlElement In sheets
                    For Each attr As DocumentFormat.OpenXml.OpenXmlAttribute In sheet.GetAttributes()
                        Debug.Print("{0}: {1}", attr.LocalName, attr.Value)
                        If attr.LocalName = "name" Then
                            sbTxtFromFile.Append(attr.Value)
                        End If
                    Next

                Next

                For Each worksheetPart As WorksheetPart In workbookPart.WorksheetParts
                    Dim reader As OpenXmlReader = OpenXmlReader.Create(worksheetPart)
                    While reader.Read()
                        If reader.ElementType Is GetType(Cell) Then
                            Do
                                Dim c As Cell = DirectCast(reader.LoadCurrentElement(), Cell)

                                If c.DataType IsNot Nothing AndAlso c.DataType.Value.ToString = "SharedString" Then
                                    Dim ssi As SharedStringItem = sharedStringItemsArray(Integer.Parse(c.CellValue.InnerText))

                                    If Not ssi.Text Is Nothing Then
                                        If Not ssi.Text.Text Is Nothing Then
                                            If intFirst = 1 Then
                                                sbTxtFromFile.Append(ssi.Text.Text)
                                                intFirst = 2
                                            Else
                                                sbTxtFromFile.Append(Environment.NewLine & ssi.Text.Text)
                                            End If
                                        End If
                                    Else
                                        If Not ssi.InnerText Is Nothing Then
                                            If intFirst = 1 Then
                                                sbTxtFromFile.Append(ssi.InnerText)
                                                intFirst = 2
                                            Else
                                                sbTxtFromFile.Append(Environment.NewLine & ssi.InnerText)
                                            End If
                                        End If
                                    End If
                                Else
                                    If Not c.CellValue Is Nothing Then
                                        If intFirst = 1 Then
                                            sbTxtFromFile.Append(c.CellValue.InnerText)
                                            intFirst = 2
                                        Else
                                            sbTxtFromFile.Append(Environment.NewLine & c.CellValue.InnerText)
                                        End If
                                    End If
                                End If
                            Loop While reader.ReadNextSibling()
                        End If
                        If sbTxtFromFile.Length > 0 Then
                            sbTxtFromFile.Append(Environment.NewLine)
                        End If
                    End While
                Next

            End Using

        Catch ex As Exception
            If ex.Message Like "The process cannot access the file '*" Then 'File in use
                sbTxtFromFile.Append("|11readonly11|")
            ElseIf ex.Message Like "Could not find*" Then 'File in use
                sbTxtFromFile.Append("|11notfound11|")
            Else
                sbTxtFromFile.Append("|11cannotread11|")
            End If
        End Try

        Return sbTxtFromFile

    End Function

有什么想法吗?

您可以根据
StyleIndex
属性读取单元格样式,这将帮助您确定应用于单元格的格式

以以下方法为例,根据您的需要进行变通:

Private Shared Sub ReadCellFormat(cell As Cell, stylesheet As Stylesheet)
    If cell.StyleIndex.HasValue Then
        Debug.Print("Style found for the cell:")
        If stylesheet.CellFormats.Count.Value > cell.StyleIndex.Value Then
            Dim format As CellFormat = stylesheet.CellFormats.ElementAt(cell.StyleIndex.Value)
            If format.NumberFormatId.HasValue AndAlso format.NumberFormatId.Value > 0 Then
                Dim numberFormat As NumberingFormat = stylesheet.NumberingFormats.Single(Function(x As NumberingFormat) x.NumberFormatId.Value = format.NumberFormatId.Value)
                Debug.Print($"Format code: {numberFormat.FormatCode}")
            End If
        End If
    End If
End Sub

注意:此方法只读取给定单元格的数字格式。

可能重复感谢您的回复。正如您在上面的示例中所看到的,我已经知道StyleIndex值,但它只是一个无意义的数字。我需要的是实际值和前缀,而不仅仅是数字格式。@goodjujuju你所说的
实际值是什么意思?您是指应用格式/前缀后的计算值吗?理想情况下,我需要应用前缀后的计算值,如excel中所示。如果我无法获取,我想返回一些信息,告诉我单元格格式是什么,例如日期、货币、自定义等@GoodJuJu我认为无法获取计算值。但是,您可以根据
NumberFormatId
属性确定单元格格式。