我写了一个从DBGrid输出到Formula One的函数,也许对你有用,我用的是Formula One 6.1,有些属性Delphi自带的Formula One 3.0不支持,你至少得安装Formula One 5.0以上版本.你可以修改一下这个函数,直接传入DataSet做参数,我用DBGrid作参数传入是考虑到在F1Book上带出DBGrid的对齐格式、列宽等.函数如下://----------------------------------------------------------------------------- // 函数名:WriteDataFromDBGrid // 功能描述:将一个DBGrid的内容输出到F1Book // 入口参数:ADBGrid - 数据网格 // AStartRow - 数据起始行 // AStartCol - 数据起始列 // AF1Book – 接受数据的Formula one book // 示例:WriteDataFromDBGrid(DBGrid1, 2, 2, F1Book1); // 上面的例子是从F1Book1的2行2列起输出DBGrid1的内容 // 作者:JoyHero //----------------------------------------------------------------------------- procedure WriteDataFromDBGrid(ADBGrid: TDBGrid; AStartRow, AStartCol: Integer; AF1Book : TF1Book); var i,j:Integer; iColCount, iRowCount:Integer; cfTemp:F1CellFormat; iUnvisibleColCount: Integer; begin with AF1Book do begin //获得数据列数 iColCount:= ADBGrid.Columns.Count; iRowCount:= ADBGrid.DataSource.DataSet.RecordCount; iUnvisibleColCount:= 0; //填充标题列 for j:=0 to iColCount-1 do begin EntryRC[AStartRow,AStartCol+j]:= ADBGrid.Columns[j].Title.Caption; ColWidth[AStartCol+j]:= ADBGrid.Columns[j].Width*50; end; //填充数据 if ADBGrid.DataSource.DataSet.Active then begin with ADBGrid.DataSource.DataSet do begin First; i:= AStartRow+1; while not Eof do begin for j:= 0 to iColCount-1 do begin EntryRC[i,AStartCol+j]:= ADBGrid.Columns[j].Field.DisplayText; end; Next; Inc(i); end; end; end; //设置标题列居中显示,字体为粗体 SetSelection(AStartRow, AStartCol, AStartRow, AStartCol+iColCount-1); HAlign:=F1HAlignCenter; FontBold:=True; //按照DBGrid的对齐方式设置数据列的对齐方式 try for j:=0 to iColCount-1 do begin SetSelection(AStartRow+1, AStartCol+j, AStartRow+iRowCount, AStartCol+j); Case ADBGrid.Columns[j].Alignment of taLeftJustify: HAlign:=F1HAlignLeft; taRightJustify: HAlign:=F1HAlignRight; taCenter: HAlign:=F1HAlignCenter; end; end; except end; //删除DBGrid不显示的列 try for j:=0 to iColCount-1 do begin if not ADBGrid.Columns[j].Visible then begin SetSelection(AStartRow, AStartCol+j-iUnvisibleColCount, AStartRow+iRowCount, AStartCol+j-iUnvisibleColCount); EditDelete(F1ShiftHorizontal); iUnvisibleColCount:= iUnvisibleColCount+1; end; end; except end; //画边框 SetSelection(AStartRow, AStartCol, AStartRow+iRowCount, AStartCol+iColCount-1-iUnvisibleColCount); cfTemp:=GetCellFormat; cfTemp.BorderStyle[F1TopBorder]:=F1BorderThin; cfTemp.BorderStyle[F1LeftBorder]:=F1BorderThin; cfTemp.BorderStyle[F1BottomBorder]:=F1BorderThin; cfTemp.BorderStyle[F1RightBorder]:=F1BorderThin; cfTemp.BorderStyle[F1HInsideBorder]:=F1BorderThin; cfTemp.BorderStyle[F1VInsideBorder]:=F1BorderThin; SetCellFormat(cfTemp); end; end;
和excel差不多,和数据库不能直接相连
真是和Excel差不多,没一个cell显示什么都要写语句付值
看看帮助吧
下载一个formula one 6.1吧
delphi自带的不是这么好用,新的excel格式不支持。
看一下它的help不就以了很容易用的!
[email protected],谢谢了
// 函数名:WriteDataFromDBGrid
// 功能描述:将一个DBGrid的内容输出到F1Book
// 入口参数:ADBGrid - 数据网格
// AStartRow - 数据起始行
// AStartCol - 数据起始列
// AF1Book – 接受数据的Formula one book
// 示例:WriteDataFromDBGrid(DBGrid1, 2, 2, F1Book1);
// 上面的例子是从F1Book1的2行2列起输出DBGrid1的内容
// 作者:JoyHero
//-----------------------------------------------------------------------------
procedure WriteDataFromDBGrid(ADBGrid: TDBGrid;
AStartRow, AStartCol: Integer; AF1Book : TF1Book);
var
i,j:Integer;
iColCount, iRowCount:Integer;
cfTemp:F1CellFormat;
iUnvisibleColCount: Integer;
begin
with AF1Book do
begin
//获得数据列数
iColCount:= ADBGrid.Columns.Count;
iRowCount:= ADBGrid.DataSource.DataSet.RecordCount;
iUnvisibleColCount:= 0; //填充标题列
for j:=0 to iColCount-1 do
begin
EntryRC[AStartRow,AStartCol+j]:=
ADBGrid.Columns[j].Title.Caption;
ColWidth[AStartCol+j]:= ADBGrid.Columns[j].Width*50;
end; //填充数据
if ADBGrid.DataSource.DataSet.Active then
begin
with ADBGrid.DataSource.DataSet do
begin
First;
i:= AStartRow+1;
while not Eof do
begin
for j:= 0 to iColCount-1 do
begin
EntryRC[i,AStartCol+j]:= ADBGrid.Columns[j].Field.DisplayText;
end;
Next;
Inc(i);
end;
end;
end; //设置标题列居中显示,字体为粗体
SetSelection(AStartRow, AStartCol, AStartRow, AStartCol+iColCount-1);
HAlign:=F1HAlignCenter;
FontBold:=True; //按照DBGrid的对齐方式设置数据列的对齐方式
try
for j:=0 to iColCount-1 do
begin
SetSelection(AStartRow+1, AStartCol+j, AStartRow+iRowCount, AStartCol+j);
Case ADBGrid.Columns[j].Alignment of
taLeftJustify:
HAlign:=F1HAlignLeft;
taRightJustify:
HAlign:=F1HAlignRight;
taCenter:
HAlign:=F1HAlignCenter;
end;
end;
except
end; //删除DBGrid不显示的列
try
for j:=0 to iColCount-1 do
begin
if not ADBGrid.Columns[j].Visible then
begin
SetSelection(AStartRow, AStartCol+j-iUnvisibleColCount,
AStartRow+iRowCount, AStartCol+j-iUnvisibleColCount);
EditDelete(F1ShiftHorizontal);
iUnvisibleColCount:= iUnvisibleColCount+1;
end;
end;
except
end; //画边框
SetSelection(AStartRow, AStartCol, AStartRow+iRowCount, AStartCol+iColCount-1-iUnvisibleColCount);
cfTemp:=GetCellFormat;
cfTemp.BorderStyle[F1TopBorder]:=F1BorderThin;
cfTemp.BorderStyle[F1LeftBorder]:=F1BorderThin;
cfTemp.BorderStyle[F1BottomBorder]:=F1BorderThin;
cfTemp.BorderStyle[F1RightBorder]:=F1BorderThin;
cfTemp.BorderStyle[F1HInsideBorder]:=F1BorderThin;
cfTemp.BorderStyle[F1VInsideBorder]:=F1BorderThin;
SetCellFormat(cfTemp);
end;
end;
F1Book1.EntryRC[x,y] := Query1.FieldByname('FieldName').asstring;
说明:X为行Y为列,用它就可以定位Grid.
另外告你一个密秘,如果没有捕获右键,在运行时可以右键双击打开formula one.
祝你成功!
在窗体上放一个F1Book(我使用的是Formula One 6.1)和一个按纽Botton1,Botton1的Click事件如下:procedure TForm1.Button1Click(Sender: TObject);
var
myConnect : F1ODBCConnect;
myQuery : F1ODBCQuery;
begin
myConnect := CoF1ODBCConnect.Create; //创建一个ODBC连接
myConnect.ConnectStr := ''; //设置ConnectStr为空,则弹出ODBC窗口选择
F1Book1.ODBCConnectEx(myConnect, True); //打开ODBC连接
myQuery := CoF1ODBCQuery.Create; //创建一个查询
myQuery.QueryStr := ''; //设置ConnectStr为空,则弹出SQL语句创建窗口,你也可以直接设置ConnectStr为你想要的查询语句
F1Book1.ODBCQueryEx(myQuery, 1, 1, True); //执行查询,并返回结果到F1Book
F1Book1.ODBCDisconnect; //断开ODBC连接
end;运行一下,点击按纽,选择一个ODBC数据源,然后输入一个“Select * from ...”的查询语句,可以看到F1Book已经返回查询的数据集了。以上两个程序在Win98、Delphi 5、Formula One 6.1下通过。
http://202.96.94.21/person/webhome15/download/file/f1setup.zip不需要注册码,Enjoy it!
F1的
3.04版中,如何向表格中放查询结果数据?以下代码为什么出错?
F1Book1.TextRC[4,1]:=MainForm.ADOQuery1['name'];
这个版本不能预览吗?
6.1中预览为:F1Book1.FilePrintPreview;
你说得不错
我今天也看了一下,F1book确实能通过odbc直接与数据库连接
但是ConnectString应该用代码来实现,我的数据库是sql server 7
我写了,但总出错 不知你会不会写?