Thank you very much... I'll try this.
----- Original Message ----
From: "Wilson, Stephen" <[EMAIL PROTECTED]>
To: [email protected]
Sent: Wednesday, November 28, 2007 6:02:37 PM
Subject: RE: [delphi-en] Zeos Dataset to excel: Field names missing
You need to include a loop before your for I loop to write the field names to
the Data VarArray:
for C:= 0 to DS.Fieldcount- 1 do Data[0,C]:= DS.Fields[C] .FieldName;
The I in your for I / for J loop will then have to be +1:
for J := 0 to DS.FieldCount - 1 do
Data[I + 1, J] := DS.Fields[J] .Value;
DS.Next;
Alternatively you could create a second, one-dimensional, VarArray with bounds
of [0, DS.FieldCount- 1]. In this case when you come to write the Headers,
TopLeftCell will be ExcelApp.Range[ 'A1'] and for the Data TopLeftCell will
have to be ExcelApp.Range[ 'B1'];
Hopefully one or other will work.
Regards
Steve
From: [EMAIL PROTECTED] ps.com [mailto:[EMAIL PROTECTED] ps.com] On Behalf
Of ron_tabada
Sent: 27 November 2007 04:50
To: [EMAIL PROTECTED] ps.com
Subject: [delphi-en] Zeos Dataset to excel: Field names missing
procedure DataSetToExcel( DS: TDataSet; const Filename: string);
var
ExcelApp,
WorkBook,
WorkSheet,
TopLeftCell: Variant;
Data: Variant;
Finger: string;
I, J: Integer;
begin
// source:
http://www.elists. org/pipermail/ delphi-talk/ 2005-February/ 020132.html
//No operation for an empty dataset
if DS.Bof and DS.Eof then Exit;
//Create a variant array with all the specified data
Data := VarArrayCreate( [0, DS.RecordCount - 1, 0, DS.FieldCount - 1],
varVariant);
DS.DisableControls;
try
Finger := DS.Bookmark;
try
DS.First;
for I := 0 to DS.RecordCount - 1 do
begin
for J := 0 to DS.FieldCount - 1 do
Data[I, J] := DS.Fields[J] .Value;
DS.Next;
end;
finally
DS.Bookmark := Finger;
end;
finally
DS.EnableControls;
end;
ExcelApp := CreateOleObject( 'Excel.Applicati on');
WorkBook := ExcelApp.WorkBooks. Add;
WorkSheet := WorkBook.WorkSheets .Add;
//Put the data in the rectangle starting at cell "A1"
TopLeftCell := ExcelApp.Range[ 'A1'];
ExcelApp.Range[ TopLeftCell,
TopLeftCell. Offset[DS. RecordCount - 1, DS.FieldCount - 1]].Value :=
Data;
//Save the workbook
Workbook.SaveAs( FileName) ;
//Quit Excel (leave this one away to keep Excel on screen)
ExcelApp.Quit;
end;
************ ********* ********* ********* ********* ********* *********
*********
This e-mail and any files transmitted with it are confidential. If you are not
the intended recipient, any reading, printing, storage, disclosure, copying or
any other action taken in respect of this e-mail is prohibited and may be
unlawful. If you are not the intended recipient, please notify the sender
immediately by using the reply function and then permanently delete what you
have received.
Content of emails received by this Trust will be subject to disclosure under
the Freedom of Information Act 2000, subject to the specified exemptions,
including the Data Protection Act 1998 and Caldicott Guardian principles.
This footnote also confirms that, unless otherwise stated, this email message
has been swept by Sophos Anti-virus for the presence of computer viruses.
************ ********* ********* ********* ********* ********* *********
*********
<!--
#ygrp-mkp{
border:1px solid #d8d8d8;font-family:Arial;margin:14px 0px;padding:0px 14px;}
#ygrp-mkp hr{
border:1px solid #d8d8d8;}
#ygrp-mkp #hd{
color:#628c2a;font-size:85%;font-weight:bold;line-height:122%;margin:10px 0px;}
#ygrp-mkp #ads{
margin-bottom:10px;}
#ygrp-mkp .ad{
padding:0 0;}
#ygrp-mkp .ad a{
color:#0000ff;text-decoration:none;}
-->
<!--
#ygrp-sponsor #ygrp-lc{
font-family:Arial;}
#ygrp-sponsor #ygrp-lc #hd{
margin:10px 0px;font-weight:bold;font-size:78%;line-height:122%;}
#ygrp-sponsor #ygrp-lc .ad{
margin-bottom:10px;padding:0 0;}
-->
<!--
#ygrp-mlmsg {font-size:13px;font-family:arial, helvetica, clean, sans-serif;}
#ygrp-mlmsg table {font-size:inherit;font:100%;}
#ygrp-mlmsg select, input, textarea {font:99% arial, helvetica, clean,
sans-serif;}
#ygrp-mlmsg pre, code {font:115% monospace;}
#ygrp-mlmsg * {line-height:1.22em;}
#ygrp-text{
font-family:Georgia;
}
#ygrp-text p{
margin:0 0 1em 0;}
#ygrp-tpmsgs{
font-family:Arial;
clear:both;}
#ygrp-vitnav{
padding-top:10px;font-family:Verdana;font-size:77%;margin:0;}
#ygrp-vitnav a{
padding:0 1px;}
#ygrp-actbar{
clear:both;margin:25px 0;white-space:nowrap;color:#666;text-align:right;}
#ygrp-actbar .left{
float:left;white-space:nowrap;}
.bld{font-weight:bold;}
#ygrp-grft{
font-family:Verdana;font-size:77%;padding:15px 0;}
#ygrp-ft{
font-family:verdana;font-size:77%;border-top:1px solid #666;
padding:5px 0;
}
#ygrp-mlmsg #logo{
padding-bottom:10px;}
#ygrp-vital{
background-color:#e0ecee;margin-bottom:20px;padding:2px 0 8px 8px;}
#ygrp-vital #vithd{
font-size:77%;font-family:Verdana;font-weight:bold;color:#333;text-transform:uppercase;}
#ygrp-vital ul{
padding:0;margin:2px 0;}
#ygrp-vital ul li{
list-style-type:none;clear:both;border:1px solid #e0ecee;
}
#ygrp-vital ul li .ct{
font-weight:bold;color:#ff7900;float:right;width:2em;text-align:right;padding-right:.5em;}
#ygrp-vital ul li .cat{
font-weight:bold;}
#ygrp-vital a{
text-decoration:none;}
#ygrp-vital a:hover{
text-decoration:underline;}
#ygrp-sponsor #hd{
color:#999;font-size:77%;}
#ygrp-sponsor #ov{
padding:6px 13px;background-color:#e0ecee;margin-bottom:20px;}
#ygrp-sponsor #ov ul{
padding:0 0 0 8px;margin:0;}
#ygrp-sponsor #ov li{
list-style-type:square;padding:6px 0;font-size:77%;}
#ygrp-sponsor #ov li a{
text-decoration:none;font-size:130%;}
#ygrp-sponsor #nc{
background-color:#eee;margin-bottom:20px;padding:0 8px;}
#ygrp-sponsor .ad{
padding:8px 0;}
#ygrp-sponsor .ad #hd1{
font-family:Arial;font-weight:bold;color:#628c2a;font-size:100%;line-height:122%;}
#ygrp-sponsor .ad a{
text-decoration:none;}
#ygrp-sponsor .ad a:hover{
text-decoration:underline;}
#ygrp-sponsor .ad p{
margin:0;}
o{font-size:0;}
.MsoNormal{
margin:0 0 0 0;}
#ygrp-text tt{
font-size:120%;}
blockquote{margin:0 0 0 4px;}
.replbq{margin:4;}
-->
Send instant messages to your online friends http://uk.messenger.yahoo.com
[Non-text portions of this message have been removed]