Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
VFPxWorkbookXLSX Manual File Creation
Message
From
07/05/2021 16:34:29
 
 
To
06/05/2021 21:34:56
General information
Forum:
Visual FoxPro
Category:
VFPX/Sedna
Miscellaneous
Thread ID:
01680200
Message ID:
01680223
Views:
31
>Below is an example (snippet of code from one of my classes) of creating a new workbook and populating it from a grid (in my case the grid is an ActiveX spreadsheet control). There are other methods for saving a table/cursor or a grid to a workbook directly -- see methods SaveGridToWorkbook(), SaveGridToWorkbookEx(), SaveTableToWorkbook(), and SaveTableToWorkbookEx(). The 'Ex' methods will directly write to the xlsx format and is fast. The other methods write to the internal cursors which allow you the developer to assign cell formatting or to do more with the workbook before saving. The non-Ex methods requires you to save the workbook with SaveWorkbook() method.
>
>
>loExcel = NEWOBJECT("VFPxWorkbookXLSX", "VFPxWorkbookXLSX.vcx")
>lnWB = loExcel.CreateWorkbook(FORCEEXT(toFileNames.List[1], "xlsx"))
>IF lnWB > 0
>*-*	Create the cell format styles
>	lnStyleHdr = loExcel.CreateFormatStyle(lnWB)
>	loExcel.AddStyleFont(lnWB, lnStyleHdr, "Arial", 10, True, False, RGB(255,255,255))
>	loExcel.AddStyleFill(lnWB, lnStyleHdr, RGB(51,102,255), RGB(51,102,255))
>
>	lnStyleTxt = loExcel.CreateFormatStyle(lnWB)
>	loExcel.AddStyleFont(lnWB, lnStyleTxt, "Arial", 10)
>
>	lnStyleMId = loExcel.CreateFormatStyle(lnWB)
>	loExcel.AddStyleFont(lnWB, lnStyleMId, "Arial", 10)
>	loExcel.AddStyleNumericFormat(lnWB, lnStyleMId, CELL_FORMAT_TEXT)
>
>	lnStyleAmt = loExcel.CreateFormatStyle(lnWB)
>	loExcel.AddStyleFont(lnWB, lnStyleAmt, "Arial", 10)
>	loExcel.AddStyleNumericFormat(lnWB, lnStyleAmt, CELL_FORMAT_CURRENCY_RED)
>
>	lnStyleDte = loExcel.CreateFormatStyle(lnWB)
>	loExcel.AddStyleFont(lnWB, lnStyleDte, "Arial", 10)
>	loExcel.AddStyleNumericFormat(lnWB, lnStyleDte, CELL_FORMAT_DATE_DMMMYY)
>
>	lnStyleTot = loExcel.CreateFormatStyle(lnWB)
>	loExcel.AddStyleFont(lnWB, lnStyleTot, "Arial", 10, True)
>	loExcel.AddStyleFill(lnWB, lnStyleTot, RGB(221,235,247), RGB(221,235,247))
>	loExcel.AddStyleNumericFormat(lnWB, lnStyleTot, CELL_FORMAT_CURRENCY_RED)
>
>	lnStyleBot = loExcel.CreateFormatStyle(lnWB)
>	loExcel.AddStyleFont(lnWB, lnStyleBot, "Arial", 10, True)
>	loExcel.AddStyleFill(lnWB, lnStyleBot, RGB(221,235,247), RGB(221,235,247))
>	loExcel.AddStyleNumericFormat(lnWB, lnStyleBot, CELL_FORMAT_CURRENCY_RED)
>	loExcel.AddStyleBorders(lnWB, lnStyleBot, BORDER_TOP, BORDER_STYLE_MEDIUM, RGB(16,100,200))
>
>*-*	Add sheet
>	lnSh = loExcel.AddSheet(lnWB, "Report")
>
>*-*	Write the header row cells
>	lnXLSXRow = 1
>	lnColCnt  = .GetNumberColumns()
>	FOR lnCol=0 TO lnColCnt-1
>		loExcel.SetCellValue(lnWB, lnSh, lnXLSXRow, lnCol+1, .GetCellText(lnCol, -1))
>	ENDFOR
>*-*	Assign header row cell formatting	
>	loExcel.SetCellStyleRange(lnWB, lnSh, 1, 1, lnXLSXRow, lnColCnt, lnStyleHdr)
>*-*	Write the cell values
>	FOR lnRow=0 TO .GetNumberRows()-2
>		lnXLSXRow = lnXLSXRow + 1
>		loExcel.SetCellValue(lnWB, lnSh, lnXLSXRow, 1, .GetCellText(0, lnRow))
>		loExcel.SetCellValue(lnWB, lnSh, lnXLSXRow, 2, .GetCellText(1, lnRow))
>		loExcel.SetCellValue(lnWB, lnSh, lnXLSXRow, 3, .GetCellValue(2, lnRow))
>		loExcel.SetCellValue(lnWB, lnSh, lnXLSXRow, 4, .GetCellValue(3, lnRow))
>	ENDFOR
>*-*	Assign cell formatting	
>	loExcel.SetCellStyleRange(lnWB, lnSh, 2, 1, lnXLSXRow, 1, lnStyleMId)
>	loExcel.SetCellStyleRange(lnWB, lnSh, 2, 2, lnXLSXRow, 2, lnStyleTxt)
>	loExcel.SetCellStyleRange(lnWB, lnSh, 2, 3, lnXLSXRow, 4, lnStyleAmt)
>
>	lnXLSXRow = lnXLSXRow + 1
>	loExcel.SetCellFormula(lnWB, lnSh, lnXLSXRow, 3, "=SUM(C2:C" + TRANSFORM(lnXLSXRow-1) + ")")
>	loExcel.SetCellFormula(lnWB, lnSh, lnXLSXRow, 4, "=SUM(D2:D" + TRANSFORM(lnXLSXRow-1) + ")")
>	loExcel.SetCellStyleRange(lnWB, lnSh, lnXLSXRow, 1, lnXLSXRow, 4, lnStyleBot)
>*-*	Set column widths
>	loExcel.SetColumnWidth(lnWB, lnSh, 1, 10)
>	loExcel.SetColumnWidth(lnWB, lnSh, 2, 45)
>	loExcel.SetColumnWidth(lnWB, lnSh, 3, 20)
>	loExcel.SetColumnWidth(lnWB, lnSh, 4, 20)
>
>*-*	Freeze top row and save workbook
>	loExcel.FreezePanes(lnWB, lnSh, 1, 0)
>	loExcel.SaveWorkbook(lnWB)
>ENDIF
>
Very handy examples - thanks!
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Previous
Reply
Map
View

Click here to load this message in the networking platform