TExcelApplication的问题 我做了一个查询,使用TExcelApplication将数据库中的内容全部写到在Excel文件中作为报表输出。请问怎样在Excel文件中画出表格的边框。谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //Excel报表的打印procedure TFmain.Print_Excel(RepID:string);var eaImport : TExcelApplication;// ewbImport : TExcelWorkbook; // v sheet : TExcelWorksheet; //sheet I,J,IrowCount,tempPos:Integer;// tempSQL,tempField,PreField:string;//临时变量 SqlStr,FileName,FieldStr:string; StartCol,StartRow:integer;begin //根据报表号找出SQL语句 //设计一张表,保存报表的基本数据 with Q1 do begin close; sql.Clear; tempSQL:='select * from syspz_Report where reportId=' + RepId; sql.Add(tempSQL); open; IRowCount:=RecordCount; if IRowCount=0 then begin ShowMess('没有该报表的记录,请确认是否添加了该报表'); exit; end; SqlStr :=Tblobfield(fieldByName('SqLStr')).asstring; FieldStr:=FieldByName('FieldStr').asstring;//显示字段的字符串 FileName:=FieldByName('Path').AsString; //文件名 StartCol:=FieldByName('StartCol').AsInteger;//起始列 StartRow:=FieldByName('StartRow').AsInteger;//起始行 end; //往EXCEL写入数据 with q1 do begin close; sql.Clear; sql.Add(SqlStr); Open; IrowCount:=RecordCount; if IRowCount=0 then begin ShowMess('该报表现在暂时没有你要的数据'); exit; end; //打开已存在的报表,插入数据 eaImport := TExcelApplication.Create(Application); try eaImport.ConnectKind := ckNewInstance; eaImport.Connect; except showmess('请先安装 Excel !'); eaImport.Free; Exit; end; try eaImport.Workbooks.Open(FileName,null,null,null,null,null,null,null,null,null,null,null,null,0); eaImport.Visible[1] := true; except eaImport.Disconnect; eaImport.Quit; eaImport.Free; showmess('无法打开文件!'); Exit; end; try ewbImport := TExcelWorkbook.Create(Application); sheet := TExcelWorksheet.Create(Application); SBar.panels[0].text:='正在读取数据...'; ewbImport.ConnectTo(eaImport.Workbooks[1]); sheet.ConnectTo(ewbImport.Worksheets[1] as _Worksheet); first; //用循环取字符串的字段,然后插入到EXCEL表里 For i:=0 to IRowCount-1 do begin j:=0; tempField:=FieldStr; //取要显示的字符串 while tempField<>'' do begin tempPos:=pos(',',tempField); PreField:=copy(tempField,1,TempPos-1); sheet.cells.Item[StartRow+i,StartCol+j]:=FieldByname(PreField).asstring; tempField:=copy(tempField,tempPos+1,length(tempField)); inc(j); end;//while next; end;//for //************************************************************************* //给数据加上边框 sheet.Range[sheet.Cells.Item[StartRow,StartCol],sheet.Cells.Item[StartRow+IRowCount,StartCol+J-1]].Borders.LineStyle :=1; {sheet.Range[sheet.Cells.Item[4,4],sheet.Cells.Item[6,6]].Borders.LineStyle :=2; sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[1].LineStyle :=3; sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[2].LineStyle :=4; sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[3].LineStyle :=5; sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[4].LineStyle :=6; sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[5].LineStyle :=7; sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[6].LineStyle :=8;} sheet.Disconnect; ewbImport.Disconnect; eaImport.Disconnect; eaImport.Quit; sheet.Free; ewbImport.Free; eaImport.Free; SBar.panels[0].text:='数据读取完毕。'; except eaImport.Disconnect; eaImport.Quit; eaImport.Free; showmess('文件读取错误!'); end;//try end;//withend; 呵呵呵,好长的代码呀!我觉得你不妨把报表做成模板的形式,再从数据库中提取数据!只做过vb的相关东西,没试过delphi ! 如何锁定目录? Delphi 7连接Informix数据库最快的连接方式是什么? 一个时钟的问题.他说我未定义的标识符 delphi中如何调用DLL中的类? 三层结构的数据库修改问题(在线等待!) 一个状态栏的问题! 有没有好的关于数据库方面的控件? 解决送高分,各位大哥大姐,求“打印”按钮的制作。 在delphi 开发的程序中怎么用.chm格式的帮助文件? DELPHI6的ADOQuery控件,如果在filter(过滤)中同时使用了and和or就会报错.为什么? 声卡问题,高手快来 将一个表已有数据写入一个新建的表
procedure TFmain.Print_Excel(RepID:string);
var
eaImport : TExcelApplication;//
ewbImport : TExcelWorkbook; // v
sheet : TExcelWorksheet; //sheet I,J,IrowCount,tempPos:Integer;//
tempSQL,tempField,PreField:string;//临时变量
SqlStr,FileName,FieldStr:string;
StartCol,StartRow:integer;begin
//根据报表号找出SQL语句
//设计一张表,保存报表的基本数据
with Q1 do
begin
close;
sql.Clear;
tempSQL:='select * from syspz_Report where reportId=' + RepId;
sql.Add(tempSQL);
open;
IRowCount:=RecordCount;
if IRowCount=0 then
begin
ShowMess('没有该报表的记录,请确认是否添加了该报表');
exit;
end;
SqlStr :=Tblobfield(fieldByName('SqLStr')).asstring;
FieldStr:=FieldByName('FieldStr').asstring;//显示字段的字符串
FileName:=FieldByName('Path').AsString; //文件名
StartCol:=FieldByName('StartCol').AsInteger;//起始列
StartRow:=FieldByName('StartRow').AsInteger;//起始行
end; //往EXCEL写入数据
with q1 do
begin
close;
sql.Clear;
sql.Add(SqlStr);
Open;
IrowCount:=RecordCount;
if IRowCount=0 then
begin
ShowMess('该报表现在暂时没有你要的数据');
exit;
end; //打开已存在的报表,插入数据
eaImport := TExcelApplication.Create(Application);
try
eaImport.ConnectKind := ckNewInstance;
eaImport.Connect;
except
showmess('请先安装 Excel !');
eaImport.Free;
Exit;
end;
try
eaImport.Workbooks.Open(FileName,null,null,null,null,null,null,null,null,null,null,null,null,0);
eaImport.Visible[1] := true;
except
eaImport.Disconnect;
eaImport.Quit;
eaImport.Free;
showmess('无法打开文件!');
Exit;
end;
try
ewbImport := TExcelWorkbook.Create(Application);
sheet := TExcelWorksheet.Create(Application);
SBar.panels[0].text:='正在读取数据...'; ewbImport.ConnectTo(eaImport.Workbooks[1]);
sheet.ConnectTo(ewbImport.Worksheets[1] as _Worksheet); first;
//用循环取字符串的字段,然后插入到EXCEL表里
For i:=0 to IRowCount-1 do
begin
j:=0;
tempField:=FieldStr;
//取要显示的字符串
while tempField<>'' do
begin
tempPos:=pos(',',tempField);
PreField:=copy(tempField,1,TempPos-1); sheet.cells.Item[StartRow+i,StartCol+j]:=FieldByname(PreField).asstring; tempField:=copy(tempField,tempPos+1,length(tempField));
inc(j);
end;//while next;
end;//for
//*************************************************************************
//给数据加上边框
sheet.Range[sheet.Cells.Item[StartRow,StartCol],sheet.Cells.Item[StartRow+IRowCount,StartCol+J-1]].Borders.LineStyle :=1;
{sheet.Range[sheet.Cells.Item[4,4],sheet.Cells.Item[6,6]].Borders.LineStyle :=2;
sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[1].LineStyle :=3;
sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[2].LineStyle :=4;
sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[3].LineStyle :=5;
sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[4].LineStyle :=6;
sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[5].LineStyle :=7;
sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[6].LineStyle :=8;} sheet.Disconnect;
ewbImport.Disconnect;
eaImport.Disconnect;
eaImport.Quit;
sheet.Free;
ewbImport.Free;
eaImport.Free;
SBar.panels[0].text:='数据读取完毕。';
except
eaImport.Disconnect;
eaImport.Quit;
eaImport.Free;
showmess('文件读取错误!');
end;//try
end;//with
end;
我觉得你不妨把报表做成模板的形式,再从数据库中提取数据!
只做过vb的相关东西,没试过delphi !