我有下面的代码,我用它以编程方式将Office 2010 Excel电子表格保存到XML。此代码生成一个完美的XML文档,其架构与Excel另存为选项相匹配。我的问题是有一种方法可以在不使用互操作程序集的情况下实现这一点。我查看了Open XML文档,但找不到“另存为”选项,允许我将文档另存为xml。这可能吗?任何c#或vb.net的帮助将不胜感激。
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
谢谢, 混沌
答案 0 :(得分:0)
简而言之,互操作是最好的,可能只是你想要实现的解决方案。
一些可行的潜在客户是:
当然,您可以使用OpenXML来获取XML映射的内容等等。如果您想要采用该路径,那么XML地图在OpenXML中的工作方式answer指向CustomXmlMappingsPart
。
然而,我的建议是接受互操作是优秀的,优点是缺点。