我想制作一个批处理脚本,
a。)将XLS或XLSX转换为CSV b。)将excel的名称写入TXT c。)在脚本的任何连续运行中跳过此excel
问题下面的完整脚本
我目前有以下内容:
1。)运行转换器的批处理文件(由以下链接中的某人提供)
FOR /f "delims=" %%i IN ('DIR *.xlsx /b') DO ExcelToCSV.vbs "Sheet2" "%%i" "%%i.csv" "CSVlog.txt"
2。)ScottF& amp; Christian Lemer在Convert XLS to CSV on command line
if WScript.Arguments.Count < 3 Then
WScript.Echo "Please specify the sheet, the source, the destination files. Usage: ExcelToCsv <sheetName> <xls/xlsx source file> <csv destination file>"
Wscript.Quit
End If
csv_format = 23
Set objFSO = CreateObject("Scripting.FileSystemObject")
src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(1))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(2))
CSVlog = objFSO.GetAbsolutePathName(WScript.Arguments.Item(3))
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)
oBook.Sheets(WScript.Arguments.Item(0)).Select
oBook.Application.Columns("A:J").NumberFormat = "@"
oBook.SaveAs dest_file, csv_format
oBook.Close False
objFSO.opentextfile(CSVlog)
CSVlog.Write(dest_file)
oExcel.Quit
在制作CSV后,脚本退出时出现错误,#34;所需的对象:&#39; CSVlog&#39;
有人可以帮助我吗
1。)删除&#34; xlsx&#34;从CSV名称? (目前CSV存储为&#34; Workbook.xlsx.csv&#34;)
2。)将xlsx名称写为CSVlog.txt
3.)在开始转换之前查询日志文件。如果文件已被处理,则不再处理。
提前谢谢, 海宁完整脚本
嗨,对于所有需要它的人,附上整个脚本。
此脚本将在其文件夹中查找* .xls并将其转换为* .csv。
您可以将* .xls替换为* .xlsb,* .xlsm,* .xlsm
感谢 ScottF,Christian Lemer和MC ND 的原件和帮助!
批处理文件:
:: Feed something into CSVLog to avoid "no search strings" errors
echo ;%date: =_%_%time: =_% >> CSVLog.txt
FOR /f "delims=" %%i IN (
'DIR *.xls /b ^| findstr /v /r /x /g:CSVlog.txt'
) DO ExcelToCSV.vbs "Details" "%%i" "%%~ni.csv" "CSVlog.txt"
我添加了/ r因为/ l会在某些文件名上出现问题。 如果您遇到问题,请替换
findstr /v /r /x /g:CSVlog.txt
与
findstr /v /l /x /g:CSVlog.txt'
VB脚本
REM based on ScottF & Christian Lemer at https://stackoverflow.com/questions/1858195/
REM & MC ND at https://stackoverflow.com/questions/23381408/
REM script will convert a defined Worksheet from an XLS into CSV.Then store the name of the XLS in the CSVlog.txt.
REM it will not process any XLS that are found in the CSVlog.txt
if WScript.Arguments.Count < 4 Then
WScript.Echo "Please specify the sheet, the source, the destination files. Usage: ExcelToCsv <sheetName> <xls/xlsx source file>
<csv destination file>"
Wscript.Quit
End If
csv_format = 23
Set objFSO = CreateObject("Scripting.FileSystemObject")
src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(1))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(2))
CSVlog = objFSO.GetAbsolutePathName(WScript.Arguments.Item(3))
entry = Wscript.Arguments.Item(1)
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)
oBook.Sheets(WScript.Arguments.Item(0)).Select
oBook.Application.Columns("A:J").NumberFormat = "@"
oBook.SaveAs dest_file, csv_format,,,,,,,,,,True
oBook.Close False
oExcel.Quit
objFSO.opentextfile(CSVlog,8,True).WriteLine(Wscript.Arguments.Item(1))
这将使分号分开&#34; CSV&#34;。要制作以逗号分隔的CSV,
从,,,,,,,,,,True
oBook.SaveAs dest_file, csv_format,,,,,,,,,,True
答案 0 :(得分:0)
对于1分和3分
:: Feed something into CSVLog to avoid "no search strings" errors
echo ;%date: =_%_%time: =_% >> CSVLog.txt
FOR /f "delims=" %%i IN (
'DIR *.xlsx /b ^| findstr /v /l /x /g:CSVlog.txt'
) DO ExcelToCSV.vbs "Sheet2" "%%i" "%%~ni.csv" "CSVlog.txt"
findstr
将过滤列表,仅返回CSVlog.txt中未包含的文件(只确保此文件存在)。 %%~ni.csv
(仅限文件名,没有扩展名,添加了.csv
文字)将处理.xlsx
删除
第2点
....
oBook.Close False
oExcel.Quit
objFSO.opentextfile(CSVlog,8,True).WriteLine(Wscript.Arguments.Item(1))
如果您打开文件,则有两种选择。在变量中存储对打开文件的引用,并使用此引用写入文件,或直接使用获取的引用写入文件。第二个选项是使用的。 .xlsx
文件的名称将写入日志,将用于过滤输入文件列表。省略了Close
方法,因为当文件引用超出范围时,文件将被关闭。