XML到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""?>") 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文件中所具有的function:

  <?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无处不在),格式化,工作表的名称是“数据导出”,因为我设置…不明白:(请解释我的人,谢谢!

只需更换线路

 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 

输出文件