2013-05-29 58 views
1

經過對過去三天的研究並編譯了一個陳舊的Excel-to-XML轉換器版本,現在是時候來到論壇尋求幫助,因爲我無法得到我需要的東西。不過,這裏有一些很好的代碼,所以任何需要它的人都可以抓住它。我有兩個轉換器:1)Excel到XML(在VB中)和2)XML到Excel(在下面的C#中)。後者成功創建了一個功能強大的Excel文件,儘管它不會轉換爲精美的XML格式。將過去三天的複雜Excel轉換爲XML

我認爲這個問題有兩方面的問題。第一個問題與XML-to-Excel轉換產生的文件不包含和標籤有關,而且我不確定如何實現這一點。第二個問題是Excel到XML轉換器奇怪地格式化XML元素(請參見下文)。

原始樣本XML文件是:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="practice.xsd"> 
<Person> 
    <FirstName>Oscar</FirstName> 
    <LastName>Banda</LastName> 
</Person> 
<Person> 
    <FirstName>Steven</FirstName> 
    <LastName>Richter</LastName> 
</Person> 
<Person> 
    <FirstName>Luis</FirstName> 
    <LastName>Contreras</LastName> 
</Person> 
<Person> 
    <FirstName>Elias</FirstName> 
    <LastName>Cooper</LastName> 
</Person> 
</Data> 

這轉換成兩列的Excel與列標題的姓和名的文件,但不承認任何地方和標籤。因此,走另一條路讓我(也注意到了錯誤的元素名稱,和奇怪的命名格式):

<?xml version="1.0" standalone="yes"?> 
    <NewDataSet> 
    <Your> 
    <First_x0020_Name>Oscar</First_x0020_Name> 
    <Last_x0020_Name>Banda</Last_x0020_Name> 
    </Your> 
    <Your> 
    <First_x0020_Name>Elias</First_x0020_Name> 
    <Last_x0020_Name>Cooper</Last_x0020_Name> 
    </Your> 
    <Your> 
    <First_x0020_Name>Steven</First_x0020_Name> 
    <Last_x0020_Name>Richter</Last_x0020_Name> 
    </Your> 
    <Your> 
    <First_x0020_Name>Luis</First_x0020_Name> 
    <Last_x0020_Name>Contreras</Last_x0020_Name> 
    </Your> 
</NewDataSet> 

我使用的VB如下:

Public Function ExcelToXMLConverter(excelFile As String) 
    Dim MyConnection As System.Data.OleDb.OleDbConnection 
    Dim ds As System.Data.DataSet 
    Dim MyCommand As System.Data.OleDb.OleDbDataAdapter 
    Dim source As String = excelFile 
    Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & source & ";Extended Properties=Excel 12.0;" 

    'get Table Name 
    MyConnection = New System.Data.OleDb.OleDbConnection(connectionString) 
    MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Practice$]", MyConnection) 
    MyCommand.TableMappings.Add("Table", "Your") 

    'Fill dataset with the data 
    ds = New System.Data.DataSet() 
    MyCommand.Fill(ds) 
    ds.WriteXml(IO.Path.GetDirectoryName(source).ToString() & "\" & IO.Path.GetFileNameWithoutExtension(source) & ".xml") 
    MyConnection.Close() 

    Return (ds) 
End Function 

這可能有一些與MyCommand.TableMappings.Add函數有關,但我不知道如何在不使用已有的字符串的情況下完成該工作。

編輯:現在我意識到tablemappings.add中的「你的」將不得不被改爲「Person」以獲得正確的元素名稱,但這並不能解釋爲什麼「First/Last Name」元素正在以一種奇怪的格式返回。我也不想硬編碼這個字符串。此外,在想知道如何以編程方式更改此變化,我將包含我的XML到Excel轉換器(在C#中)以查看是否有人可以建議將方法和元素包含在生成的Excel文件中,例如當我以另一種方式轉換Excel-to-XML時,我得到原始文檔。

public String XMLtoExcel(string xmlInputFile) 
    { 
     object misValue = System.Reflection.Missing.Value; 
     // create Excel 
     Excel.Application myApp = new Excel.Application(); 
     Excel.Workbook myWbk = myApp.Workbooks.Add(misValue); 
     Excel.Worksheet myWst = (Excel.Worksheet)myWbk.Worksheets.get_Item(1); 

     // load xml file 
     string input = xmlInputFile; 
     DataSet ds = new DataSet(); 
     XmlReader xmlFile; 
     XmlReaderSettings settings = new XmlReaderSettings(); 
     settings.DtdProcessing = DtdProcessing.Parse; 
     xmlFile = XmlReader.Create(input, settings); 
     ds.ReadXml(xmlFile); 

     // write root into excel 
     //XmlDocument doc = new XmlDocument(); 
     //doc.Load(input); 
     //XmlNode root = doc.DocumentElement; 
     //myWst.Cells[1, 1] = root.Name; 

     // write data in to excel 
     int col, row; 
     int i = 1; 
     int counter = ds.Tables[0].Columns.Count; 
     for (col = 0; col <= ds.Tables[0].Columns.Count - 1; col++) 
     { 
      myWst.Cells[1, i].value = ds.Tables[0].Columns[col].ColumnName; 
      myWst.Cells[1, i].EntireRow.Font.Bold = true; 
      i += 1; 
     } 

     i = 2; 

     int k = 1; 
     for (col = 0; col <= ds.Tables[0].Columns.Count - 1; col++) 
     { 
      i = 2; 
      for (row = 0; row <= ds.Tables[0].Rows.Count - 1; row++) 
      { 
       myWst.Cells[i, k].Value = ds.Tables[0].Rows[row].ItemArray[col]; 
       i += 1; 
      } 
      k += 1; 
     } 
} 
+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

0

Excel電子表格的列標題包含名稱中的空格,如「First Name」。 (0020是空間的ASCII碼)。當您從excel中查詢這些列時,應該在沒有空格的情況下將它們別名。試試這個VB代碼:

'get Table Name 
MyConnection = New System.Data.OleDb.OleDbConnection(connectionString) 
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select [First Name] AS FirstName, [Last Name] AS LastName from [Practice$]", MyConnection) 
MyCommand.TableMappings.Add("Table", "Person") 'You already figured this one out 
+0

有沒有辦法讓空間被刪除,「人」以編程方式添加? – user

+0

我的查詢(上面)將列名稱別名以除去空格,表格映射「Person」已經由您找出。你的意思是你不想使用查詢來通過查詢爲列別名嗎? – tgolisch

+0

謝謝,順便說一下。 我的意思是說,我寧願不寫「select [First Name] ...」,因爲我很可能會添加其他列,並希望它們全部不用空格。如果有很多列,按照您提供的方式進行操作最終可能會成爲非常重要的任務。 – user