有什么办法让Excel保存根元素中的XML属性?

我一直在尝试使用MS Excel 2007来编辑存储在XML文件中的表格数据。 它根据模式(xsd文件)导入甚至validationXML数据,但在导出时会从根元素中删除xmlns,xlmns:xsi和xsi:schemaLocation属性。 它还将默认名称空间更改为显式名称空间。

以下是比较之前/之后:

之前 (导入到Excel 之前的XML文件)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <database xmlns="experimentManager" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="experimentManager Database.xsd"> <conditionTokens> ... </conditionTokens> <participants> ... </participants> </database> 

(从Excel导出的XML文件)

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns1:database xmlns:ns1="experimentManager"> <ns1:conditionTokens> ... </ns1:conditionTokens> <ns1:participants> ... </ns1:participants> </ns1:database> 

有什么办法可以防止Excel剥离这些属性,搞乱命名空间? 我已经阅读了关于XML映射和导入/导出的MS帮助,但是在GUI中似乎没有任何我想要做的设置。 如果我需要编写一个自定义的macros,这是一种可能性,但是如果有一个更好/更简单的方法,我宁愿不这样做。

第二个问题:是否有更好的工具可以使用类似Excel的UI轻松编辑XML文件的某些部分?

好吧,我点点头,写了一个好的VBAmacros。 我想我会和大家分享,以防其他人遇到同样的问题。

这个macros基本上调用Excel内置的XML Export()方法,然后在生成的文件上执行一系列文本replace。 文本replace完全取决于你。 只要将它们放在工作表中,就像下面链接中的那个一样。

如何设置“replace规则”的示例: 点击我的屏幕截图

在这个例子中,我用空格replace了tab,空格为“:ns1”,空格为“ns1:”,以及具有原始根元素的精简根元素。

您可以按照自己喜欢的方式设置replace规则的格式,只要按照以下说明操作即可:

  1. select所有“find什么”单元格,并给他们名称*“FindWhat”(不要在您的select中包含标题行;空白将被忽略)。
  2. select所有“replace”单元格,并给它们名称*“ReplaceWith”(应该有“find什么”和“replace”单元格之间的一对一映射;使用空格删除不需要的文本)。
  3. input工作簿中某处XML映射的名称,并将该单元命名为“XmlMap”。
  4. 运行macros。 (您将被要求指定您要导出到的文件。)

*如果您不熟悉Excel 2007中的命名范围,请单击“公式”选项卡并select“名称pipe理器”。

好吧,我不会让你再悬念(LOL)…这是macros的代码。 只需将其放置在VBA编辑器的模块中即可。 我没有提供这个免费代码的保证(如果你没有正确命名范围,你可以很容易地破坏它),但是我尝试过的几个例子已经为我工作了。

 Option Explicit Sub ExportXml() Dim exportResult As XlXmlExportResult Dim exportPath As String Dim xmlMap As String Dim fileContents As String exportPath = RequestExportPath() If exportPath = "" Or exportPath = "False" Then Exit Sub xmlMap = range("XmlMap") exportResult = ActiveWorkbook.XmlMaps(xmlMap).Export(exportPath, True) If exportResult = xlXmlExportValidationFailed Then Beep Exit Sub End If fileContents = ReadInTextFile(exportPath) fileContents = ApplyReplaceRules(fileContents) WriteTextToFile exportPath, fileContents End Sub Function ApplyReplaceRules(fileContents As String) As String Dim replaceWorksheet As Worksheet Dim findWhatRange As range Dim replaceWithRange As range Dim findWhat As String Dim replaceWith As String Dim cell As Integer Set findWhatRange = range("FindWhat") Set replaceWithRange = range("ReplaceWith") For cell = 1 To findWhatRange.Cells.Count findWhat = findWhatRange.Cells(cell) If findWhat <> "" Then replaceWith = replaceWithRange.Cells(cell) fileContents = Replace(fileContents, findWhat, replaceWith) End If Next cell ApplyReplaceRules = fileContents End Function Function RequestExportPath() As String Dim messageBoxResult As VbMsgBoxResult Dim exportPath As String Dim message As String message = "The file already exists. Do you want to replace it?" Do While True exportPath = Application.GetSaveAsFilename("", "XML Files (*.xml),*.xml") If exportPath = "False" Then Exit Do If Not FileExists(exportPath) Then Exit Do messageBoxResult = MsgBox(message, vbYesNo, "File Exists") If messageBoxResult = vbYes Then Exit Do Loop RequestExportPath = exportPath End Function Function FileExists(path As String) As Boolean Dim fileSystemObject Set fileSystemObject = CreateObject("Scripting.FileSystemObject") FileExists = fileSystemObject.FileExists(path) End Function Function ReadInTextFile(path As String) As String Dim fileSystemObject Dim textStream Dim fileContents As String Dim line As String Set fileSystemObject = CreateObject("Scripting.FileSystemObject") Set textStream = fileSystemObject.OpenTextFile(path) fileContents = textStream.ReadAll textStream.Close ReadInTextFile = fileContents End Function Sub WriteTextToFile(path As String, fileContents As String) Dim fileSystemObject Dim textStream Set fileSystemObject = CreateObject("Scripting.FileSystemObject") Set textStream = fileSystemObject.CreateTextFile(path, True) textStream.Write fileContents textStream.Close End Sub 

事实上,这比这更容易。

  1. .xlsx后缀更改为.zipxlsx格式实际上是压缩的xml文件!
  2. 在Windows资源pipe理器中打开zip文件
  3. 浏览到xl子目录
  4. xmlMaps.xml文件复制到.zip文件夹之外的位置
  5. 编辑文件以用您的首选名称空间replacensX:条目并保存更改。
  6. 复制文件并覆盖.zip文件夹中的版本
  7. 将该文件夹重命名为.xslx

现在你的XML映射将显示你喜欢的命名空间。