XML到Excel的映射

XML到Excel的映射,xml,vb.net,excel,mapping,export-to-excel,Xml,Vb.net,Excel,Mapping,Export To Excel,我尝试使用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""?>")

我尝试使用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
专用子ExportToExcel()
将fs设置为新IO.StreamWriter(“exported.xls”,False)
财政司司长书面资料(“”)
财政司司长书面资料(“”)
财政司司长书面资料(“”)
'创建工作表的样式
财政司司长书面资料(“”)
'列标题的样式
财政司司长书面资料(“”)
财政司司长书面资料(“”)
财政司司长书面资料(“”)
财政司司长书面资料(“”)
财政司司长书面资料(“”)
'列信息的样式
财政司司长书面资料(“”)
财政司司长书面资料(“”)
财政司司长书面资料(“”)
财政司司长书面资料(“”)
'编写工作表内容
财政司司长书面资料(“”)
财政司司长书面资料(“”)
对于i,整数=0到1
财政司司长书面资料(“”)
对于j,作为整数=0到2
财政司司长书面资料(“”)
财政司司长致辞全文(“H”)
财政司司长书面资料(“”)
下一个
财政司司长书面资料(“”)
下一个
“关闭文档
财政司司长书面资料(“”)
财政司司长书面资料(“”)
财政司司长书面资料(“”)
财政司司长(关闭)
端接头
这是我生成的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>

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

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

只要更换线路就行了

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

请在问题上加上您正在使用的编程语言的名称,好吗?看起来像VB.NET。另外,为什么不创建一个常规XML文件,然后在Excel中打开它?或者至少使用DOM。
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