簡體   English   中英

如何使用vb.net檢索具有合並單元格的復雜Excel文件並另存為xml文件?

[英]How to retrieve complex excel file with merged cells and save as xml file using vb.net?

我有這個可以檢索excel文件並另存為xml文件。

Imports Microsoft.Office.Interop.Excel
Imports System.Xml
Imports System.IO

Module Module1
Sub Main()
    Try
        Dim excel As Application = New Application
        Dim filename As String = "person"
        Dim file_extension As String
        Dim path As String = "C:\Users\"
        Dim w As Workbook
        Try
            file_extension = "xlsx"
            w = excel.Workbooks.Open(path & filename + "." & file_extension)
        Catch ex As Exception
            file_extension = "xls"
            w = excel.Workbooks.Open(path & filename + "." & file_extension)
        End Try

        For i As Integer = 1 To w.Sheets.Count
            Dim sheet As Worksheet = w.Sheets(i)
            Dim r As Range = sheet.UsedRange
            Dim array(,) As Object = r.Value(XlRangeValueDataType.xlRangeValueDefault)

            If array IsNot Nothing Then

                Dim bound0 As Integer = array.GetUpperBound(0)
                Dim bound1 As Integer = array.GetUpperBound(1)

                Dim settings As XmlWriterSettings = New XmlWriterSettings()
                settings.Indent = True

                Using writer As XmlWriter = XmlWriter.Create(filename + ".xml", settings)
                    writer.WriteStartDocument()
                    writer.WriteStartElement(filename)
                    For j As Integer = 2 To bound0
                        writer.WriteStartElement(sheet.Name)
                        For x As Integer = 1 To bound1
                            writer.WriteElementString(array(1, x), array(j, x))
                        Next
                        writer.WriteEndElement()
                    Next
                    writer.WriteEndElement()
                    writer.WriteEndDocument()
                End Using
            End If
        Next
        w.Close()
    Catch ex As Exception
        Console.WriteLine("MS Excel file is invalid.")
        Console.WriteLine(ex.Message)
        Console.ReadKey()
    End Try
End Sub
End Module

例如,當我有這個文件作為我的excel文件時:

文件名: person.xlsx片名: personfile

Name     Age     Gender
John     5       M
Jane     4       F

然后xml文件將以這種方式返回。

<person>
 <personfile>
  <Name>John</Name>
  <Age>5</Age>
  <Gender>M</Gender>
 </personfile>
 <personfile>
  <Name>Jane</Name>
  <Age>4</Age>
  <Gender>F</Gender>
 </personfile>
</person>

將其另存為person.xml

現在我的問題是...如果excel文件合並了單元格怎么辦? 如何解決錯誤? 當excel文件合並了單元格時,它將返回

ERROR: Index and length must refer to a location within the string
Parameter name: length

這是我應該檢索的示例excel文件。 Excel文件

PS也有組合框。

該代碼將表視為沒有合並單元格的二維數組。 最好的方法是將其應用於符合這些條件的表格部分,例如其中沒有合並的單元格。

根據文檔之間結構的固定或變化程度,這可能很容易也可能很困難。

假設所需數據始終位於同一固定位置,則可以將r變量設置為相關范圍,而不是整個工作表。

這適用於我用幾種不同的合並單元格情況制作的測試紙:

Private Sub Main
    Try
        Dim excel As Application = New Application
        Dim filename As String = "person"
        Dim file_extension As String
        Dim path As String = "C:\Users\"
        Dim w As Workbook
        Try
            file_extension = "xlsx"
            w = excel.Workbooks.Open(path & filename + "." & file_extension)
        Catch ex As Exception
            file_extension = "xls"
            w = excel.Workbooks.Open(path & filename + "." & file_extension)
        End Try

        For i As Integer = 1 To w.Sheets.Count
            Dim sheet As Object = w.Sheets(i)
            Dim r As Object = sheet.UsedRange

            'Changes to your original code begin here

            Dim bound0 As Integer = r.Rows.Count
            Dim bound1 As Integer = r.Columns.Count
            Dim array(bound0, bound1) As Object
            For a As Integer = 1 To bound0
                For b As Integer = 1 To bound1
                    Try
                        array(a, b) = r.Cells(a, b).Value
                    Catch
                        array(a, b) = Nothing
                    End Try
                Next
            Next

            If array IsNot Nothing Then 'I left this in, though I can't imagine how it could be needed now

                Dim settings As XmlWriterSettings = New XmlWriterSettings()
                settings.Indent = True

                Using writer As XmlWriter = XmlWriter.Create(filename + ".xml", settings)
                    writer.WriteStartDocument()
                    writer.WriteStartElement(filename)
                    For j As Integer = 2 To bound0
                        writer.WriteStartElement(sheet.Name)
                        For x As Integer = 1 To bound1
                            If array(j, x) IsNot Nothing Then
                                Dim h As Integer = x
                                Do Until array(1, h) IsNot Nothing
                                    h -= 1
                                Loop
                                writer.WriteElementString(array(1, h), array(j, x))

                                'No more changes to your code after this point

                            End If
                        Next
                        writer.WriteEndElement()
                    Next
                    writer.WriteEndElement()
                    writer.WriteEndDocument()
                End Using
            End If
        Next
        w.Close()
    Catch ex As Exception
        Console.WriteLine("MS Excel file is invalid.")
        Console.WriteLine(ex.Message)
        Console.ReadKey()
    End Try
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM