INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Usefull Functions & Procedures

Export a Formatted Table to Excel using HTML by baltman
Posted: 12 Jan 04 (Edited 12 Apr 04)

Export a Table to Excel with Formatting using HTML

Supports Numeric, Date and Character fields, formats number fields wider than 4 with commas and auto decimals based on data table. Just replace "MyTable" with yours.

You can define your own data formats too. To get the definition(s), save a pre-formatted Excel file as HTML and open with notepad and 'borrow' the definition.

Brian

CODE

table_2_excel_with_html([MyTable])

PROCEDURE table_2_excel_with_html
LPARAMETERS lcTargetTable

IF USED(lcTargetTable)=.f.
USE (lcTargetTable) IN 0
ENDIF

SELECT (lcTargetTable)

lcDir=JUSTPATH(FULLPATH(lcTargetTable))
lcTextMergeFile=JUSTSTEM(lcTargetTable)+[.html]
lcTextMergeFileFull=lcDir + [\] + lcTextMergeFile

SET CONSOLE OFF
SET TEXTMERGE ON TO [&lcTextMergeFile] NOSHOW

\<HTML>
\<HEAD>
\<META http-EQUIV='Content-Type' CONTENT='text/html; charset=windows-1252'>
\<META NAME='Generator' CONTENT='VFP'>
\<TITLE>a</TITLE>
\</HEAD>
\<BODY>
\<TABLE CELLSPACING=0 BORDER=0 CELLPADDING=3 WIDTH=264>
\<TR><TD WIDTH='100%' VALIGN='TOP'>
\    <style>
\<!--table

&&Characters
\.xlChr
\ {mso-style-parent:style0;
\ font-family:Arial, sans-serif;
\ mso-font-charset:0;
\ text-align:right;
\ white-space:normal;}

&& number no dec
\.xlN0
\ {mso-style-parent:style0;
\ mso-number-format:0;
\ white-space:normal;}

&& number 1 dec
\.xlN1
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>0\.0<<CHR(34)>>;
\ white-space:normal;}

&& number 2 dec
\.xlN2
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>0\.00<<CHR(34)>>;
\ white-space:normal;}

&& number 3 dec
\.xlN3
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>0\.000<<CHR(34)>>;
\ white-space:normal;}

&& number 4 dec
\.xlN4
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>0\.0000<<CHR(34)>>;
\ white-space:normal;}


&& number 5 dec
\.xlN5
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>0\.00000<<CHR(34)>>;
\ white-space:normal;}

&& number 6 dec
\.xlN6
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>0\.000000<<CHR(34)>>;
\ white-space:normal;}

&& number 7 dec
\.xlN7
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>0\.0000000<<CHR(34)>>;
\ white-space:normal;}

&& number 8 dec
\.xlN8
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>0\.00000000<<CHR(34)>>;
\ white-space:normal;}

&& number 9 dec
\.xlN9
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>0\.000000000<<CHR(34)>>;
\ white-space:normal;}

&& number 0 dec and comma
\.xlNC0
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\#\,\#\#0<<CHR(34)>>;
\ white-space:normal;}

&& number 1 dec and comma
\.xlNC1
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\#\,\#\#0\.0<<CHR(34)>>;
\ white-space:normal;}

&& number 2 dec and comma
\.xlNC2
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\#\,\#\#0\.00<<CHR(34)>>;
\ white-space:normal;}

&& number 3 dec and comma
\.xlNC3
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\#\,\#\#0\.000<<CHR(34)>>;
\ white-space:normal;}

&& number 4 dec and comma
\.xlNC4
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\#\,\#\#0\.0000<<CHR(34)>>;
\ white-space:normal;}

&& number 5 dec and comma
\.xlNC5
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\#\,\#\#0\.00000<<CHR(34)>>;
\ white-space:normal;}

&& number 6 dec and comma
\.xlNC6
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\#\,\#\#0\.000000<<CHR(34)>>;
\ white-space:normal;}

&& number 7 dec and comma
\.xlNC7
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\#\,\#\#0\.0000000<<CHR(34)>>;
\ white-space:normal;}

&& number 8 dec and comma
\.xlNC8
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\#\,\#\#0\.00000000<<CHR(34)>>;
\ white-space:normal;}

&& number 9 dec and comma
\.xlNC9
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\#\,\#\#0\.000000000<<CHR(34)>>;
\ white-space:normal;}

&& currency 0 dec
\.xlC0
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\0022$\0022\#\,\#\#0<<CHR(34)>>;}

&& currency 1 dec
\.xlC1
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\0022$\0022\#\,\#\#0\.0<<CHR(34)>>;}

&& currency 2 dec
\.xlC2
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\0022$\0022\#\,\#\#0\.00<<CHR(34)>>;}

&&.xlC3 is currency 3 dec
\.xlC3
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\0022$\0022\#\,\#\#0\.000<<CHR(34)>>;}

&&.xlC4 is currency 4 dec
\.xlC4
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>\0022$\0022\#\,\#\#0\.0000<<CHR(34)>>;}

\.xlDate
\ {mso-style-parent:style0;
\ mso-number-format:<<CHR(34)>>mm\/dd\/yyyy<<CHR(34)>>;}
\ -->
\ </style>
\\<BODY><TABLE>
\<tr>

FOR lnField = 1 to AFIELDS(laFields) &&headers
 \<td class=xlChr><<laFields(m.lnField,1)>><\td>
ENDFOR
\</tr>

SCAN
  FOR lnField = 1 to AFIELDS(laFields)
 
 DO CASE &&Add Formats
  CASE (laFields(m.lnField,2)=[N] OR laFields(m.lnField,2)=[I] OR ;
   laFields(m.lnField,2)=[B]) AND laFields(m.lnField,3)<=3&&Number/Decimal
   \<td class=xlN<<laFields(m.lnField,4)>>>
  
  CASE (laFields(m.lnField,2)=[N] OR laFields(m.lnField,2)=[I] OR ;
   laFields(m.lnField,2)=[B]) AND laFields(m.lnField,3)>3&&Commas/Decimal
   \<td class=xlNC<<laFields(m.lnField,4)>>>
     
  CASE laFields(m.lnField,2)=[D] OR laFields(m.lnField,2)=[T]&&Date
   \<td class=xlDate>

  CASE laFields(m.lnField,2)=[Y] &&Currency (US)
   \<td class=xlC2>

  OTHERWISE &&Character
   \<td class=xlChr>

 ENDCASE
  &&STRTRAN allows html tags to work
      \\<<RTRIM(TRANSFORM(EVALUATE(FIELD(m.lnField))))>><\td>
   
  ENDFOR
\</tr>
ENDSCAN
\</table></body></html>
SET TEXTMERGE OFF
SET TEXTMERGE TO
SET CONSOLE ON

&&Open HTML w/excel
lcOldError = On([ERROR])
On Error loExcel = .Null.
loExcel = Createobject([Excel.Application])
On Error &lcOldError

With loExcel
.WorkBooks.Open(lcTextMergeFileFull)
.ActiveWindow.DisplayGridlines = .t.
.visible=.t.
ENDWITH
ENDPROC

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close