我有下面的代碼,用於以編程方式將Office 2010 Excel電子表格保存爲XML。此代碼生成一個完美的XML文檔,其架構與Excel Save As選項匹配。我的問題是有一種方法可以在不使用互操作程序集的情況下實現這一點。我瀏覽了Open XML文檔,找不到一個「另存爲」選項,它允許我將文檔保存爲xml。這可能嗎?任何幫助在C#或vb.net將不勝感激。將.XLSX或.XLSM轉換爲XML
Private Function ConvertExcelToXml(path As String, file As String) As Boolean
Dim returnValue as Boolean
returnValue = True
Try
Dim importfile As String = (path & file) + ".xlsm"
Dim exportfile As String = (path & file) + ".xml"
Dim app As New Application()
app.Workbooks.Open(importfile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing)
Dim data As String = String.Empty
app.ActiveWorkbook.XmlMaps(1).ExportXml(data) 'export the excel sheet xml to the string variable
System.IO.File.Delete(exportfile) 'delete the file if it already exists
System.IO.File.WriteAllText(exportfile, data) 'write the new file
app.Workbooks.Close()
Catch ex As Exception
returnValue = false
End Try
return returnValue
End Function
感謝, 混沌
當您談到轉換爲XML時,是指SpreadsheetML或XML Spreadheets? – strongbutgood
我不確定這些格式是什麼。我試圖獲得一個XML文件,該文件與我在Excel中使用「另存爲」選項時所得到的匹配。 – Chaos
所以你如何將xml映射(首先由'ActiveWorkbook.XmlMaps(1)'檢索到)映射到電子表格中?您最初是將一些XML導入到Excel中,還是以編程方式添加地圖? – strongbutgood