XML 到 Excel 的映射

发布于 2024-12-21 23:52:19 字数 3100 浏览 2 评论 0原文

我尝试使用 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无处不在),格式,工作表的名称是“数据导出”,因为我设置它......不明白:(请解释一下。非常感谢!

I try to export some data to Excel using XML. Here is an example of my code that generates the Excel file:

    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

And this is what I have in my generated xls file:

    <?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>

Seems to be correct, but when I open the xls I have a crazy output:enter image description here

But it is not everything: if I write manualy the xml structure to my xsl file (or I copy-paste it from another file for example) the output is ok - I see my rows & columns with right values (H,H,H everywhere), formatting, the name of the worksheet is "Data Export" as I set it... don't understand :( Please, explain me someone. Thanks a lot!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糖 2024-12-28 23:52:19

只需替换该行

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 

Output File

Just Replace the line

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

With

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"">")

It will resolve the issues

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

Output File

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文