///LastLineIsSeparate last line separate line
///LeftOffset, TopOffset is Excel table position offset
///Delphi2007
Procedure DBGridEhToExcelEx (DBGridEh: TDBGridEh; LastLineIsSeparate: Boolean;
LeftOffset: integer; TopOffset: integer);
Var
XLApp: variant;
Sheet: variant;
ARange: variant;
TitleList: TList;
MaxDepth: integer;
///the left is the index to the Title starting from 0
///the top is the Title. A List of the [x] index starting from 0
Procedure DrawSheetHead (Left, Top, Width: integer);
Var
I: integer;
STR: string;
TmpWidth: integer;
The Row, Col: integer;
Cell1, Cell2: variant;
The RightMost: integer;//at the far right column position starting from 0
The begin
The RightMost: Left + Width=1;
While the Left & lt;=RightMost do
The begin
If (TStringList (TitleList. The List [Left]), the Count & lt;=0)
Or (Top=TStringList (TitleList. The List [Left]), the Count) then
The begin
///for the final title. The caption formed a line separate situation
///at this time there may be some stringlist. Count=0
///or TitleList output last caption
TmpWidth:=1;
Row:=Top + 1 + TopOffset;
Col:=Left + 1 + LeftOffset;
Cell1:=XLApp. Cells. The Item [Row, Col];
Row:=Top + 1 + TopOffset + MaxDepth TStringList (TitleList. The List [Left]). The count.
Col:=Left + 1 + LeftOffset;
Cell2:=XLApp. Cells. The Item [row, col];
ARange:=Sheet. Range [Cell1, Cell2];
ARange. Merge;
ARange. Horizontalalignment:=xlCenter;
End
The else
The begin
If the Top & lt; TStringList (TitleList. The List [Left]). Then the Count - 1
The begin
TmpWidth:=1;
STR:=TStringList (TitleList. The List [Left]). The Strings (Top),
For I=1 to Width - 1 do
The begin
If ((Left + I) & gt; The RightMost)
Or (Top & gt;=TStringList (TitleList. The List [Left +] I). The Count) then
break;
If the STR=TStringList (TitleList. The List [Left +] I). The Strings [Top] then
Inc (tmpWidth)
The else
break;
end;
Row:=Top + 1 + TopOffset;
Col:=Left + 1 + LeftOffset;
Cell1:=XLApp. Cells. The Item [Row, Col];
Row:=Top + 1 + TopOffset;
Col:=Left + + tmpWidth LeftOffset;
Cell2:=XLApp. Cells. The Item [Row, Col];
ARange:=Sheet. Range [Cell1, Cell2];
ARange. Merge;
ARange. Value:=STR;
ARange. Horizontalalignment:=xlCenter;
DrawSheetHead (Left, Top + 1, tmpWidth);
End
Else if (Top=TStringList (TitleList. The List [Left]), the Count - 1) then//the last line
The begin
TmpWidth:=1;
STR:=TStringList (TitleList. The List [Left]). The Strings (Top),
For I=1 to Width - 1 do
The begin
If ((Left + I) & gt; The RightMost)
Or (Top & gt;=TStringList (TitleList. The List [Left +] I). The Count) then
break;
If the STR=TStringList (TitleList. The List [Left +] I). The Strings [Top] then
Inc (tmpWidth)
The else
break;
end;
Row:=Top + 1 + TopOffset;
Col:=Left + 1 + LeftOffset;
Cell1:=XLApp. Cells. The Item [Row, Col];
Row:=Top + 1 + TopOffset + MaxDepth TStringList (TitleList. The List [Left]). The count.
Col:=Left + + tmpWidth LeftOffset;
Cell2:=XLApp. Cells. The Item [row, col];
ARange:=Sheet. Range [Cell1, Cell2];
ARange. Merge;
ARange. Value:=STR;
ARange. Horizontalalignment:=xlCenter;
end;
end;
Left:=Left + tmpWidth;
end;
end;
The function CreateTitleList (DBGridEh: TDBGridEh) : TList;
Var
I, the position: integer;
STR: string;
Offset: integer;
The begin
Result:=TList. Create;
There must be other string before and after the//'|'
Result. The Count:=DBGridEh. VisibleColumns. Count;
For I:=0 to the result. Do the Count - 1
The begin
Result. The List [I] :=TStringList. Create;
STR:=DBGridEh VisibleColumns [I] Title. The Caption;
Offset:=1;
While true do
The begin
Position:=PosEx (' | ', STR, offset);
If the position (=0) then
The begin
If (offset=1) then
The begin
TStringList (result. The List [I]). The add (STR);
break;
End
The else//offset & gt; 1
The begin
TStringList (result. The List [I]). The Add (copy (STR, offset, length (STR) - offset + 1));
break;
end;
End
The else
The begin
TStringList (result. The List [I]). The Add (copy (STR, offset, the position - offset));
Offset:=position + 1;
end;
end;
end;
end;
Procedure ExportData (DBGridEh: TDBGridEh; LeftOffset: integer; TopOffset: integer);
Var
The DataSet: TDataSet;
RecordCount: integer;
The row, col: integer;
The begin
The DataSet:=DBGridEh DataSource. The DataSet;
The DataSet. DisableControls;
If LastLineIsSeparate then
MaxDepth:=MaxDepth + 1;
///data
RecordCount:=DataSet. RecordCount;
The DataSet. First;
For row:=0 to do RecordCount - 1
The begin
///ShowProgress (row + 1, RecordCount);
For col:=0 to DBGridEh. VisibleColumns. Do the Count - 1
The begin
Sheet. Cells [row + 1 + TopOffset + MaxDepth, Col + LeftOffset + 1]
:=DBGridEh VisibleColumns [col] DisplayText;
end;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull