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 文件中的内容:
<?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无处不在),格式,工作表的名称是“数据导出”,因为我设置它......不明白:(请解释一下。非常感谢!
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!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需替换该行
即可
解决问题
Just Replace the line
With
It will resolve the issues