下面代码是我的测试,是可以达到你的目的:取得每个格子的颜色 private { Private declarations } Fcolor : TColor; public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.DBGrid1ColEnter(Sender: TObject); begin Fcolor := TInplaceEdit(Sender).Brush.Color;//取得颜色 end;procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); begin if Field.FieldName='NAME' then begin TInplaceEdit(Sender).Brush.Color := clRed;//记录颜色 DBGrid1.Canvas.Brush.Color := clRed; DBGrid1.Canvas.FillRect(Rect); end; end;procedure TForm1.Button1Click(Sender: TObject);//执行导出 begin Table1.First; dbgrid1.selectedindex:=-1; while not table1.eof do begin DBGrid1.SelectedIndex := 0;//触发colEnter Label1.Font.Color := Fcolor;//这里你可以指定execl格子的颜色 next; end;; end;以上代码需要你自己再修改,应该可满足你的要求
要注意一点dbgrid的options中的alwaysSHowEditor若为false,那么在导出时procedure TForm1.Button1Click(Sender: TObject);//执行导出 begin Table1.First; 设定alwaysSHowEditor为true dbgrid1.selectedindex:=-1; while not table1.eof do begin DBGrid1.SelectedIndex := 0;//触发colEnter Label1.Font.Color := Fcolor;//这里你可以指定execl格子的颜色 next; end; 设定alwaysSHowEditor为false end;
对应的话,要根据你的代码了,看你是如何将数据导出到excel的。 我猜你是遍历dataset,可以这样,仅供参考,具体如何,自己再修改咯procedure TForm1.Button1Click(Sender: TObject);//执行导出 var i: integer; begin Table1.First; 设定alwaysSHowEditor为true dbgrid1.selectedindex:=-1; while not table1.eof do begin for i:=0 to table1.Fieldcount-1 do begin DBGrid1.SelectedIndex := i;//触发colEnter,取得颜色 指定的excel.cell := Fields[i].value; 指定的excel.cell color := FColor;//这里你可以指定excel格子的颜色 end; next; end; 设定alwaysSHowEditor为false end;
二,类似的,我曾想过在DrawColumnCell事件中,通过二维数组模式,在单元格填充颜色的时候就将颜色赋值给数组,然后再再转出excel时,将对应二维数组的值赋予到excel对应cell中,但是这个在画dbgrid的时候,貌似无限循环了。
DataCol: Integer; Column: TColumnEh; State: TGridDrawState);
if column.Field.AsInteger>=1 then
begin
dbgrideh4.Canvas.Brush.Color:=cllime;
dbgrideh4.Canvas.Font.Color:=cllime;
dbgrideh4.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
在DrawColumnCell是无限,但问题他只是在表内数据发生变化后,进行重画.(实质上在同一时间,你就是手工对应一次表格颜色.)那么在EXCEL导出的时候,你就从头遍列一边,然后把导出这个时间(意思就是数据不变)的表的颜色给EXCEL画上去.
能理解吗?我的第二个方法,呵呵,是叫你.在每一次导EXCEL的时候,(噢,就是数据内容不变的时候),把数据每个格剪一个图片,然后把图片中
不会写到字的点,取出这个点的色彩,转成颜色.(DBGRID好像比较难得到这个格,呵呵,我可能想错了,这第二个方法,在DBGRID中好像
比第一种还难,在DBGRID中CEIL值好像让DBGRID给私有了.在STRINGGRID中就能得到每个格的RECT.)第一种方法,我想应该能实现.
{ Private declarations }
Fcolor : TColor;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.DBGrid1ColEnter(Sender: TObject);
begin
Fcolor := TInplaceEdit(Sender).Brush.Color;//取得颜色
end;procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if Field.FieldName='NAME' then
begin
TInplaceEdit(Sender).Brush.Color := clRed;//记录颜色
DBGrid1.Canvas.Brush.Color := clRed;
DBGrid1.Canvas.FillRect(Rect);
end;
end;procedure TForm1.Button1Click(Sender: TObject);//执行导出
begin
Table1.First;
dbgrid1.selectedindex:=-1;
while not table1.eof do
begin
DBGrid1.SelectedIndex := 0;//触发colEnter
Label1.Font.Color := Fcolor;//这里你可以指定execl格子的颜色
next;
end;;
end;以上代码需要你自己再修改,应该可满足你的要求
begin
Table1.First;
设定alwaysSHowEditor为true
dbgrid1.selectedindex:=-1;
while not table1.eof do
begin
DBGrid1.SelectedIndex := 0;//触发colEnter
Label1.Font.Color := Fcolor;//这里你可以指定execl格子的颜色
next;
end;
设定alwaysSHowEditor为false
end;
小弟菜鸟,大侠见笑了。
我猜你是遍历dataset,可以这样,仅供参考,具体如何,自己再修改咯procedure TForm1.Button1Click(Sender: TObject);//执行导出
var
i: integer;
begin
Table1.First;
设定alwaysSHowEditor为true
dbgrid1.selectedindex:=-1;
while not table1.eof do
begin
for i:=0 to table1.Fieldcount-1 do
begin
DBGrid1.SelectedIndex := i;//触发colEnter,取得颜色
指定的excel.cell := Fields[i].value;
指定的excel.cell color := FColor;//这里你可以指定excel格子的颜色
end;
next;
end;
设定alwaysSHowEditor为false
end;