xiaoquansb 发表于 2010-8-22 21:30:00

[求助]用lisp调用vb把ANSI编码的文件转换成UTF-8


Private Sub Command1_Click()
Dim FileName As String, b() As Byte, S As String
Dim ADO_Stream As Object
FileName = App.Path & "\1.txt" '请修改为具体文件名
If Dir(FileName) = "" Then Exit Sub
ReDim b(FileLen(FileName))
Open FileName For Binary As #1
Get #1, , b
Close #1
S = StrConv(b, vbUnicode)

Set ADO_Stream = CreateObject("ADODB.Stream")

With ADO_Stream
.Type = 2
.Mode = 3
.Charset = "utf-8"
.open
.WriteText S
.SaveToFile FileName, 2
End With

Set ADO_Stream = Nothing
MsgBox "文件转换完毕!"
End Sub


上面是一段vb程序源码,可把ANSI编码的文件转换成UTF-8
可怜小弟太菜,不懂vb,只懂一点lsp
哪位老师可代为修改一下,让lsp调用
对话框就不用了,给出文件地址,自动转换就可以了,不用什么提示的
先谢谢了

xshrimp 发表于 2019-11-3 22:44:39

本帖最后由 xshrimp 于 2019-12-17 09:34 编辑

UTF-8编码的文件转换成ANSI
(xzj-file-WriteTextFile"c:\\ansi.txt"   (xzj-file-ReadTextFile    "c:\\ansi.txt""utf-8")   "gb2312")

ANSI编码的文件转换成UTF-8

(xzj-file-WriteTextFile"c:\\ansi.txt"   (xzj-file-ReadTextFile   "c:\\ansi.txt""gb2312")   "utf-8")


