天天看點

将兩個excel檔案合并到一個新的Excel檔案中

1、首先需要安裝autoit,這個網上應該有很多

2、建立兩個檔案,裡邊寫一些東西

3、腳本編寫

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.5
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <Excel.au3>
#include <MsgBoxConstants.au3>
#include-once
#include "Array.au3"
#include "ExcelConstants.au3"

Local $oExcel = _Excel_Open()
Local $filePath = $CmdLine[1]
Local $oWorkBook = _Excel_BookOpen($oExcel,$filePath)

Local $targetPath = $CmdLine[2]
Local $oWorkBook1 = _Excel_BookOpen($oExcel,$targetPath)


Local $oWorkBook2 = _Excel_BookNew($oExcel,Default)


Local $oSheetList = _Excel_SheetList($oWorkBook)

Dim $i

For $i=1 To UBound($oSheetList)
   Local $oSheet = _Excel_SheetCopyMove($oWorkbook,$i,$oWorkBook2,$i,False,Default)
   $oSheet.Name = "a" & $i
Next

Local $oSheetList1 = _Excel_SheetList($oWorkBook1)

;_Excel_BookSaveAs($oWorkBook2,"d:/merge.xls")

Dim $j
For $j=1 To UBound($oSheetList1)
   $toIndex = UBound($oSheetList) + $j
   Local $oSheet1 = _Excel_SheetCopyMove($oWorkbook1,$j,$oWorkBook2,$toIndex,False,Default)
   $oSheet1.Name = "a" & $toIndex
Next

_Excel_BookSaveAs($oWorkBook2,"d:/merge.xls")

_Excel_SheetDelete($oWorkBook2,1);

_Excel_Close($oExcel)

MsgBox(64,"提示","執行完成")      

4、按照本機的位數,編譯成相關的exe檔案

将兩個excel檔案合并到一個新的Excel檔案中

5、最後進入到cmd,執行xxx.exe d:/source.xls d:/target.xls

将兩個excel檔案合并到一個新的Excel檔案中

合并後的檔案路徑腳本裡有寫

将兩個excel檔案合并到一個新的Excel檔案中

 合并後的檔案

補充:

将多個Excel檔案合并到一個檔案中

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.5
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <Excel.au3>
#include-once
#include "Array.au3"
#include "ExcelConstants.au3"

Local $source = $CmdLine[1]

Local $sourceArray = StringSplit($source,",")

Local $target = $CmdLine[2]

Local $oExcel = _Excel_Open()

Local $tWorkBook = _Excel_BookNew($oExcel,Default)

Dim $sum = 0

For $i = 1 To $sourceArray[0]
     Local $item = $sourceArray[$i]
     Local $itemWorkBook = _Excel_BookOpen($oExcel,$item)
     Local $itemSheetList = _Excel_SheetList($itemWorkBook)
     For $j = 1 To UBound($itemSheetList)
         $toIndex = $sum + 1
         Local $itemSheet = _Excel_SheetCopyMove($itemWorkBook,$j,$tWorkBook,$toIndex,False,Default)
         $itemSheet.Name = "a" & $toIndex
         $sum = $sum + 1
      Next
Next


_Excel_BookSaveAs($tWorkBook,$target)

_Excel_SheetDelete($tWorkBook,1)

_Excel_Close($oExcel)      
将兩個excel檔案合并到一個新的Excel檔案中

 執行方式

大緻流程:就是将第一個參數切割成數組,然後循環添加到新建立的Excel檔案中