【发布时间】:2016-12-27 18:39:30
【问题描述】:
我有下面的代码,我用它以编程方式将 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
谢谢, 混乱
【问题讨论】:
-
当您谈到转换为 XML 时,您指的是 SpreadsheetML 还是 XML Spreadheets?
-
我不确定这些格式到底是什么。如果我在 Excel 中使用“另存为”选项,我正在尝试获取与我将获得的内容相匹配的 XML 文件。
-
那么您是如何将 xml 映射(由
ActiveWorkbook.XmlMaps(1)检索到的)首先放入电子表格的?您最初是将一些 XML 导入 excel 还是以编程方式添加地图? -
在 Excel 中创建 XML 映射。
标签: c# excel vb.net excel-interop openxml-sdk