*PS : excel-all.h (Excel header file) if you have not open your Excel, Alt+f11, f2 shows oject browser. then find excel constant (which is begin xl ).
Use memotable && your table name or cursor
=Table2ClipBoard()
#include "excel-all.h"
oExcel = createobject("Excel.Application"
With oExcel
.Workbooks.Add
.visible = .t.
With .ActiveWorkbook.ActiveSheet
.Paste
.Range('A1').CurrentRegion.Replace("PMARK",""+chr(10)+"",xlPart,xlByRows, .F.)
Endwith
With .Selection
.ColumnWidth = 50 && Increase width for memo
.HorizontalAlignment = xlGeneral
* .VerticalAlignment = xlTop
.WrapText = .T.
Endwith
Endwith
Function Table2ClipBoard
lcTempFileName = "X"+sys(2015)+".tmp"
handle = fcreate(lcTempFileName) && Create a temp file
#Define TABULATE chr(9)
#Define NL chr(13)
For ix = 1 to fcount()
=fwrite(handle, field(ix))
If ix < fcount()
=fwrite(handle, TABULATE)
Endif
Endfor
=fwrite(handle, NL)
Scan && Start scan..endscan
For ix = 1 to fcount() && Write field values
=fwrite(handle, typeconvert(ix) )
If ix < fcount()
=fwrite(handle, TABULATE)
Endif
Endfor
=fwrite(handle, NL)
Endscan
lnSize=fseek(handle,0,2)
=fseek(handle,0,0)
_Cliptext = fread(handle, lnSize) && Read file to clipboard
=fclose(handle)
Erase (lcTempFileName)
Function typeconvert
Lparameters tnField
lcType = type(field(ix))
If lcType = "G"
lcField = field(ix)
* Return '' && VFP5 and 3
Return transform(&lcField) && VFP6 and up
Endif
luValue = eval(field(ix))
Do case
Case lcType = "D"
lcValue = dtoc(luValue)
Case lcType = "T"
lcValue = ttoc(luValue)
Case lcType $ "NY"
lcValue = padl(luValue,20," "
Case lcType = "L"
lcValue = iif(luValue,"Yes","No"
Case lcType $ "M" && Replace paragraph marks with "PMARK"
lcValue = strtran(luValue, chr(13)+chr(10), "PMARK"
Case lcType $ "C"
lcValue = luValue
Otherwise
lcValue = ""
Endcase
Return alltrim(lcValue)
As you know, the problem with Excel memo fields is not that they are restricted to 255 characters.They're not. But they are restricted to a single paragraph. In other words, you can't have a CHR(13) or CHR(10) in them.
So basically you need to strip out those characters, and replace them with something like a space, which is what you are doing.
I'm facing this same problem in my present application, but fortunately the client undersands the issues and is willing to accept that the memo won't have multiple paragraphs.
Mike Lewis
Edinburgh, Scotland
This old bit of code is OK for outputting smaller files but otherwise its probably too slow. Might give you an idea tho.
Try it on a small file first (there's no progress meter on the screen)
Kaz
*-* mdfile = the data file to output to Excel
*-* mxfile = the Excel file name to Save As
STORE SYS(5)+CURDIR() TO mcurdir
mdfile = ALLTRIM(GETFILE('DBF','File Name','OK',0,'Select Data File to Export'))
STORE SUBSTR(SYS(2015),3,8)+'.XLS' TO mxfile
USE &mdfile
oExcelSession=CreateObject("Excel.Application"
oExcelSession.Workbooks.Add
oExcelSession.Visible=.T.
*- insert column headings
oRange = oExcelSession.ActiveSheet.Range("A1:A1"
WITH oRange
FOR mxc = 1 TO FCOUNT()
.Columns[mxc].Value = FIELD(mxc)
ENDFOR
ENDWITH
*- insert the data for the whole file
STORE 0 TO mcount
SCAN
STORE mcount+1 TO mcount
store "A"+ltrim(str(mcount+1,6)) to mr
store mr+":"+mr to mr
oRange = oExcelSession.ActiveSheet.Range(mr)
WITH oRange
FOR mxc = 1 TO FCOUNT()
STORE FIELD(mxc) TO mf
.Columns[mxc].Value = &mf
ENDFOR
ENDWITH
ENDSCAN
Your code looks good, but you are right that it will be slow.
In general, whenever you reference a COM property or method, there is a significant overhead. When you do it in a loop, as is the case here, that overhead could become noticeable.
Still, your approach is good, and it should work fine for small data.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.