目的:
省去開啟EXCEL檔,轉存為CSV檔之手動作業,縮短作業時間,提高工作效率,尤其是對象為複數個檔案場合
1,利用vbs 設計程式,執行後,便可轉換 產出csv檔
1.1 vbs程式應用說明
* input file ---由vbs 第一個引數取得 (即變數 src_file 之Excel file name )
* output file---由vbs 第二個引數取得 (即變數名dest_file 之CSV file name )
* 處理流程 :
1.取得引數
2. Open EXCEL
3.OPEN Excel Book
4. Save as CSV file format --- csv file 不存在時會新增csv file,若既存 會詢問是否覆蓋
5.Close
1.2 程式設計如下參考:
REM --vbs for EXCEL save as csv file--------
if WScript.Arguments.Count < 2 Then
WScript.Echo"Please specify the source and the destination files. Usage: ExcelToCsv <source file: excel> <destination file: csv>"
Wscript.Quit
End If
csv_format = 6
Set objFSO = CreateObject("Scripting.FileSystemObject")
src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)
oBook.SaveAs dest_file, csv_format
oBook.Close False
oExcel.Quit
2.一次轉換一個EXCEL檔
2.1 於cmd 輸入如下命令,便可自動完成轉換作業
CScript //B excel2csv.vbs inpExcelFName outCSVFName
* 輸入決定兩個引數,便可執行
兩個引數亦可以變數代稱, 如set PARi=ExcelFName;set PARo=CSVFName
2.2上述亦可設計cmd執行檔,執行該cmd便可自動完成轉換作業
REM --cmd for EXCEL 轉成 csv -----------
del SH.csv
CALL CScript //B excel2csv.vbs SH.xlsx SH.csv
EXIT
3.整批轉換多個EXCEL檔
3.1 設計cmd執行檔,執行該cmd,便可自動完成整個資料夾內所有EXCEL檔之轉換。
日後,資料夾內之EXCEL檔名,或檔案內容,檔案數量之變更追加均無影響,該cmd 仍可正常執行
3.2 實例說明
* input excel file 可將資料夾內複數個excel 檔案,全部取出作轉換
* output CSV file,執行後產出多個CSV檔,並 存放在指定的資料夾內
*出力之csv file ,若既存 會詢問是否覆蓋
BATCH作業為了省略該動作,故設計為csv 可先全部刪除,再讓它全部新增產出
* 本例input file 共31 個Excel檔案,如下
* 本cmd 執行後, 新增產出 CSV file 共31 個檔案,如下
* 本cmd執行後結果產出之訊息,記錄於 txt file ,檔名: Result_Vol_Xls2Csv.txt ,可供使用者確認,內容如下
* ** Result of excel convert ** * 2024/07/23 週二 11:25:49.48
--------- 執行動作說明 對象檔名 --------
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-偉豪Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-孟陽Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-家苗Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-廖秀玉Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-招治Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-捷女Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-春英Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-林家在Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-林美珠Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-楊雪梨Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-楚玟Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-歐坤一Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-洪王麗珠Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-玉潔Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-王瑛瑛Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-玲慧Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-素梅Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-美玲Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-范美珠Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-趙羅秀英Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-進輝Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-郭秀珍Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-金美娟Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-金菊Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-錦珍Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-陳春香Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-陳禮華Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-陳秀貞Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-雅珶Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-鳳英Y".csv
** already coverted csv file save as C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工\CSV\"衛福部志工服務數-黃敏Y".csv
------------ Convert to csv completed 正常終了 2024/07/23 週二 11:28:19.07
3.3 本cmd檔名: Vol_Xls2Csv.cmd,實例設計如下參考:
@echo off
rem set PAR1=D:\志工\志工EXCEL
set PAR1=C:\Users\admin\Desktop\衛福部志工系統-志工時數\志工
C:
rem D:
cd %PAR1%
echo.
echo *** Convert Excel sheets to csv files ***
IF NOT EXIST %PAR1% ( echo ### ERROR : %PAR1% is not exist
pause & Goto EJob )
::-----vbs執行轉換時,若csv檔名已存在,會跳出視窗,問是否要覆蓋
echo ***
set /p SW="** Do you delete all csv files ? (Y/N) :"
if /i %SW% == Y (del %PAR1%\CSV\*.*
echo * already deleted all csv file in %PAR1%\CSV\*.* >> Result_Vol_Xls2Csv.txt)
::----執行結果出力之表頭設定
echo. > Result_Vol_Xls2Csv.txt
echo * ** Result of excel convert ** * %DATE% %TIME% >> Result_Vol_Xls2Csv.txt
echo. >> Result_Vol_Xls2Csv.txt
echo --------- 執行動作說明 對象檔名 -------- >> Result_Vol_Xls2Csv.txt
echo ***
echo *** Convert Excel sheets to csv files in process 執行中...
::-----讀取所有excel檔,並執行轉換為csv檔
For /f "tokens=1 delims=?" %%A in ( 'forfiles /P "%PAR1%" /M "*.xls*" /c "cmd /c echo @fname" ') do (
CALL :Xls2Csv %%A)
echo ***
echo *** Convert Excel sheets to csv files completed 執行完了
echo ------------ Convert to csv completed 正常終了 %DATE% %TIME% >> Result_Vol_Xls2Csv.txt
:EJob
pause ejob
EXIT
:Xls2Csv
SET xlsFName=%1
::-----CSV子目錄需先存在,Eexcel無副檔名xls 仍可;csv出力時檔名不能既存,需先刪除
IF EXIST %PAR1%\CSV\%xlsFName%.csv (del %PAR1%\CSV\%xlsFName%.csv
echo * already deleted old csv file %PAR1%\CSV\%xlsFName%.csv >> Result_Vol_Xls2Csv.txt)
CALL CScript //B excel2csv.vbs %PAR1%\%xlsFName% %PAR1%\CSV\%xlsFName%.csv
echo ** already coverted csv file save as %PAR1%\CSV\%xlsFName%.csv >> Result_Vol_Xls2Csv.txt
rem pause ---run vbs?
EXIT /B
-----by linct------