Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Writing to an excel file
Message
From
11/05/2011 03:47:28
 
 
To
10/05/2011 15:53:56
General information
Forum:
Visual FoxPro
Category:
Client/server
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01510189
Message ID:
01510283
Views:
61
Hi Yossi
Take look at rep2excel file here in downloads section. It contains excel OLE wrapper automation class for
writing to excel without need to dig into complexity of excel populating code snippets.
It lets you populate excel file as if it was 3d array. There are many method calls for easy writing of excel;
like Write array in a row or column, cursor in a section (range), formating of cells, setting print area etc.

Typical code construction ;
local oXL,cOutputFile
oXL=createobject('excel_file_automation')  && instantiate wrapper class instead of plain excel object


***Open some excel template file 
oXL.open_instance('C:\Xl_template.xls')  &&If you do not pass template file, writing starts from empty file


** Write stuff directly using nSheet,nRow,nCol syntax
oXL.make_cell(1,3,4,'Writing')   
oXL.make_cell(1,4,4,'Excel')
oXL.make_cell(1,5,4,'is')
oXL.make_cell(1,6,4, 'Easy')
        *************************************************

** Or use higher level methods

       local nSheet,i,j,cFieldList
       cFieldList='myCursor.field1|myCursor.field2|myCursor.field3'   &&list of a fields delimited by '|'

       nSheet=1  &&First sheet
       i=10          &&Start in row 
       j=3   &&Start in column no


        select myCursor
        scan 
             oXL.fields_in_row(nSheet, i , j,cFieldList)
             i=i+1
        endscan

 
       oXL.set_print_area(1,1,i+1,10)   &&Preset print area in resulting excel
        ******************************

        cOutputFile='c:\myExcel.xls'  &&Preset name/path of resulting file
*       cOutputFile=getfile('xls')   && or call dialog to get name of the resulting file

        **Save file and release object
        oXL.save_as(cOutputFile)
        oXL.release


        ***Excel Done, do whatever with file  



****************************************  More wrapper methods
*     oXL.make_cell_formula()
*                lparameters i,j,k,cFormula

*      oXL.make_cell_format()
*                lparameters i,j,k,cFormat

*     oXL.insert_row()
*                lparameters nAfterRow

*     oXL.set_row_height()
*                lparameters nRow,nHeight

*     oXL.set_column_size()
*                lParameters nCol,nPixSize

*     oXL.get_row_height
*      lparameters nRow

*     oXL.delete_row() 
*                lparameters nRow

*     oXL.array_in_row()
*                lparameters i,j,k,aSomeArray

* ETC
**************************************************
So you get the download, find excel automation class code inside source, and dump it into some of your prg's where you
keep your general functions. Or you simply add prg into your project and set procedure to it.
Then you are good to go.

HTH
Sergio






>>>Hi All:
>>>
>>>I'm trying to populate an Excel file with values using automation, but nothing gets written. What am I doing wrong?
>>>
>>>This is the code:
>>>
>>>
>>>LOCAL loExcel AS Excel.APPLICATION
>>>
>>>loExcel = CREATEOBJECT("Excel.application")
>>>loExcel.VISIBLE = .F.
>>>
>>>loWorkBook = loExcel.Workbooks.OPEN('P:\Flock Information.xls')
>>>loExcel.displayalerts = .F.
>>>
>>>loWorkBook.Sheets("28 Day").SELECT
>>>
>>>FOR i = 288 TO 300
>>>	legRow = 'M' + ALLTRIM(STR(i))
>>>	loWorkBook.ActiveSheet.RANGE("&legRow").select
>>>	loExcel.ActiveCell.Value = ALLTRIM(STR(i))   &&<--- This doesn't work
>>>*!*		loWorkBook.ActiveSheet.RANGE("&legRow").Value = ALLTRIM(STR(i))  &&<---- This doesn't work either
>>>NEXT
>>>
>>>
>>>*  Hardcoding the range also does not work
>>>loWorkBook.ActiveSheet.RANGE("M288").select
>>>loExcel.ActiveCell.Value = "Hello"	
>>>
>>>* Execute this code just to make sure there is a connection, and IT WORKS
>>>lcTest = loWorkBook.ActiveSheet.RANGE("L288").VALUE
>>>? lcTest
>>>	
>>>loExcel.displayalerts = .F.
>>>loExcel.QUIT()
>>>RELEASE loExcel
>>>loExcel = NULL
>>>
>>
>>
>>You don't need to SELECT anything in Excel to refer to it, nor do you need to use character strings to refer to rows and columns. You can use code like:
>>
>>
>>loWorkbook.Sheets("28 Day").Cell(m.i, m.j).Value = ALLTRIM(STR(i))
>>
>>
>>Tamar
>
>Tamar:
>
>I tried this but got 'Invalid Name':
>
>
>lcSheet = "28 Day"
>lcSheet = "Sheet1"
>
>lnColumn = 14
>FOR i = 288 TO 300
>	*legRow = 'M' + ALLTRIM(STR(i))
>	loWorkbook.Sheets(lcSheet).Cell(i, lnColumn).Value = ALLTRIM(STR(i))
>	*loWorkBook.ActiveSheet.RANGE("&legRow").select
>	*loExcel.ActiveCell.Value = ALLTRIM(STR(i))   &&<--- This doesn't work
>*!*		loWorkBook.ActiveSheet.RANGE("&legRow").Value = ALLTRIM(STR(i))  &&<---- This doesn't work either
>NEXT
>
>
>My question is (rather than you spoonfeeding me the solution), whats the best way to get the right syntax, using your line of code as an example? I tried with intellisense, but got lost.
>
>Looking on the web, the only syntax I could find was
>
>
>ActiveCell.Offset(0, -2).Value)
>
>
>Yossi
*****************
Srdjan Djordjevic
Limassol, Cyprus

Free Reporting Framework for VFP9 ;
www.Report-Sculptor.Com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform