經過對過去三天的研究並編譯了一個陳舊的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;
}
}
我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –