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

を解決します

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"">") 

でライン

fs.WriteLine("<Workbook xmlns:ss=""urn:schemas-microsoft-com: Office:spreadsheet"">") 

を交換してくださいプログラマティあなたが使っている言語は何ですか? VB.NETのように見えます。 – JimmyPena

+1

また、通常のXMLファイルを作成してExcelで開くのはなぜですか?または少なくともDOMを使用します。 – JimmyPena

答えて

2

はちょうどそれはあなたがの名前で、あなたの質問にタグを付けてくださいだろう問題

1. Output of the sheet 
2. Name of the worksheet 

Output File

+0

kzub - 上記のコードを試しましたか?これは役に立ちましたか? –

+0

はい、私はそれを試して、それは動作します。ありがとう!!! – kzub

関連する問題