这是一个小 vbscript,它使用 ADO (i) 读取 excel 文件 (ii) 写入 CSV 文件:
option explicit
' Reading Excel Files:
' http://support.microsoft.com/kb/257819
dim CONNECTION1
set CONNECTION1 = WScript.CreateObject("ADODB.CONNECTION")
CONNECTION1.Open "Provider=MICROSOFT.JET.OLEDB.4.0;Data Source=C:\Users\Salman\Desktop\input.xls;Extended Properties=""Excel 8.0;HDR=Yes;"""
dim RECORDSET1
set RECORDSET1 = WScript.CreateObject("ADODB.RECORDSET")
RECORDSET1.open "SELECT * FROM [Sheet1$]", CONNECTION1, 3, 1
' Notes:
' CharacterSet 65001 is UTF-8
' add/remove columns and change datatype to match you excel file
dim CONNECTION2
set CONNECTION2 = WScript.CreateObject("ADODB.CONNECTION")
CONNECTION2.Open "Provider=MICROSOFT.JET.OLEDB.4.0;Data Source=C:\Users\Salman\Desktop\;Extended Properties=""text;HDR=Yes;FMT=Delimited;CharacterSet=65001;"""
CONNECTION2.Execute "CREATE TABLE [output.csv] ([English] VARCHAR(200), [Swedish] VARCHAR(200), [Russian] VARCHAR(200), [Chinese Simplified] VARCHAR(200))"
dim RECORDSET2
set RECORDSET2 = WScript.CreateObject("ADODB.RECORDSET")
RECORDSET2.Open "SELECT * FROM [output.csv]", CONNECTION2, 2, 2
do until RECORDSET1.EOF
RECORDSET2.AddNew
dim i
for i = 0 to RECORDSET1.Fields.Count - 1
WScript.Echo RECORDSET1.Fields(i).Value
RECORDSET2.Fields(i).Value = RECORDSET1.Fields(i).Value
next
RECORDSET2.Update
RECORDSET1.MoveNext
loop
示例 Excel 文件内容 (Excel 2003):
English Swedish Russian Chinese Simplified
this should work Detta bör fungera это должно работать 这应该工作
this should work Detta bör fungera это должно работать 这应该工作
this should work Detta bör fungera это должно работать 这应该工作
this should work Detta bör fungera это должно работать 这应该工作
CSV 文件内容示例(不带 BOM 的 UTF-8):
"English","Swedish","Russian","Chinese Simplified"
"this should work","Detta bör fungera","это должно работать","这应该工作"
"this should work","Detta bör fungera","это должно работать","这应该工作"
"this should work","Detta bör fungera","это должно работать","这应该工作"
"this should work","Detta bör fungera","это должно работать","这应该工作"
使用 cscript 从命令行运行此脚本:
C:\>cscript export.vbs
注意:您可能会在 64 位 Windows 上遇到以下 ADO 错误:
ADODB.Connection:找不到提供程序。它可能没有正确安装。(错误代码 0x800A0E7A)
此问题的解决方法是在 32 位模式下运行脚本。您可以通过在“运行”对话框中输入以下命令来启动 32 位命令提示符来执行此操作:
%WINDIR%\SysWOW64\cmd.exe
从此命令提示符处执行 cscript 命令。