复制代码 代码如下:
Const xlWorkbookNormal = -4143
Const xlSaveChanges = 1
objStartFolder = "c:/test" '要读取的源文件目录
desExcel= "c:/result1.xls" '最后生成的汇总excel
Set ExcelApp = CreateObject("Excel.Application")
Set destbook = ExcelApp.Workbooks.Add '创建空文件
Set objFSO = CreateObject("Scripting.FileSystemObject")'建立filesystemobject
Set objFolder = objFSO.GetFolder(objStartFolder)'获取文件夹
Set colFiles = objFolder.Files '获得源目录下所有文件
intRow=1 '行数
For Each objFile in colFiles '依次处理文件夹中的文件
If UCase(Right(Trim(objFile.Name), 3)) ="XLS" Then '只处理xls文件
Set srcbook = ExcelApp.Workbooks.Open(objStartFolder + "/" + objFile.Name) '打开xls文件
'srcbook.Worksheets(1).Copy destbook.Worksheets(1)
srcbook.activate
intCol = 1 '列数
Do Until ExcelApp.Cells(1,intCol).Value = ""
tempdata=ExcelApp.Cells(1, intCol).Value
destbook.activate
ExcelApp.Cells(intRow, intCol).Value=tempdata
srcbook.activate
intCol = intCol + 1
Loop
srcbook.Close '关闭已经打开的xls文件
End If
intRow=intRow+1
Next
destBook.SaveAs desExcel,xlWorkbookNormal
destBook.close xlSaveChanges
ExcelApp.quit