DisplayMaskExcel = '' do case case inlist(m.Fieldtype, T_CHARACTER) case inlist(m.Fieldtype, T_NUMERIC, T_DOUBLE) DisplayMaskExcel = '#,###.00' + '_);[Red](' + '#,###.00' + ')' case inlist(m.Fieldtype, T_DATE) DisplayMaskExcel = 'dd/mm/yyyy' case inlist(m.Fieldtype, T_DATETIME) DisplayMaskExcel = 'dd/mm/yyyy hh:mm:ss' case inlist(m.Fieldtype, T_MEMO) case inlist(m.Fieldtype, T_LOGICAL) case inlist(m.Fieldtype, T_CURRENCY) DisplayMaskExcel = '#,###.00' + '_);[Red](' + '#,###.00' + ')' case inlist(m.Fieldtype, 'I') DisplayMaskExcel = '#,###' + '_);[Red](' + '#,###' + ')' endcase(2) I then go through the columns and set the column properties
nRow = 1 for i=1 to ncols xlSheet.Cells[nRow,i].Value = ['] + h[i,2] && this is the field caption, eg 'Name if( !empty(h[i,5]) ) xlsheet.Columns(i).NumberFormat = h[i,5] && ie DisplayMaskExcel from above endif if( !empty(h[i,6]) ) && if you have a char type you want to align right xlsheet.Columns(i).HorizontalAlignment = h[i,6] && -4152 && 'xlRight' endif endfor xlSheet.PageSetup.PrintTitleRows = "$1:$1"(3) to fill the sheet
=afields(af) scan all while !m.Escaped nRow = m.nRow + 1 for i = 1 to m.ncols x = eval(af(i, 1)) =VariableToExcel(m.x, af[i, 2], m.xlSheet.Cells[m.nRow,m.i]) && below endfor endscan(4) Finally, you may want to autofit the columns
for i = 1 to ncols xlSheet.Columns(i).EntireColumn.AutoFit endfor
&& VariableToExcel function VariableToExcel(Content, FieldType, CellReference) local Success Success = TRUE local x, FormatMask FieldType = iif(empty(m.FieldType), vartype(m.Content), m.FieldType) x = null do case case isnull(m.Content) case inlist(m.FieldType, T_CHARACTER, T_MEMO) x = ['] + rtrim(m.Content) case inlist(m.FieldType, T_DATE) x = iif(empty(m.Content), '', '=date(' + transform(dtos(m.Content), '@R 9999,99,99') + ')') FormatMask = 'dd/mm/yyyy' case inlist(m.FieldType, T_DATETIME) x = ; iif(empty(m.Content), ; '', ; '=date(' + transform(dtos(m.Content), '@R 9999,99,99') + ')' ; + '+time(' + transform(right(ttoc(m.Content,1),6), '@R 99,99,99') + ')' ; ) FormatMask = 'dd/mm/yyyy hh:mm:ss' case inlist(m.FieldType, T_CURRENCY) x = m.Content FormatMask = '#,###.00' + '_);[Red](' + '#,###.00' + ')' case inlist(m.FieldType, 'I') x = m.Content FormatMask = '#,###' + '_);[Red](' + '#,###' + ')' case inlist(m.FieldType, T_NUMERIC, T_DOUBLE) x = m.Content FormatMask = '#,###.00' + '_);[Red](' + '#,###.00' + ')' case inlist(m.FieldType, T_LOGICAL) x = iif(m.Content, 'TRUE', 'FALSE') endcase do case case (vartype(m.CellReference) <> T_OBJECT) Success = FALSE case isnull(m.x) x = '' otherwise CellReference.Value = m.x if( !empty(m.FormatMask) ) CellReference.NumberFormat = m.FormatMask endif endcase return m.Success endfunc