【问题标题】:XML to Excel MappingXML 到 Excel 映射
【发布时间】:2012-01-23 09:40:05
【问题描述】:

我尝试使用 XML 将一些数据导出到 Excel。这是我生成 Excel 文件的代码示例:

    Private Sub ExportToExcel()
    Dim fs As New IO.StreamWriter("exported.xls", False)
    fs.WriteLine("<?xml version=""1.0""?>")
    fs.WriteLine("<?mso-application progid=""Excel.Sheet""?>")
    fs.WriteLine("<Workbook xmlns:ss=""urn:schemas-microsoft-com: Office:spreadsheet"">")

    ' Create the styles for the worksheet
    fs.WriteLine(" <Styles>")

    ' Style for the column headers
    fs.WriteLine(" <Style ss:ID=""1"">")
    fs.WriteLine(" <Font ss:Bold=""1""/>")
    fs.WriteLine(" <Alignment ss:Horizontal=""Center"" ss:Vertical=""Center"" " & _
    "ss:WrapText=""1""/>")
    fs.WriteLine(" <Interior ss:Color=""#C0C0C0"" ss:Pattern=""Solid""/>")
    fs.WriteLine(" </Style>")

    ' Style for the column information
    fs.WriteLine(" <Style ss:ID=""2"">")
    fs.WriteLine(" <Alignment ss:Vertical=""Center"" ss:WrapText=""1""/>")
    fs.WriteLine(" </Style>")
    fs.WriteLine(" </Styles>")

    ' Write the worksheet contents
    fs.WriteLine("<Worksheet ss:Name=""Data Export"">")
    fs.WriteLine(" <Table>")

    For i As Integer = 0 To 1
        fs.WriteLine(" <Row>")
        For j As Integer = 0 To 2
            fs.WriteLine(" <Cell>")
            fs.WriteLine(" <Data ss:Type=""String"">H</Data>")
            fs.WriteLine(" </Cell>")
        Next
        fs.WriteLine(" </Row>")
    Next

        ' Close up the document
        fs.WriteLine(" </Table>")
        fs.WriteLine("</Worksheet>")
        fs.WriteLine("</Workbook>")

        fs.Close()

End Sub

这就是我生成的 xls 文件中的内容:

    <?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns:ss="urn:schemas-microsoft-com: Office:spreadsheet">
 <Styles>
 <Style ss:ID="1">
 <Font ss:Bold="1"/>
 <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
 <Interior ss:Color="#C0C0C0" ss:Pattern="Solid"/>
 </Style>
 <Style ss:ID="2">
 <Alignment ss:Vertical="Center" ss:WrapText="1"/>
 </Style>
 </Styles>
<Worksheet ss:Name="Data Export">
 <Table>
 <Row>
 <Cell>
 <Data ss:Type="String">H</Data>
 </Cell>
 <Cell>
 <Data ss:Type="String">H</Data>
 </Cell>
 <Cell>
 <Data ss:Type="String">H</Data>
 </Cell>
 </Row>
 <Row>
 <Cell>
 <Data ss:Type="String">H</Data>
 </Cell>
 <Cell>
 <Data ss:Type="String">H</Data>
 </Cell>
 <Cell>
 <Data ss:Type="String">H</Data>
 </Cell>
 </Row>
 </Table>
</Worksheet>
</Workbook>

似乎是正确的,但是当我打开 xls 时,我有一个疯狂的输出:

但这不是一切:如果我手动将 xml 结构写入我的 xsl 文件(或者我从另一个文件复制粘贴它)输出是好的 - 我看到我的行和列具有正确的值(H, H,H到处),格式,工作表的名称是“数据导出”,因为我设置它......不明白:(请给我解释一下。非常感谢!!!

【问题讨论】:

  • 请用您正在使用的编程语言的名称标记您的问题吗?看起来像 VB.NET。
  • 另外,为什么不创建一个常规的 XML 文件,然后在 Excel 中打开它呢?或者至少使用 DOM。

标签: xml vb.net excel mapping export-to-excel


【解决方案1】:

只需替换行

fs.WriteLine("<Workbook xmlns:ss=""urn:schemas-microsoft-com: Office:spreadsheet"">")

fs.WriteLine("<Workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet""")
fs.WriteLine("xmlns:o=""urn:schemas-microsoft-com:office:office""")
fs.WriteLine("xmlns:x=""urn:schemas-microsoft-com:office:excel""")
fs.WriteLine("xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"">")

解决问题

1. Output of the sheet
2. Name of the worksheet 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2022-11-29
    相关资源
    最近更新 更多