Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Combining XLS files into one workbook
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
COM/DCOM et OLE Automation
Versions des environnements
Visual FoxPro:
VFP 6 SP5
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
Visual FoxPro
Divers
Thread ID:
01035967
Message ID:
01102461
Vues:
16
Hi Borislav,

I have it now working just fine using Craig's utility. Here is the code as I have it now:
************************************************************
*  Function CombineExcelFiles
************************************************************
*  Author............: VCS  Developers Team
*  Project...........: Visual Collections System
*  Created...........: Craig Boyd 3/6/2006  23:55:50
*  Copyright.........: (c) Jzanus, 2006
*) Description.......:
*  Calling Samples...: DIMENSION aXLSFiles(3)
*!*					aXLSFiles(1) = "C:\temp1.xls"
*!*					aXLSFiles(2) = "C:\temp2.xls"
*!*					aXLSFiles(3) = "C:\temp3.xls"
*!*					CombineExcelFiles(@aXLSFiles, "C:\XLSCombined.xls")
*  Parameter List....:
*  Major change list.:
function CombineExcelFiles (taXLSFiles, tcDestination, tlDeleteOriginal)
external array taXLSFiles
local loExcel as Excel.application, ;
	loWorkBook as Excel.Worksbook, ;
	loWorkSheet , ;
	lnCounter, lcWorkSheetCaption, lcError, ;
	lcValidChars

lcError = ""

try
	lcValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "
	loExcel = newobject("Excel.Application")
	with loExcel
		.ScreenUpdating = .f.
		.DisplayAlerts = .f.
		.WorkBooks.add()
		lnCounter = 0
		
		** Delete all existing worksheets except 1
		for each loWorkSheet in .WorkBooks(1).WorkSheets
			lnCounter = m.lnCounter + 1
			IF m.lnCounter > 1
				loWorkSheet.delete
			endif	
		endfor

		for lnCounter = 1 to alen(taXLSFiles,1)
			if file(taXLSFiles[m.lnCounter])
				lcWorkSheetCaption = juststem(taXLSFiles[m.lnCounter])
				loWorkBook = .WorkBooks.open(taXLSFiles[m.lnCounter])
				loWorkBook.WorkSheets(1).copy(null, ;
					.WorkBooks(1).WorkSheets(.WorkBooks(1).WorkSheets.count))
				.WorkBooks(1).ActiveSheet.name = ;
					right(alltrim(chrtran(m.lcWorkSheetCaption, ;
					chrtran(m.lcWorkSheetCaption,m.lcValidChars,"")," ")), 31) &&loWorkBook.Name
				loWorkBook.close(.f.) && Don't save changes
				if m.tlDeleteOriginal
					erase (taXLSFiles[m.lnCounter])
				endif
			endif
		endfor
		** Remove the first original sheet from (Sheet1)
		.Workbooks(1).WorkSheets(1).Delete

		.WorkBooks(1).saveas(m.tcDestination)
		.ScreenUpdating = .t.
		.DisplayAlerts = .t.
	endwith

catch to loError
	lcError = Log_Error(m.loError)
finally
	if vartype(m.loExcel) = 'O'
		with loExcel
			.ScreenUpdating = .t.
			.DisplayAlerts = .t.
			.quit()
		endwith
	endif
endtry

if not empty(m.lcError)
	=ErrorMsg(m.lcError)
endif

return m.lcError

endfunc
>Something like that?
>
>DIMENSION MyExcelFiles[5]
>MyExcelFiles[1] = "c:\XlsF1.XLS"
>MyExcelFiles[2] = "c:\XlsF2.XLS"
>MyExcelFiles[3] = "c:\XlsF3.XLS"
>MyExcelFiles[4] = "c:\XlsF4.XLS"
>MyExcelFiles[5] = "c:\XlsF5.XLS"
>
>
>
>FUNCTION CombineExcel(laExcelFiles, lcNewFileName)
>    LOCAL oExcel AS Excel.Application
>    LOCAL nXls, cActive, cNewBook
>    oExcel = CREATEOBJECT("Excel.Application")
>    oExcel.WorkBooks.New()
>    oExcel.WorkBooks.SaveAs(lcNewFileName)
>    cActive = oExcel.ActiveWorkbook.Name
>    FOR nXLS = 1 TO ALEN(laExcelFiles,1)
>        ** Because COPY TO XL produce only one sheet we must insert a new one here
>        oExcel.ActiveWorkbook.Sheets.Add()
>        oExcel.WorkBooks.Open(laExcelFiles[nXLS])
>        cNewBook = oExcel.ActiveWorkbook.Name
>        oExcel.ActiveWorkbook.ActiveSheet.Cells.Select()
>        oExcel.Selection.Copy()
>        oExcel.Windows(oActive).Activate
>        oExcel.ActiveWorkbook.ActiveSheet.Paste()
>        oExcel.Windows(cNewBook).Activate
>        oExcel.ActiveWorkbook.Close()
>        oExcel.Windows(oActive).Activate
>    NEXT
>    oExcel.ActiveWorkbook.Save()
>    oExcel.Quit()
>    oExcel = NULL
>    RELEASE oExcel
>RETURN
>
>Not tested
>
>
>>>>Sergey,
>>>>
>>>>Thanks for the tip! Now, who has done this and might you have some code you might be willing to share?
>>>>
>>>>Thanks!
>>>
>>>oExcel = CREATEOBJECT("Excel.Application")
>>>DIMENSION MyExcelFiles[5]
>>>MyExcelFiles[1] = "c:\XlsF1.XLS"
>>>MyExcelFiles[2] = "c:\XlsF2.XLS"
>>>MyExcelFiles[3] = "c:\XlsF3.XLS"
>>>MyExcelFiles[4] = "c:\XlsF4.XLS"
>>>MyExcelFiles[5] = "c:\XlsF5.XLS"
>>>
>>>oExcel.WorkBooks.Open(MyExcelFiles[1])
>>>cActive = oExcel.ActiveWorkbook.Name
>>>FOR nXLS = 2 TO 5
>>>    ** Because COPY TO XL produce only one sheet we must insert a new one here
>>>    oExcel.ActiveWorkbook.Sheets.Add()
>>>    oExcel.WorkBooks.Open(MyExcelFiles[nXLS])
>>>    cNewBook = oExcel.ActiveWorkbook.Name
>>>    oExcel.ActiveWorkbook.ActiveSheet.Cells.Select()
>>>    oExcel.Selection.Copy()
>>>    oExcel.Windows(oActive).Activate
>>>    oExcel.ActiveWorkbook.ActiveSheet.Paste()
>>>    oExcel.Windows(cNewBook).Activate
>>>    oExcel.ActiveWorkbook.Close()
>>>    oExcel.Windows(oActive).Activate
>>>NEXT
>>>oExcel.ActiveWorkbook.Save()
>>>oExcel.Quit()
>>>oExcel = NULL
>>>RELEASE oExcel
>>>
>>>Something like that
>>
>>Great code. But as I see in yours and Craig examples, you add new sheets with the informaiton to the first file, e.g. modifying the first Excel file. I was wondering if it's possible to do the same using a new clean file as output and don't touch the files from array?
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform