vba 建立txt檔案 並且寫入字串

2021-09-10 15:45:32 字數 1101 閱讀 4514

有的時候,我們在操作excel的時候,

最後常常需要輸出乙個記錄的log,這樣可以供後期追溯。

主要使用createtextfile方法。

private sub createtxtfile()

dim fso as object

dim mytxt as object

dim myfname as string

dim i as integer

dim nowdate as string

dim sht as worksheet

nowdate = cdate(now())

nowdate = replace(nowdate, ":", "") '去除now()函式的返回的冒號

nowdate = replace(nowdate, "/", "") '去除now()函式的返回的斜槓

nowdate = replace(nowdate, " ", "_") '去除now()函式的返回的空格

myfname = thisworkbook.path & "/" & nowdate & ".txt" '確定新建的txt檔案的路徑

set fso = createobject("scripting.filesystemobject") '建立檔案需要使用scripting.filesystemobject物件

set mytxt = fso.createtextfile(filename:=myfname, overwrite:=true) '使用createtextfile建立檔案

for each sht in thisworkbook.worksheets '迴圈每乙個sheet

mytxt.write sht.name & vbcrlf '使用write方法寫入sheet名,然後插入乙個換行符

next

mytxt.close

set mytxt = nothing

set fso = nothing

end sub

**演示如下:

txt檔案寫入

txt檔案的讀取 public static void main string args system.out.println 一共行數 i 寫入txt檔案 方法一 file file new file g aaa bbb ceshi.txt 相對路徑,如果沒有則要建立乙個新的output。txt檔...

C 寫入txt 檔案

filestream fs new filestream c s.txt filemode.create,fileaccess.write streamwriter sw new streamwriter fs sw.basestream.seek 0,seekorigin.begin for in...

PHP寫入txt檔案

myfiletoread fopen txt readme.txt r or die unable to open file myfileread fread myfiletoread,20 myfiletowrite fopen txt writeme.txt a or die unable to...