2011-12-19 4 views
2

XML을 사용하여 일부 데이터를 Excel로 내보내려고합니다. 다음은 Excel 파일 생성 내 코드의 예입니다XML to 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

을하지만 모든되지 않습니다 : 내 XSL 파일을 수동으로 실시 XML 구조를 작성 (또는 내가-복사 붙여 넣기 예를 들어 다른 파일에서) 출력이 확인하는 경우 - 내 행을 볼 수 & 오른쪽 값 (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