如何使用UTF-8编码将excel文件保存为csv?

时间:2012-05-14 06:33:01

标签: vbscript

我一直在努力解决excel问题。我的excel文件(2003版)中有俄文字符,我想把它保存到csv.But问题是每当我这样做而不是俄文字符我我得到??????经过各种论坛后,我发现问题出在MS excel上。

所以现在我想知道,如果我能编写一个VB脚本来读取所需单元格中的字符串,则将它们转换为UTF-8编码并将其存储在文本文件中。我有2个问题:

  1. 我的解决方案是否可行?我不知道是否可以做到?任何指针都会有所帮助。我已经在网上搜寻过,找不到任何东西。

  2. 由于我对VB脚本完全没有问题,有人可以帮我提供示例代码吗?

  3. 最重要的是,如果有人知道更好的方法,请告诉我。

    由于 斯潘基


    我已经完成了Salman的代码并尝试了我的excel(嵌入其中)。我正面对一些问题,我在网上搜索后对其进行了一些修改。修改后的代码如下:

    Sub test()
    
       'Modified lines
       Dim CONNECTION1 As New ADODB.Connection
       Set CONNECTION1 = New ADODB.Connection
      'Set CONNECTION1 = WScript.CreateObject("ADODB.CONNECTION")
       'Modified line
       CONNECTION1.Open "Provider=MICROSOFT.JET.OLEDB.4.0;Data Source=c:\Documents and Settings\bht1kor \Desktop       \test.xls;Extended Properties=""Excel 8.0;HDR=Yes;"""
    
    
        Dim RECORDSET1 As New ADODB.Connection
       Set RECORDSET1 = New ADODB.Connection
      'commented the line
      'WScript.CreateObject ("ADODB.RECORDSET")
       RECORDSET1.Open CONNECTION1 ', 3, 1
       Set rs1 = RECORDSET1.Execute("SELECT * FROM [Sheet1$]")
    
       ' Notes:
       ' CharacterSet 65001 is UTF-8
       ' add/remove columns and change datatype to match you excel file
    
    
        Dim CONNECTION2 As New ADODB.Connection
        Set CONNECTION2 = New ADODB.Connection
        'commented the line
        'Set CONNECTION2 = WScript.CreateObject("ADODB.CONNECTION")
        CONNECTION2.Open "Provider=MICROSOFT.JET.OLEDB.4.0;Data Source=c:\Documents and Settings\bht1kor\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 As New ADODB.Connection
        Set RECORDSET2 = New ADODB.Connection
        'commented the line
        'Set RECORDSET2 = WScript.CreateObject("ADODB.RECORDSET")
        RECORDSET2.Open CONNECTION2, adOpenKeyset, adLockOptimistic
    
        'Do Until RECORDSET1.EOF
         RECORDSET2.AddNew
         Dim i
         For i = 0 To RECORDSET1.Fields.Count - 1
         'comented the line
         'WScript.Echo RECORDSET1.Fields(i).Value
         RECORDSET2.Fields(i).Value = RECORDSET1.Fields(i).Value
         Next
         RECORDSET2.Update
         RECORDSET1.MoveNext
         Loop
        End Sub
    

    运行此代码时我仍然遇到问题。在运行以下行时:

    RECORDSET2.Open CONNECTION2,adOpenKeyset,adLockOptimistic

    它发出运行时错误“无法启动您的应用程序。工作组文件丢失或由其他用户独占打开”。我对这件事感到非常难过。有任何想法吗。对我而言,它看起来像带有数据源的probs。 注意: - 我没有安装MS访问。

2 个答案:

答案 0 :(得分:3)

这是一个使用ADO的小vbscript(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命令。

答案 1 :(得分:-1)

Open Office可以做到这一点!尝试将Excel文件加载到Open Office并“另存为”csv文件。然后,系统将要求您输入导出文件的编码。选择“Unicode(UTF-8)”。