2011-12-19 202 views
2

我尝试使用XML将一些数据导出到Excel。这里是生成Excel文件我的代码示例:XML到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我有一个疯狂的输出:enter image description here

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

+0

请你用的名称标记您的问题programmi你正在使用的ng语言?看起来像VB.NET。 – JimmyPena 2011-12-20 19:14:38

+1

另外,你为什么不创建一个普通的XML文件,然后在Excel中打开它?或者至少使用DOM。 – JimmyPena 2011-12-20 19:16:24

回答

2

只需更换线

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

+0

kzub - 你有没有试过上面的代码?这个有帮助吗? – 2012-03-18 10:31:59

+0

是的,我试了一下,它的工作原理。谢谢你!!! – kzub 2012-04-25 22:19:34