Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Dynamically update an Excel file
Message
De
26/04/2001 03:57:11
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
25/04/2001 13:21:13
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00499704
Message ID:
00500007
Vues:
27
>>What do you want to do ? More info pls.
>>Basically :
>
>>oExcel=createobject('Excel.Application')
>>with oExcel
>> .Workbooks.Open(cXLSFileName)
>> *...
>>endwith
>>Cetin
>
>I need to opena an Excel file (which you code explains), then update certain columns with data. Then save and close the file.
>
>Thanks, Chris

Chris,
Yesterday I was about to leave.
Bruce already supplied you with some basics.
When working with Excel or any other OLE Automation via VBA keep in mind VBA code should be at its minimum required. VBA runs awfully slow compared to VFP. ie: huge data might be transferred to Excel. VFP side might complete its job in 1-2 secs but Excel side might need more than a minute to show that data.
Suppose you want to send Excel only and only 10 rows*3 columns data. There are a number of ways to do it. ie:
-Place data in Excel cell by cell (slowest because 30 calls to VBA)
-Copy data to clipboard and paste in Excel
-Copy data to an Excel file, open it, select, copy, paste in target
-Copy data to an Excel file, update predefined links in target
Last 3 all are fast because they use much fewer calls to VBA. That's why I asked what kind of update you need.

Think of this VFP+Excel VBA code :
tStart = seconds()
handle=fcreate('myDummy.txt')
For ix=1 to 100
  For jx=1 to 100
    =fwrite(handle,iif(jx=1,'',chr(9))+;
      'This is row '+transform(ix)+',Column '+transform(jx))
  Endfor
  =fwrite(handle,chr(13)+chr(10))
Endfor
=fclose(handle)
_Cliptext=FileToStr('myDummy.txt')
Erase ('myDummy.txt')
? seconds()-tStart
oExcel = createobject('Excel.Application')
With oExcel
  .Workbooks.Add
  tStart = seconds()
  .Activeworkbook.Activesheet.Range('A1').Pastespecial()
  .visible = .T.
Endwith
? seconds()-tStart

*!*	oExcel = createobject('Excel.Application')
*!*	With oExcel
*!*	  .Workbooks.Add
*!*	  With .Activeworkbook.Activesheet
*!*	    tStart = seconds()
*!*	    For ix=1 to 100
*!*	      For jx=1 to 100
*!*	     .Cells(ix,jx)='This is row '+transform(ix)+',Column '+transform(jx)
*!*	      Endfor
*!*	    Endfor
*!*	  Endwith
*!*	  .visible = .T.
*!*	Endwith
*!*	? seconds()-tStart
VFP part sounds to be dummy ? :) Not at all, it's the fastest part in code. You could still make that part with tables in many different ways (ie: Application.Datatoclip(), copy to .. type delimited with tab etc). It's Excel side that's slow. Try uncommenting the commented part and commenting others to see cell by cell is more than 20 times slower.

Now after saying these another way of sending data to Excel and updating an existing workbook (in code second workbook is a new one -.Add- in your case it would be an existing one. Just for sample in that workbook 1stsheet D14 is assumed to be where update columns would be copied) :
Create cursor myCursor ;
 (myString c(10), myNumber i, myDate d, myDatetime t, myFloat b(4))
Rand(-1)
For ix = 1 to 100
  Insert into myCursor values ;
    (sys(2015),;
    int(rand()*100),;
    int(rand()*100)+date(),;
    int(rand()*86400*100)+datetime(),;
    rand()*1000)
Endfor
lcExcelFile = 'myExcelTest.xls'
Copy to (lcExcelFile) type xls

oExcel = createobject('Excel.Application')
With oExcel
  .Workbooks.Open(sys(5)+curdir()+lcExcelFile) && Open xls we just saved
* Select all data (Currentregion) and copy to clipboard
  .ActiveWorkBook.ActiveSheet.Cells(1,1).CurrentRegion.Copy 
  .ActiveWorkBook.Saved = .T. && File not changed - not needed really
  .Workbooks.Add && Create a new workbook as target
  .ActiveWorkBook.ActiveSheet.Range("D14").PasteSpecial() && Paste
* Empty clipboard so we don't need extra coding to prevent dummy
* 'You placed data on clipboard ...' dialog from excel
  _Cliptext = '' 
  .Workbooks(1).Close(.F.) && Close our intermediate workbook
  .Visible = .T. && Show excel
Endwith
PS:Note that in all samples oExcel.visible is the last call. Experiment how it slows down if you place it earlier in code (but while developing it's wise to have it at top so it would be easier to close excel in case an error in code occurs)
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform