Excel通过脚本导出到.txt [英] Excel export to .txt via script

查看:89
本文介绍了Excel通过脚本导出到.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了将大量Excel单元导出到.txt文件的问题,该文件必然需要采用UTF-8格式.因此,我制作了一个VBScript,该文件由批处理文件执行,并且完全可以完成它的工作(尽管它创建了UTF-16文件).

I got the issue to export a huge amount of Excel cells to a .txt file, which necessarily needs to in UTF-8 format. Therefore I made a VBScript, which is executed by a batch file and it totally does what it should (despite it creates a UTF-16 file).

Set file = fso.OpenTextFile(FILE, 2, True, -1)

在文档中提到-1将生成一个Unicode文件,我非常确定,这仅限于UTF-16.

In the documentation is mentioned that the -1 will generate a Unicode file and I am quite sure, that this is limited to UTF-16.

我现在的问题是:我是否缺少某些东西,或者用VBScript完全不可能做到这一点?有没有更简单的方法?除此之外:这个平台是独立的吗?

My questions are now: am I missing something or it is quite not possible to achieve this with VBScript? Is there an easier way? Besides that: is this platform independent?

推荐答案

FileSystemObject不支持UTF-8,但是

The FileSystemObject doesn't support UTF-8, but ADODB.Stream objects do.

...

'Note: wb is the variable holding your workbook object
'Save worksheet as Unicode text ...
wb.SaveAs "C:\utf16.txt", 42

'... read the Unicode text into a variable ...
Set fso = CreateObject("Scripting.FileSystemObject")
txt = fso.OpenTextFile("C:\utf16.txt", 1, False, -1).ReadAll

'... and export it as UTF-8 text.
Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type     = 2 'text
stream.Position = 0
stream.Charset  = "utf-8"
stream.WriteText txt
stream.SaveToFile "C:\utf8.txt", 2
stream.Close

...

这篇关于Excel通过脚本导出到.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