Global Const $xlSheetVisible = -1
MsgBox(4096, "严重警告!", "接下来你将使用本工具把“总部”,“敏泰”,“数码”,“纺机”,“博尚”,“三笠”六张利润表的数据拷贝到你接下来选择的目标文件中。请确认你选择的目标文件是你准备好的空白文件,否则数据覆盖后将不可恢复!", "")
Global $SeleteF = FileOpenDialog("请选择目标文件", "", "Excel文件 (*.xls;*.xlsx)", 1 + 4)
If @error = 1 Then Exit
If MsgBox(1, "提示", "你确定要把所有项目及总部利润表数据复制到:" & $SeleteF & "吗?", "") = 2 Then Exit
Global $oExcel_GL = _ExcelBookOpen($SeleteF)
_ExcelSheetActivate($oExcel_GL, "项目汇总")
Local $sFolder = FileSelectFolder("请选择源文件存放路径.", "", 2);Local
Local $hSearch = FileFindFirstFile($sFolder & "\*.xls")
If $hSearch = -1 Then Exit
While 1
Local $sFile = FileFindNextFile($hSearch);Local
If @error Then ExitLoop
$gFileNanme = _WinAPI_PathFindFileName($SeleteF);$gFileNanme = StringRight($SeleteF, StringLen($SeleteF) - stringinstr($SeleteF, '\', 0,-1));这里用两种方法从路径中提取文件名,一种是用AU3基本函数,另外一中是用UDF函数,都可以的。
If $sFile <> $gFileNanme Then
Local $oExcel = _ExcelBookOpen($sFolder & "" & $sFile)
$close = $oExcel
For $cl In _ExcelSheetList($oExcel)
_ExcelSheetActivate($oExcel, $cl)
Switch _ExcelReadCell($oExcel, 1, 1)
Case "利润表(总部)"
_Copy("利润表(总部)", 4)
Case "利润表(敏泰)"
_Copy("利润表(敏泰)", 8)
Case "利润表(数码)"
_Copy("利润表(数码)", 12)
Case "利润表(纺机)"
_Copy("利润表(纺机)", 16)
Case "利润表(博尚)"
_Copy("利润表(博尚)", 20)
Case "利润表(三笠)"
_Copy("利润表(三笠)", 24)
Case Else
ContinueLoop
EndSwitch
Next
EndIf
_Excelbookclose( $close, 0)
WEnd
FileClose($hSearch)
_ExcelBookClose($oExcel_GL, 1)
_ExcelBookClose($oExcel, 0)
Func _Copy($str, $num)
For $i = 6 To 22
If $i <> 8 And $i <> 18 And $i <> 21 Then
_ExcelWriteCell($oExcel_GL, _ExcelReadCell($oExcel, $i, 3), $i - 2, $num) ;写入内容到单元格
EndIf
Next
EndFunc ;==>_Copy
;
;
;UDF函数
;-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
;-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
;
Func _WinAPI_PathFindFileName($sPath);从路径中提取文件名
Local $Ret = DllCall('shlwapi.dll', 'wstr', 'PathFindFileNameW', 'wstr', $sPath)
If @error Then
Return SetError(1, 0, '')
EndIf
Return $Ret[0]
EndFunc ;==>_WinAPI_PathFindFileName
Func _ExcelBookOpen($sFilePath, $fVisible = 1, $fReadOnly = False, $sPassword = "", $sWritePassword = "")
Local $oExcel = ObjCreate("Excel.Application")
If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
If Not FileExists($sFilePath) Then Return SetError(2, 0, 0)
If $fVisible > 1 Then $fVisible = 1
If $fVisible < 0 Then $fVisible = 0
If $fReadOnly > 1 Then $fReadOnly = 1
If $fReadOnly < 0 Then $fReadOnly = 0
With $oExcel
.Visible = $fVisible
If $sPassword <> "" And $sWritePassword <> "" Then .WorkBooks.Open($sFilePath, Default, $fReadOnly, Default, $sPassword, $sWritePassword)
If $sPassword = "" And $sWritePassword <> "" Then .WorkBooks.Open($sFilePath, Default, $fReadOnly, Default, Default, $sWritePassword)
If $sPassword <> "" And $sWritePassword = "" Then .WorkBooks.Open($sFilePath, Default, $fReadOnly, Default, $sPassword, Default)
If $sPassword = "" And $sWritePassword = "" Then .WorkBooks.Open($sFilePath, Default, $fReadOnly)
; Select the first *visible* worksheet.
For $i = 1 To .ActiveWorkbook.Sheets.Count
If .ActiveWorkbook.Sheets($i).Visible = $xlSheetVisible Then
.ActiveWorkbook.Sheets($i).Select()
ExitLoop
EndIf
Next
EndWith
Return $oExcel
EndFunc ;==>_ExcelBookOpen
Func _ExcelSheetActivate($oExcel, $vSheet)
If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
If IsNumber($vSheet) Then
If $oExcel.ActiveWorkbook.Sheets.Count < $vSheet Then Return SetError(2, 0, 0)
Else
Local $fFound = 0
Local $aSheetList = _ExcelSheetList($oExcel)
For $xx = 1 To $aSheetList[0]
If $aSheetList[$xx] = $vSheet Then $fFound = 1
Next
If Not $fFound Then Return SetError(3, 0, 0)
EndIf
$oExcel.ActiveWorkbook.Sheets($vSheet).Select()
Return 1
EndFunc ;==>_ExcelSheetActivate
Func _ExcelSheetList($oExcel)
If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
Local $iTemp = $oExcel.ActiveWorkbook.Sheets.Count
Local $aSheets[$iTemp + 1]
$aSheets[0] = $iTemp
For $xx = 1 To $iTemp
$aSheets[$xx] = $oExcel.ActiveWorkbook.Sheets($xx).Name
Next
Return $aSheets
EndFunc ;==>_ExcelSheetList
Func _ExcelReadCell($oExcel, $sRangeOrRow, $iColumn = 1)
If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
If Not StringRegExp($sRangeOrRow, "[A-Z,a-z]", 0) Then
If $sRangeOrRow < 1 Then Return SetError(2, 0, 0)
If $iColumn < 1 Then Return SetError(2, 1, 0)
Return $oExcel.Activesheet.Cells($sRangeOrRow, $iColumn).Value
Else
Return $oExcel.Activesheet.Range($sRangeOrRow).Value
EndIf
EndFunc ;==>_ExcelReadCell
Func _ExcelWriteCell($oExcel, $sValue, $sRangeOrRow, $iColumn = 1)
If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
If Not StringRegExp($sRangeOrRow, "[A-Z,a-z]", 0) Then
If $sRangeOrRow < 1 Then Return SetError(2, 0, 0)
If $iColumn < 1 Then Return SetError(2, 1, 0)
$oExcel.Activesheet.Cells($sRangeOrRow, $iColumn).Value = $sValue
Return 1
Else
$oExcel.Activesheet.Range($sRangeOrRow).Value = $sValue
Return 1
EndIf
EndFunc ;==>_ExcelWriteCell
Func _ExcelBookClose($oExcel, $fSave = 1, $fAlerts = 0)
If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
Local $sObjName = ObjName($oExcel)
If $fSave > 1 Then $fSave = 1
If $fSave < 0 Then $fSave = 0
If $fAlerts > 1 Then $fAlerts = 1
If $fAlerts < 0 Then $fAlerts = 0
; Save the users specified settings
Local $fDisplayAlerts = $oExcel.Application.DisplayAlerts
Local $fScreenUpdating = $oExcel.Application.ScreenUpdating
; Make necessary changes
$oExcel.Application.DisplayAlerts = $fAlerts
$oExcel.Application.ScreenUpdating = $fAlerts
Switch $sObjName
Case "_Workbook"
If $fSave Then $oExcel.Save()
; Check if multiple workbooks are open
; Do not close application if there are
If $oExcel.Application.Workbooks.Count > 1 Then
$oExcel.Close()
; Restore the users specified settings
$oExcel.Application.DisplayAlerts = $fDisplayAlerts
$oExcel.Application.ScreenUpdating = $fScreenUpdating
Else
$oExcel.Application.Quit()
EndIf
Case "_Application"
If $fSave Then $oExcel.ActiveWorkBook.Save()
$oExcel.Quit()
Case Else
Return SetError(1, 0, 0)
EndSwitch
Return 1
EndFunc ;==>_ExcelBookClose
请教各位,谢谢!