Reputation: 232
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:
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!!!
Upvotes: 2
Views: 4807
Reputation: 1546
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
Upvotes: 2