今天需要将多个Excel文档转换为XML格式,本打算用MS Office自带的另存为XML文件的功能,结果转换成MS Office2003 XML之后的文件就是一坨屎!Office 2007自带的XML文档转换的功能也TMD超级繁琐,根据帮助手册自己建了.xsd文件导入到Excel之后也无法导出XML数据,白白浪费了时间。

后来Google到了这篇文章。文中提供了现成的VBA源代码,稍微修改一下即可拿来使用(中文注释为本人所加):

'Attribute VB_Name = "XL_to_XML"
Sub MakeXML()
' create an XML file from an Excel table
Dim MyRow As Integer, MyCol As Integer, Temp As String, YesNo As Variant, DefFolder As String
Dim XMLFileName As String, XMLRecSetName As String, MyLF As String, RTC1 As Integer
Dim RangeOne As String, RangeTwo As String, Tt As String, FldName(99) As StringMyLF = Chr(10) & Chr(13)    ' a line feed command
DefFolder = "C:\MyXML\"   'change this to the location of saved XML filesYesNo = MsgBox("This procedure requires the following data:" & MyLF _& "1 A filename for the XML file" & MyLF _& "2 A groupname for an XML record" & MyLF _& "3 A cellrange containing fieldnames (col titles)" & MyLF _& "4 A cellrange containing the data table" & MyLF _& "Are you ready to proceed?", vbQuestion + vbYesNo, "MakeXML CiM")If YesNo = vbNo ThenDebug.Print "User aborted with 'No'"Exit Sub
End IfXMLFileName = FillSpaces(InputBox("1. Enter the name of the XML file:", "MakeXML CiM", "GRE_Core_Words"))
If Right(XMLFileName, 4) <> ".xml" ThenXMLFileName = XMLFileName & ".xml"
End IfXMLRecSetName = FillSpaces(InputBox("2. Enter an identifying name of a record:", "MakeXML CiM", "item"))RangeOne = InputBox("3. Enter the range of cells containing the field names (or column titles):", "MakeXML CiM", "A2:D2")
If MyRng(RangeOne, 1) <> MyRng(RangeOne, 2) ThenMsgBox "Error: names must be on a single row" & MyLF & "Procedure STOPPED", vbOKOnly + vbCritical, "MakeXML CiM"Exit Sub
End If
MyRow = MyRng(RangeOne, 1)
For MyCol = MyRng(RangeOne, 3) To MyRng(RangeOne, 4)If Len(Cells(MyRow, MyCol).Value) = 0 ThenMsgBox "Error: names range contains blank cell" & MyLF & "Procedure STOPPED", vbOKOnly + vbCritical, "MakeXML CiM"Exit SubEnd IfFldName(MyCol - MyRng(RangeOne, 3)) = FillSpaces(Cells(MyRow, MyCol).Value)
Next MyColRangeTwo = InputBox("4. Enter the range of cells containing the data table:", "MakeXML CiM", "A3:D6257")
If MyRng(RangeOne, 4) - MyRng(RangeOne, 3) <> MyRng(RangeTwo, 4) - MyRng(RangeTwo, 3) ThenMsgBox "Error: number of field names <> data columns" & MyLF & "Procedure STOPPED", vbOKOnly + vbCritical, "MakeXML CiM"Exit Sub
End If
RTC1 = MyRng(RangeTwo, 3)If InStr(1, XMLFileName, ":\") = 0 ThenXMLFileName = DefFolder & XMLFileName
End IfOpen XMLFileName For Output As #1
Print #1, "<?xml version=" & Chr(34) & "1.0" & Chr(34) & " encoding=" & Chr(34) & "UTF-8" & Chr(34) & "?>"
Print #1, "<wordbook>"For MyRow = MyRng(RangeTwo, 1) To MyRng(RangeTwo, 2)
Print #1, "<" & XMLRecSetName & ">"For MyCol = RTC1 To MyRng(RangeTwo, 4)' the next line uses the FormChk function to format dates and numbersPrint #1, "<" & FldName(MyCol - RTC1) & ">" & Cells(MyRow, MyCol).Value & "</" & FldName(MyCol - RTC1) & ">"' the next line does not apply any formatting'  Print #1, "<" & FldName(MyCol - RTC1) & ">" & RemoveAmpersands(Cells(MyRow, MyCol).Value) & "</" & FldName(MyCol - RTC1) & ">"Next MyColPrint #1, "</" & XMLRecSetName & ">"Next MyRow
Print #1, "</wordbook>"
Close #1
MsgBox XMLFileName & " created." & MyLF & "Process finished", vbOKOnly + vbInformation, "MakeXML CiM"
Debug.Print XMLFileName & " saved"
End Sub
Function MyRng(MyRangeAsText As String, MyItem As Integer) As Integer
' analyse a range, where MyItem represents 1=TR, 2=BR, 3=LHC, 4=RHCDim UserRange As Range
Set UserRange = Range(MyRangeAsText)
Select Case MyItemCase 1MyRng = UserRange.RowCase 2MyRng = UserRange.Row + UserRange.Rows.Count - 1Case 3MyRng = UserRange.ColumnCase 4MyRng = UserRange.Columns(UserRange.Columns.Count).Column
End Select
Exit FunctionEnd Function
Function FillSpaces(AnyStr As String) As String
' remove any spaces and replace with underscore character
Dim MyPos As Integer
MyPos = InStr(1, AnyStr, " ")
Do While MyPos > 0Mid(AnyStr, MyPos, 1) = "_"MyPos = InStr(1, AnyStr, " ")
Loop
FillSpaces = LCase(AnyStr)
End FunctionFunction FormChk(RowNum As Integer, ColNum As Integer) As String
' formats numeric and date cell values to comma 000's and DD MMM YY
FormChk = Cells(RowNum, ColNum).Value
If IsNumeric(Cells(RowNum, ColNum).Value) ThenFormChk = Format(Cells(RowNum, ColNum).Value, "#,##0 ;(#,##0)")
End If
If IsDate(Cells(RowNum, ColNum).Value) ThenFormChk = Format(Cells(RowNum, ColNum).Value, "dd mmm yy")
End If
End FunctionFunction RemoveAmpersands(AnyStr As String) As String
Dim MyPos As Integer
' replace Ampersands (&) with plus symbols (+)MyPos = InStr(1, AnyStr, "&")
Do While MyPos > 0Mid(AnyStr, MyPos, 1) = "+"MyPos = InStr(1, AnyStr, "&")
Loop
RemoveAmpersands = AnyStr
End Function

如果不想将文件中的&替换为+,可以将第55行的RemoveAmpersands(FormChk(MyRow, MyCol))替换为Cells(MyRow, MyCol).Value。

需要注意的是在Excel文件中必须有一行是标题行——即改行存放各列的标题,否则生成的XML文件格式不一定是你所需要的。

由于我对XML文件的编码格式有要求,必须是UTF-8,而Windows默认的编码格式是GBXXXX,因此需要对生成的XML文件进行编码格式转换。搜索到了linux 查看文件编码以及修改编码一文,摘录重点如下:

查看文件编码

  在Linux中查看文件编码可以通过以下几种方式:

  1.在Vim中可以直接查看文件编码

  :set fileencoding

  即可显示文件编码格式。

  如果你只是想查看其它编码格式的文件或者想解决用Vim查看文件乱码的问题,那么你可以在~/.vimrc 文件中添加以下内容:

  set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936

  这样,就可以让vim自动识别文件编码(可以自动识别UTF-8或者GBK编码的文件),其实就是依照fileencodings提供的编码列表尝试,如果没有找到合适的编码,就用latin-1(ASCII)编码打开。

  2. enca (如果你的系统中没有安装这个命令,可以用sudo yum install -y enca 安装 )查看文件编码

  $ enca filename

  filename: Universal transformation format 8 bits; UTF-8

  CRLF line terminators

  需要说明一点的是,enca对某些GBK编码的文件识别的不是很好,识别时会出现:

  Unrecognized encoding

文件编码转换

  1.在Vim中直接进行转换文件编码,比如将一个文件转换成utf-8格式

  :set fileencoding=utf-8

  2. enconv 转换文件编码,比如要将一个GBK编码的文件转换成UTF-8编码,操作如下

  enconv -L zh_CN -x UTF-8 filename

  3. iconv 转换,iconv的命令格式如下:

  iconv -f encoding -t encoding inputfile

  比如将一个UTF-8 编码的文件转换成GBK编码

  iconv -f GBK -t UTF-8 file1 -o file2

Excel文件转换为XML以及Linux文件编码格式转换相关推荐

  1. HarmonyOS之将SVG文件转换为XML文件

    SVG(Scalable Vector Graphics)可缩放矢量图形,是一种图像文件格式.目前由于 HarmonyOS 图形渲染引擎不支持 SVG 格式图片的渲染,开发者需要将 SVG 格式的图片 ...

  2. 用C#把文件转换为XML

    using System; using System.Drawing; using System.Collections; using System.ComponentModel; using Sys ...

  3. xBIM 基础06 将STEP物理文件转换为XML

    系列目录    [已更新最新开发文章,点击查看详细]  一.STEP标准简介 STEP,它是Standard for the Exchange of Product model data的缩写. 产品 ...

  4. linux查看文件的有效权限,linux文件权限查看及修改-chmod ------入门的一些常识

    查看Linux文件的权限:ls -l 文件名称 查看linux文件夹的权限:ls -ld 文件夹名称(所在目录) 修改文件及文件夹权限: sudo chmod -(代表类型)×××(所有者)×××(组 ...

  5. linux文件的权限模式,Linux文件权限和访问模式

    为了更加安全的存储文件,Linux为不同的文件赋予了不同的权限,每个文件都拥有下面三种权限: 所有者权限:文件所有者能够进行的操作 组权限:文件所属用户组能够进行的操作 外部权限(其他权限):其他用户 ...

  6. GDCM:将一个DICOM文件转换为另一个DICOM文件测试

    GDCM:将一个DICOM文件转换为另一个DICOM文件 GDCM:将一个DICOM文件转换为另一个DICOM文件 GDCM:将一个DICOM文件转换为另一个DICOM文件 #include < ...

  7. window linux传输工具,window与linux文件传输工具,linux文件传输

    window与linux文件传输工具,linux文件传输 window与linux文件传输工具 [一般用于SecureCRT ssh中使用] 法一:直接用yum安装lrzsz(推荐) yum inst ...

  8. linux文件权限对应数字,linux文件权限更改命令chmod及数字权限

    chmod -change file mode bits :更改文件权限 chmod是用来改变文件或者目录权限的命令,但只有文件的属主和超级用户(root)才有这种权限. 更改文件权限的2种方式: 一 ...

  9. 怎样将CDR文件转换为PS分图层文件

      做平面设计时为了作图需要我们经常要把cdr的文件转换为psd格式的分图层文件,CorelDRAW是常用的矢量图形制作工具,Photoshop是功能强大的图像处理软件,在做一些设计作品的时候两款软件 ...

  10. 将Excel数据转换为XML

    与您可能想到的相反,并不是每个文档最初都是用XML编写的. 实际上,大多数文档是使用其他工具准备的,后来又转换为XML. 许多文档源自关系数据库(例如DB2)或Microsoft Office应用程序 ...

最新文章

  1. 调制的缺点_电光调制与声光调制原理和应用领域
  2. UVA 11255 Necklace
  3. 基于第三方开源库的OPC服务器开发指南(2)——LightOPC的编译及部署
  4. 错误信息:Microsoft 分布式事务处理协调器(MS DTC)已取消此分布式事务
  5. Storm 01_初识初知
  6. 帮助孩子学会感恩_页数204_出版日期2015.03_完整版PDF电子书下载
  7. 目前最细致清晰的NSDictionary以及NSMutableDictionary用法总结(转)
  8. 迭代开发需要一种不同的观点[4]
  9. lol最克制诺手的英雄_LOL“英雄恐惧症”,当你上单遇到诺手时,你会用什么英雄对线...
  10. 1、计算机组成与体系结构
  11. Oracle财务管理系统
  12. Visual SourceSafe基本操作
  13. FMI飞马网【线下】FMI2017人工智能前沿应用与人才发展论坛!
  14. 光纤故障定位器行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  15. 自动弹窗加QQ群代码
  16. 离散数学--Chap14 图
  17. 应聘华为各类工程师通信基础题库以及答案
  18. JavaScript常用库—jQuery
  19. vue 父子组件 组件挂载 组件通信 slot插槽
  20. Beats: Filebeat 和 pipeline processors

热门文章

  1. 带动画的自定义view——做一个移动的箭头
  2. 数字档案馆子系统划分与功能
  3. 八大数据结构-数组 栈 队列 链表 树 散列表 堆 图
  4. 百度地图和Echarts的简单使用
  5. 2020如何一键群发小程序给5000好友或群
  6. 整流、滤波与线性串联型稳压电源工作原理
  7. kdj买卖指标公式源码_优化kdj买卖指标公式
  8. 获得百词斩实体书的单词次序(咸鱼的编程初体验!)
  9. MySQL更新数据语句
  10. DHT爬虫和使用BEP协议完成metadata的下载(BT下载)