2011-12-19 122 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