DBGridEh组件事件基本与DBGrid相同,在此不再赘述。
多表头的DBGridEH导出到Excel中
把自己的解决法子说一下,虽然解决的不是很完美,缺少单元格合并和字体设置。
我重写了单元DBGridEhImpExp中的函数TDBGridEhExportAsXLS.WriteTitle
希望有高手继续完善这个函数,让之最终导出的Excel格式和Elib一样。
//------------------------------------------------------------------
procedure TDBGridEhExportAsXLS.WriteTitle(ColumnsList: TColumnsEhList);
var
i, k: Integer;
FPTitleExpArr: TTitleExpArr;
ListOfHeadTreeNodeList: TList;
ColSpan, RowSpan: Integer;
str1:String;
L: Word; //writestringcell
begin
if ColumnsList.Count = 0 then
Exit;
if DBGridEh.UseMultiTitle then
begin
try
CreateMultiTitleMatrix(DBGridEh, ColumnsList, FPTitleExpArr, ListOfHeadTreeNodeList);
//输出除最后一行表头的内容
for k := ListOfHeadTreeNodeList.Count - 1 downto 1 do
begin
for i := 0 to ColumnsList.Count - 1 do
begin
if THeadTreeNode(TList(ListOfHeadTreeNodeList.Items[k]).Items) <> nil then
begin
CalcSpan(ColumnsList, ListOfHeadTreeNodeList, k, i, ColSpan, RowSpan);
str1:=THeadTreeNode(TList(ListOfHeadTreeNodeList.Items[k]).Items).Text;
//=====================================
L := Length(str1);
CXlsLabel[1] := 8 + L;
CXlsLabel[2] := FRow;
CXlsLabel[3] := FCol;
CXlsLabel[5] := L;
StreamWriteWordArray(Stream, CXlsLabel);
StreamWriteAnsiString(Stream, str1);
if FCol = ExpCols.Count - 1 then
begin
Inc(FRow);
FCol := 0;
end
else
FCol:=FCol+ColSpan;
//=====================================
end;
end;
end;
FRow := ListOfHeadTreeNodeList.Count - 1;
FCol :=0;
//输出最后一行表头
for i := 0 to ColumnsList.Count - 1 do
begin
if THeadTreeNode(TList(ListOfHeadTreeNodeList.Items[0]).Items) <> nil then
begin
CalcSpan(ColumnsList, ListOfHeadTreeNodeList, 0, i, ColSpan, RowSpan);
str1:=THeadTreeNode(TList(ListOfHeadTreeNodeList.Items[0]).Items).Text;
//WriteStringCell(str1);
//==================================
L := Length(str1);
CXlsLabel[1] := 8 + L;
CXlsLabel[2] := FRow;
CXlsLabel[3] := FCol;
CXlsLabel[5] := L*ColSpan;
StreamWriteWordArray(Stream, CXlsLabel);
StreamWriteAnsiString(Stream, str1);
if FCol = ExpCols.Count - 1 then
begin
Inc(FRow);
FCol := 0;
end
else
FCol:=FCol+ColSpan;
//==================================
end
else
begin
Inc(FCol);//单云格进一
end;
end;
FRow:=ListOfHeadTreeNodeList.Count;
FCol:=0;
finally
for i := 0 to ListOfHeadTreeNodeList.Count - 1 do
TList(ListOfHeadTreeNodeList.Items).Free;
ListOfHeadTreeNodeList.Free;
end;
end
else //不使用多表头
begin
for i := 0 to ColumnsList.Count - 1 do
begin
WriteStringCell(ColumnsList.Title.Caption);
end;
end;
end;
//------------------------------------------------------------------
显示数据自动滚动
procedure TForm1.Timer1Timer(Sender: TObject);
var
m:tmessage;
begin
m.Msg:=WM_VSCROLL;
m.WParamLo:=SB_LINEDOWN;
m.WParamHi:=1 ;
m.LParam:=0;
postmessage(self.DBGrid1.Handle,m.Msg,m.WParam,m.LParam);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
self.Timer1.Enabled:=true;
end;
如果需要让他自动不断地从头到尾滚动,添加如下代码
if table1.Eof then table1.First;
一、打印dbgrid数据:
//==============================================================================
//打印DBGrid中的所有数据********************************************************
//==============================================================================
procedure DBGridPrint(DBGrid: TDBGrid; Title: string);
var PrintDialog: TPrintDialog;
RowHeight, Temp_X, Temp_Y, PageEdgeX, PageEdgeY, PixelsPerInchX, PixelsPerInchY: integer;
TempStr: string;
Scale: Double;
Rect: TRect;
//==============================================================================
//1.输出标题********************************************************************
//==============================================================================
procedure Print_Title;
begin
Rect := Bounds(0,0,Printer.PageWidth,PageEdgeY);
Printer.Canvas.Font.Name := '楷体_GB2312';
Printer.Canvas.Font.Style := Printer.Canvas.Font.Style + [fsBold];
Printer.Canvas.Font.Size := 20;
DrawText(Printer.Canvas.Handle,PChar(Title),Length(Title),Rect,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
Printer.Canvas.Rectangle(PageEdgeX, PageEdgeY, Printer.PageWidth - PageEdgeX, PageEdgeY + 1);
end;
//==============================================================================
//2.输出列头********************************************************************
//==============================================================================
procedure Print_Column;
var j: integer;
begin
Printer.Canvas.Font.Name := '黑体';
Printer.Canvas.Font.Size := 9;
Temp_X := PageEdgeX;
Temp_Y := PageEdgeY;
//========================================================================
for j:=1 to DBGrid.Columns.Count do
begin
if not DBGrid.Columns[j-1].Visible then Continue;
//======================================================================
TempStr := DBGrid.Columns[j-1].Title.Caption;
Rect := Bounds(Temp_X, Temp_Y, Trunc(DBGrid.Columns[j-1].Width*Scale), RowHeight);
case DBGrid.Columns[j-1].Field.Alignment of
//====================================================================
//case.1.居中*********************************************************
//====================================================================
taCenter: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
//====================================================================
//case.2.居左*********************************************************
//====================================================================
taLeftJustify: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
//====================================================================
//case.3.居右*********************************************************
//====================================================================
taRightJustify: if Rect.Right-Rect.Left>=Printer.Canvas.TextWidth(TempStr)
then DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_RIGHT or DT_VCENTER or DT_SINGLELINE)
else DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
end;
Temp_X := Temp_X + Trunc(DBGrid.Columns[j-1].Width*Scale);
end;
Temp_Y := Temp_Y + RowHeight;
end;
//==============================================================================
//3.输出DBGrid内容**************************************************************
//==============================================================================
procedure Print_Cells;
var j: integer;
begin
Printer.Canvas.Font.Name := '宋体';
Printer.Canvas.Font.Style := Printer.Canvas.Font.Style - [fsBold];
while Temp_Y<Printer.PageHeight-PageEdgeY do
begin
Temp_X := PageEdgeX;
for j:=1 to DBGrid.Columns.Count do
begin
if not DBGrid.Columns[j-1].Visible then Continue;
//====================================================================
Rect := Bounds(Temp_X, Temp_Y, Trunc(DBGrid.Columns[j-1].Width*Scale), RowHeight);
if (DBGrid.Columns[j-1].Field is TCurrencyField)
or (DBGrid.Columns[j-1].Field is TLargeIntField)
or (DBGrid.Columns[j-1].Field is TSmallIntField)
or (DBGrid.Columns[j-1].Field is TIntegerField)
or (DBGrid.Columns[j-1].Field is TFloatField)
or (DBGrid.Columns[j-1].Field is TWordField)
then TempStr := FormatFloat(',##0.00',DBGrid.Columns[j-1].Field.AsFloat)
else TempStr := DBGrid.Columns[j-1].Field.AsString;
//====================================================================
case DBGrid.Columns[j-1].Field.Alignment of
//==================================================================
//case.1.居中*******************************************************
//==================================================================
taCenter: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
//==================================================================
//case.2.居左*******************************************************
//==================================================================
taLeftJustify: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
//==================================================================
//case.3.居右*******************************************************
//==================================================================
taRightJustify: DrawText(Printer.Canvas.Handle,PChar(TempStr),Length(TempStr),Rect,DT_RIGHT or DT_VCENTER or DT_SINGLELINE);
end;
Temp_X := Temp_X + Trunc(DBGrid.Columns[j-1].Width*Scale);
end;
Temp_Y := Temp_Y + RowHeight;
DBGrid.DataSource.DataSet.Next;
if DBGrid.DataSource.DataSet.Eof then Exit;
end;
end;
//==============================================================================
//4.输出页脚********************************************************************
//==============================================================================
procedure Print_Footer;
begin
Temp_Y := Printer.PageHeight - PageEdgeY + RowHeight;
//========================================================================
//4.0.输出横线************************************************************
//========================================================================
Printer.Canvas.Rectangle(PageEdgeX, Temp_Y, Printer.PageWidth - PageEdgeX, Temp_Y + 1);
//========================================================================
//4.1.输出日期************************************************************
//========================================================================
Rect := Bounds(PageEdgeX, Temp_Y, Printer.PageWidth-PageEdgeX, RowHeight);
DrawText(Printer.Canvas.Handle,PChar(DateTimeToStr(Now)),Length(DateTimeToStr(Now)),Rect,DT_LEFT or DT_VCENTER or DT_SINGLELINE);
//========================================================================
//4.2.输出页号************************************************************
//========================================================================
Rect := Bounds(PageEdgeX, Temp_Y, Printer.PageWidth-PageEdgeX*2, RowHeight);
DrawText(Printer.Canvas.Handle,PChar('#'+IntToStr(Printer.PageNumber)),Length('#'+IntToStr(Printer.PageNumber)),Rect,DT_RIGHT or DT_VCENTER or DT_SINGLELINE);
end;
//==============================================================================
begin
PrintDialog := TPrintDialog.Create(DBGrid);
if PrintDialog.Execute then
begin
//==========================================================================
//0.取当前打印机X,Y方向每英寸像素*******************************************
//==========================================================================
PixelsPerInchX := GetDeviceCaps(Printer.Handle, LOGPIXELSX);
PixelsPerInchY := GetDeviceCaps(Printer.Handle, LOGPIXELSY);
//==========================================================================
//1.变量初始化**************************************************************
//==========================================================================
PageEdgeX := PixelsPerInchX div 6;
PageEdgeY := PixelsPerInchY div 5 * 4;
RowHeight := Trunc(1.5 * 9 * PixelsPerInchY / 72);
Scale := PixelsPerInchX / Screen.PixelsPerInch;
//==========================================================================
try
Printer.BeginDoc;
DBGrid.DataSource.DataSet.First;
while not DBGrid.DataSource.DataSet.Eof do
begin
Print_Title;
Print_Column;
Print_Cells;
Print_Footer;
Printer.NewPage;
end;
//========================================================================
if not Printer.Aborted then Printer.EndDoc;
except
on E:EPrinter do
begin
MessageBox(Application.Handle,PChar('打印机没有准备好!'),'提示!',MB_OK+MB_ICONINFORMATION);
Printer.Abort;
Exit;
end;
on E:Exception do
begin
raise;
Exit;
end;
end;
end;
//============================================================================
PrintDialog.Free;
end;
二、dbgrideh标题排序:
首先把需要排序的title.titlebutton:=true
ehlib的optioneh中autosortmarking最好设为true
titelbutton事件中写:
var
sortstring:string;
begin
//进行排序
with Column do
begin
if FieldName = '' then Exit;
case Title.SortMarker of
smNoneEh:
begin
Title.SortMarker := smDownEh;
sortstring := Column.FieldName + ' ASC';
end;
smDownEh:sortstring:= Column.FieldName + ' ASC';
smUpEh:sortstring:=Column.FieldName + ' DESC';
end;
//数据集排序。
try
dataset.Sort:=sortstring //dataset为实际数据集变量名
except
end;