(defun xzj-file-ReadTextFile (File charset / encode filepath filestream)
(if
    (and
      (setq FilePath   (findfile File))
      (setq FileStream (vlax-create-object "Adodb.Stream"))
    )
    (progn                        
      (vlax-put-property FileStream 'Type 2) ;1二进制读取 2文本模读取
      (vlax-put-property FileStream 'mode 3) ;1-读,2-写,3-读写
      (vlax-put-property FileStream 'charset charset)      ;unicode;utf-8;Ascii;gb2312;big5;gbk               
      (vlax-invoke FileStream 'Open)                        
      (vlax-invoke FileStream 'LoadFromFile filePath)                              
      (setq Encode (vlax-invoke FileStream 'readtext))
      (vlax-invoke FileStream 'close)
      (vlax-release-object FileStream)
      Encode
    )
)
)(defun xzj-file-WriteTextFile (File charset str /filestream)
;;;判断文件路径是否存在
(if (findfile (vl-filename-directory File))
   (if
          (setq FileStream (vlax-create-object "Adodb.Stream"))
          (progn                        
            (vlax-put-property FileStream 'Type 2) ;1二进制读取 2文本模读取
            (vlax-put-property FileStream 'mode 3) ;1-读,2-写,3-读写
            (vlax-put-property FileStream 'charset charset)      ;unicode;utf-8;Ascii;gb2312;big5;gbk               
            (vlax-invoke FileStream 'Open)
            (vlax-invoke FileStream 'WriteText str)                        
            (vlax-invoke FileStream 'SaveToFile File 2)
            (vlax-invoke FileStream 'flush)
            (vlax-invoke FileStream 'Close)                        
            (vlax-release-object FileStream)
            T                        
          )
      )
   (progn
   (princ "\n文件路径不存在,无法创建文件!")
      nil
   )   
)   
)


wudechao 发表于 2020-2-25 02:20:04

本帖最后由 wudechao 于 2020-2-25 02:25 编辑

xshrimp 发表于 2019-11-3 22:44
UTF-8编码的文件转换成ANSI
(xzj-file-WriteTextFile"c:\\ansi.txt"   (xzj-file-ReadTextFile    "c:\\ ...
;我把它合并
;例如 (file-encode-trans"c:\\ansi.txt"   "gb2312"    "utf-8")
(defun file-encode-trans (file charset1 charset2 / obj encode)
(setq obj (vlax-create-object "ADODB.Stream"))
(vlax-put-property obj 'type 2);1二进制读取 2文本模读取
(vlax-put-property obj 'mode 3);1-读,2-写,3-读写
(vlax-invoke obj 'open)
(vlax-put-property obj "charset" charset1);;unicode;utf-8;ascii;gb2312;big5;gbk
(vlax-invoke-method obj 'loadfromfile file)
(setq encode (vlax-invoke obj 'readtext))
(vlax-invoke obj 'close)
(vlax-release-object obj)
(setq obj (vlax-create-object "ADODB.Stream"))
(vlax-put-property obj 'type 2);1二进制读取 2文本模读取
(vlax-put-property obj 'mode 3);1-读,2-写,3-读写
(vlax-invoke obj 'open)
(vlax-put-property obj "charset" charset2);;unicode;utf-8;ascii;gb2312;big5;gbk
(vlax-invoke obj 'writetext encode)
(vlax-invoke-method obj 'savetofile file 2);1新建,2覆盖
(vlax-invoke obj 'flush);将缓存中的数据强制输出
(vlax-invoke obj 'close)
(vlax-release-object obj)
)


xshrimp 发表于 2010-8-23 14:35:00

检查文件的编码<div><div>;;命令: (GPS-&gt;CHECKCODE "c:\\unicode big endian.txt")</div><div>;;"unicode big endian"</div><div>;;命令: (GPS-&gt;CHECKCODE "c:\\unicode.txt")</div><div>;;"Unicode"</div><div>;;命令: (GPS-&gt;CHECKCODE "c:\\utf-8.txt")</div><div>;;"UTF-8"</div><div>;;命令: (GPS-&gt;CHECKCODE "c:\\ansi.txt")</div><div>;;"GB2312"</div></div><div><br/></div><div>(gps-&gt;WriteToFile &nbsp;"c:\\utf8.txt" &nbsp;"Str" "utf-8")</div><div>(gps-&gt;ReadFile &nbsp; &nbsp; "c:\\ansi.txt" "gb2312")</div><div><br/></div><div>转化文件编码</div><div><b style="line-height: 15px; font-size: 12px; "><font class="Apple-style-span" color="#FF00FF">ANSI编码的文件转换成UTF-8</font></b></div><div><font class="Apple-style-span" color="#FF00FF">(gps-&gt;gtu &nbsp;"c:\\ansi.txt")</font></div>

xiaoquansb 发表于 2010-8-23 18:30:00

太感谢了

xshrimp 发表于 2010-8-23 19:19:00

<div style="font-family: Arial, Helvetica, sans-serif; list-style-type: none; list-style-position: initial; list-style-image: initial; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 24px; "><div style="font-family: Arial, Helvetica, sans-serif; list-style-type: none; list-style-position: initial; list-style-image: initial; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 24px; "><span class="Apple-style-span" style="font-family: 'verdana,宋体'; line-height: 14px; font-size: 14px; "><b style="line-height: 15px; font-size: 12px; "><font class="Apple-style-span" color="#FF00FF" style="line-height: 18px; ">UTF-8</font></b><b style="line-height: 15px; font-size: 12px; "><font class="Apple-style-span" color="#FF00FF" style="line-height: 18px; ">编码的文件转换成</font></b><b style="line-height: 15px; font-size: 12px; "><font class="Apple-style-span" color="#FF00FF" style="line-height: 18px; ">ANSI</font></b></div><div style="font-family: Arial, Helvetica, sans-serif; list-style-type: none; list-style-position: initial; list-style-image: initial; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 24px; ">(gps-&gt;WriteToFile &nbsp;"c:\\ansi.txt"&nbsp;&nbsp;&nbsp;(gps-&gt;ReadFile &nbsp; &nbsp; "c:\\ansi.txt" &nbsp;"utf-8") &nbsp;&nbsp;"gb2312")</div><div style="font-family: Arial, Helvetica, sans-serif; list-style-type: none; list-style-position: initial; list-style-image: initial; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 24px; "><br/></div><div style="font-family: Arial, Helvetica, sans-serif; list-style-type: none; list-style-position: initial; list-style-image: initial; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 24px; "><b style="line-height: 15px; font-size: 12px; "><font class="Apple-style-span" color="#FF00FF" style="line-height: 18px; ">ANSI编码的文件转换成UTF-8</font></b></div></div><div style="font-family: Arial, Helvetica, sans-serif; list-style-type: none; list-style-position: initial; list-style-image: initial; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 24px; ">(gps-&gt;WriteToFile &nbsp;"c:\\ansi.txt"&nbsp;&nbsp;&nbsp;(gps-&gt;ReadFile &nbsp; &nbsp; "c:\\ansi.txt" &nbsp;"gb2312") &nbsp;&nbsp;"utf-8")</div></span>

xiaoquansb 发表于 2010-8-23 22:50:00

<p>再次感谢<font face="Verdana" color="#61b713"><b>xshrimp</b></font>,这个对我来说太重要了</p>

flowerson 发表于 2019-11-1 16:11:07

xshrimp 发表于 2010-8-23 19:19
UTF-8编码的文件转换成ANSI(gps-&gt;WriteToFile &nbsp;"c:\\ansi.txt"&nbsp;&nbsp;&nbsp;(gps-&gt;ReadFile...

不知可否提供源码下载?可以收点明经币都可以。

flowerson 发表于 2019-11-4 01:15:12

xshrimp 发表于 2019-11-3 22:44


非常谢谢xshrimp。xzj-file-WriteTextFile 里面的三参数顺序 File charset str 应该是Filestr charset对吧?

wudechao 发表于 2020-2-25 02:31:00

flowerson 发表于 2019-11-4 01:15
非常谢谢xshrimp。xzj-file-WriteTextFile 里面的三参数顺序 File charset str 应该是Filestr charset ...

确实如此!
页: [1] 2
查看完整版本: [求助]用lisp调用vb把ANSI编码的文件转换成UTF-8