delphi如何利用Excel97生成报表,十万火急!!!!!!(300大元奉上)(100分)

  • 主题发起人 主题发起人 地平线
  • 开始时间 开始时间

地平线

Unregistered / Unconfirmed
GUEST, unregistred user!
利用Excel97生成报表,最好可以给出例子。
 
可以调用OFFICE的宏来实现啊。调用OFFICE的宏能实现任何OFFICE功能(基本上)。
我这儿没有现成的怎么样来控制EXCEL的代码,虽然以前用过,可惜不在了。不过有怎么
样来控制WORD的代码,均测试通过的,其基本功能有书签定位、插入表格、替换字符等等
功能。我想原理是一样的,可以触类旁通嘛。
要不要,要的话,给你发一个过去。
 
我也要可以发一个吗?gyzly@263.net
 
http://www.delphibbs.com/delphibbs/DispQ.asp?LID=446727
 
实际上就是office vba,delphi5已经做了很多封装了
 
....
ExcelApplication1: TExcelApplication;
ExcelWorkbook1: TExcelWorkbook;
ExcelWorksheet1: TExcelWorksheet;
....

procedure Tfrmexcel.FormClose(Sender: TObject;
var Action: TCloseAction);
begin

Try
ExcelWorksheet1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
Except
on e:exceptiondo

showmessage(pchar('退出excel时出错,如果不能再次使用,请先结束任务:'+e.message);
end;

end;


procedure Tfrmexcel.createexcelform;
begin

try
ExcelApplication1.Connect;
ExcelApplication1.Visible[0]:=True;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(EmptyParam,0));
// ExcelWorkSheet1.Activate;
ExcelWorkSheet1.ConnectTo(ExcelWorkbook1.WorkSheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _WorkSheet);
ExcelWorkSheet1.Activate;
except
on e:exceptiondo

begin

showmessage(pchar('启动excel时出错,检查是否安装了excel:'+e.message));
ExcelWorksheet1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
exit;
end;

end;

end;

procedure Tfrmexcel.BitBtn2Click(Sender: TObject);
var i: integer;
begin

//创建excel
createexcelform;
//输出数据
Try
with datasetdo

begin

disablecontrols;
first;
while not eofdo

begin

for i:=0 to 域count-1do

begin

ExcelWorkSheet1.Cells.Item[RecNo,i+1] := Fields.AsVariant;
end;

next;
end;

enablecontrols;
end;

Except
on e:exceptiondo

begin

showmessage(pchar('输出数据时出错:'+e.message));
exit;
end;

end;

end;
 
还不行呀。我是要往一个已经做好的格式固定的excel表里面加数据。
现在有两个问题没法解决:
1。如何打开已经存在的excel表。
2。如何往表里添数据。
 
很简单,象上面的例子一样,先建立一个OLE Automation对象,然后用这个对象控制,你想
干什么都可以的。给你个例子,希望你看的懂:

if gb_ExcelHaveOpen = False then

begin

vExcel := CreateOleObject('Excel.Application');
sPath := ExtractFilePath(Application.ExeName);
sFile := sPath + CSTR_TRANVELPLACEMONTH;
vExcel.WorkBooks.Open(sFile);
vSheet := vExcel.WorkBooks[1].WorkSheets[1];
end;

with wwDBE_Monthen
tNo.DataSource.DataSetdo

begin

sYear := FieldByName('INFO_YEAR').AsString;
sEntNo := FieldByName('ENT_NO').AsString;
vSheet.Cells[5, 3] := FieldByName('ENT_NAME').AsString;
vSheet.Cells[6, 3] := FieldByName('ENT_NO').AsString;
.
.
.
end;


 
这方面的资料太多了。建议你最好把它做成一个控件。
通过控件到自动把数据库的数据写到Excel文件中。如果需要我这边有一堆资料。发给你
 
热水:我也要,谢谢!xiximing@263.net
 
这个问题对我来说太简单,我参与制作的《河南省环保总局报表生成与决策系统》打印报表全都
是这样做的。下面我给出我最近做的《自考生成绩报表系统》的原码,我想这一定能满足你的要
求:
unit UntFrmMain;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, StdCtrls, Buttons, ExtCtrls, ComCtrls, Grids, DBGrids,
DBCtrls;

type
TFrmMain = class(TForm)
pnlTool: TPanel;
BitBtnPrint: TBitBtn;
BitBtnQuit: TBitBtn;
DSPrint: TDataSource;
DBNavigator: TDBNavigator;
DBGridPrint: TDBGrid;
StatusBar: TStatusBar;
EdtGrade: TEdit;
Label1: TLabel;
EdtYear: TEdit;
Label2: TLabel;
qryPrint: TTable;
EdtPage: TEdit;
procedure BitBtnQuitClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BitBtnPrintClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;


var
FrmMain: TFrmMain;
ThisNo : Integer;
implementation
Uses ComObj;//这一行不可少呦
{$R *.DFM}

procedure TFrmMain.BitBtnQuitClick(Sender: TObject);
begin

if QryPrint.Active then

QryPrint.Close;
Close;
end;


procedure TFrmMain.FormCreate(Sender: TObject);
begin

Qryprint.DatabaseName:=ExtractFileDir(Application.ExeName);
QryPrint.TableName:='DBInfo.DB';
if Qryprint.Active=False then
Qryprint.Active:=True;
ThisNo:=0;
end;


procedure TFrmMain.BitBtnPrintClick(Sender: TObject);
Var
I,J:Integer;
QryPrintView:TQuery;
ExcelVar:Variant;
cellposition:string;
Const
SQLStr='SELECT ID, Name, Sex, Political, Birthday, Post, FinalRec, Code1, Code2, Code3, Code4, Remark '+
'FROM "DBInfo.db" Dbinfo';

HRang:array[1..14] of string=('A','B','C','D','E','F','G','H','I','J','K','L','M','N');
begin

QryPrintView := TQuery.Create(Self);
try
QryprintView.DataBaseName:=ExtractFileDir(Application.ExeName);
QryprintView.Close;
QryprintView.SQL.Clear;
QryprintView.SQL.Add(Sqlstr);
QryprintView.Open;
QryPrintView.First;
QryprintView.MoveBy(ThisNo);
if QryPrintView.Eof then
Exit;
ExcelVar:=CreateOleObject('Excel.Application');
ExcelVar.WorkBooks.Open(ExtractFilePath(ParamStr(0))+'StuRegister.xls');//事先画好的Excel表

ExcelVar.range['A1'].value:=EdtGrade.Text;
ExcelVar.range['C1'].value:=EdtYear.Text;

for i:=5 to 24do

begin

for j:=1 to QryprintView.fieldcountdo

begin

cellposition:=hrang[j]+inttostr(i);
ExcelVar.range[cellposition].value:=QryprintView.fields[j-1].asstring;
end;

if Not QryPrintView.Eof then

QryprintView.next
else

begin

ShowMessage('打印数据输出完毕!!!');
Break;
end;

end;


finally
QryPrintView.Free;
end;

ExcelVar.application.Visible:=True;
ExcelVar.activesheet.printpreview;
ThisNo:=ThisNo+20;
EdtPage.Text:='第'+IntToStr(Round(ThisNo/20)+1)+'页';
end;


end.

 
Excel也能当成数据库来操作。
 
Excel也能操作数据库。
sql
 
jollier,用你的方法,我已经完整解决了问题。
其它朋友也提出了很好的办法。不过不太适合我的要求。在此也谢谢你们。
 
热水:我也要,非常谢谢!
monky007@sina.com.cn
 
热水:我也要,非常谢谢!
 
热水:我也要,非常谢谢!
zcm1975117@21cn.com
 
下面是我写的通用文档微机管理系统的打印部分原代码,包括合并execl单元格,加入分页符号,设置行高,列宽,设置execl页头,设置每页记录数,画单元格边框,调用execl模板等等
软件主页 http://xhonghui.patching.net/

procedure TfrmMain.Button3Click(Sender: TObject);
//动态打印excel报表
var
tiaojian : string;
//tiaojian 为一个sql查询语句,全宗号,目录号,起始日期为一数据库字段
begin


tiaojian := '';
if Edit7.Text <> '' then

begin

tiaojian := tiaojian + ' ( 全宗号=''' + EDit7.Text + ''' ) and';
end;

if Edit8.Text <> '' then

begin

tiaojian := tiaojian + ' ( 目录号=''' + Edit8.Text + ''') and';
end;

if Edit9.Text <> '' then

begin

tiaojian := tiaojian + ' ( 起始日期>= ' + Edit9.Text + ' ) and';
end
else
tiaojian := tiaojian + ' ( 起始日期>=0) and';

if Edit10.Text <> '' then

begin

tiaojian := tiaojian + ' ( 起始日期<= ' + Edit10.Text + ' ) and';
end;

tiaojian := copy(tiaojian, 1, length(tiaojian) - 3);
//tiaojian 为一个sql查询语句
Printanjuan(tiaojian);
//调用打印过程
ShellExecute(Handle, 'Open', PChar(GetCurpath + 'temp.xls'), nil, nil, sw_shownormal);
//调用excel查看生成的文件
ProgressBar1.Position:=0;
//ProgressBar1 为一个进程条控件
end;


procedure TfrmMain.Printanjuan(tiaojiao:string);
var
nowhangi, i, jilushu, LCID: integer;
//nowhangi当前 execl的所在行,jilushu当前的记录位置
ssql, PathName: string;
oldcur: tcursor;
ExcelApplication1: TExcelApplication;
ExcelWorkbook1: TExcelWorkbook;
ExcelWorkSheet1: TExcelWorksheet;
xl, Cell1: olevariant;
begin

LCID := LOCALE_USER_DEFAULT;
if FileExists(GetCurpath + 'temp.xls') then
//删除动态生成的临时temp.xls文件
try
DeleteFile(GetCurpath + 'temp.xls');
except
end;


ssql := '';
//执行sql查询
ssql := 'select * from mainanjuan where (' + tiaojiao + ') order by 系统编号 ';
do
adosql(dm1.queryanjuan, ssql);
begin

try
ExcelApplication1 := TExcelApplication.Create(Self);
ExcelWorkbook1 := TExcelWorkbook.Create(Self);
ExcelWorkSheet1 := TExcelWorksheet.Create(Self);


except
showmessage('对不起,您没有安装Excel 2000!');
abort;
end;

end;

ExcelApplication1.Connect;
ExcelApplication1.Workbooks.Add(null, 0);
//如果调用一个模板 ,改动该句如:
//ExcelApplication1.Workbooks.Add('c:/zichang.xls', 0);
ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
//写入execl
ProgressBar1.Position:=5;
if dm1.queryanjuan.RecordCount > 0 then

begin

//设置列宽,行高
nowhangi := 1;
ExcelApplication1.Range['A1', 'A1'].ColumnWidth := 3.5;
ExcelApplication1.Range['B1', 'B1'].ColumnWidth := 3.5;
ExcelApplication1.Range['C1', 'C1'].ColumnWidth := 3.5;
ExcelApplication1.Range['D1', 'D1'].ColumnWidth := 8;
ExcelApplication1.Range['E1', 'E1'].ColumnWidth := 20;
ExcelApplication1.Range['F1', 'F1'].ColumnWidth := 8.75;
ExcelApplication1.Range['G1', 'G1'].ColumnWidth := 8.75;
ExcelApplication1.Range['H1', 'H1'].ColumnWidth := 3.0;
ExcelApplication1.Range['I1', 'I1'].ColumnWidth := 3.0;
ExcelApplication1.Range['J1', 'J1'].ColumnWidth := 3.0;
//设置每页的记录数目
for jilushu := 1 to (dm1.queryanjuan.RecordCount)do

begin
//ProgressBar1 为一个进程条控件
ProgressBar1.Position:= (jilushu*100) div (dm1.queryanjuan.RecordCount);
try
if jilushu mod strtoint(edit14.Text) = 1 then
//edit14.Text的值为一个数字,表示每页的记录数目
begin
//打印每页的页头
ExcelApplication1.Range['A' + inttostr(nowhangi), 'J' + inttostr(nowhangi)].Merge(xl);//合并execl单元格
ExcelWorkSheet1.cells.Item[nowhangi, 1].font.Name := fontselectbox.FontName;
ExcelWorkSheet1.cells.Item[nowhangi, 1].font.size := 24;
ExcelWorkSheet1.cells.Item[nowhangi, 1].RowHeight := 32;
ExcelWorkSheet1.cells.Item[nowhangi, 1] := '案卷目录';
ExcelWorkSheet1.cells.Item[nowhangi, 1].HorizontalAlignment := xlCenter;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 1].font.Name := fontselectbox.FontName;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 1].font.size := 12;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 1].Font.Bold := True;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 1].Orientation := xlVertical;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 1].VerticalAlignment := xlTop;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 1] := '顺序号';
ExcelWorkSheet1.cells.Item[nowhangi + 1, 2].font.Name := fontselectbox.FontName;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 2].font.size := 12;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 2].Font.Bold := True;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 2].Orientation := xlVertical;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 2].VerticalAlignment := xlTop;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 2] := '全宗号';
ExcelWorkSheet1.cells.Item[nowhangi + 1, 3].font.Name := fontselectbox.FontName;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 3].font.size := 12;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 3].Font.Bold := True;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 3].Orientation := xlVertical;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 3].VerticalAlignment := xlTop;
ExcelWorkSheet1.cells.Item[nowhangi + 1, 3] := '目录号';
//加入分页符号
if (jilushu div strtoint(edit14.Text) > 0) then

begin


Cell1 := ExcelWorksheet1.Cells.Item[nowhangi, 11];
ExcelWorksheet1.VPageBreaks.Add(Cell1);
ExcelWorksheet1.HPageBreaks.Add(Cell1);
end;

nowhangi := nowhangi + 2;

end;

//打印查询数据
ExcelWorkSheet1.cells.Item[nowhangi, 1].RowHeight := 60;
ExcelWorkSheet1.cells.Item[nowhangi, 1].font.Name := fontselectbox.FontName;
ExcelWorkSheet1.cells.Item[nowhangi, 1].font.size := strtoint(Edit11.text);
ExcelWorkSheet1.cells.Item[nowhangi, 1].WrapText := True;
ExcelWorkSheet1.cells.Item[nowhangi, 1] := inttostr(jilushu);
ExcelWorkSheet1.cells.Item[nowhangi, 2].font.Name := fontselectbox.FontName;
ExcelWorkSheet1.cells.Item[nowhangi, 2].WrapText := True;
ExcelWorkSheet1.cells.Item[nowhangi, 2].font.size := strtoint(Edit11.text);
ExcelWorkSheet1.cells.Item[nowhangi, 2] := dm1.queryanjuan.fieldbyname('全宗号').asstring;
ExcelWorkSheet1.cells.Item[nowhangi, 3].font.Name := fontselectbox.FontName;
ExcelWorkSheet1.cells.Item[nowhangi, 3].WrapText := True;
ExcelWorkSheet1.cells.Item[nowhangi, 3].font.size := strtoint(Edit11.text);
ExcelWorkSheet1.cells.Item[nowhangi, 3] := dm1.queryanjuan.fieldbyname('目录号').asstring;


except
end;

dm1.queryanjuan.Next;
nowhangi := nowhangi + 1;
end;

end;

try //插入页码
ExcelWorkSheet1.PageSetup.RightHeader := '&amp;9第&amp;P 页 共&amp;N页';
except
showmessage('您可能没有安装打印机');
end;

for i := 1 to nowhangi - 1do
//画单元格边框
if taoda.Checked then

begin

with ExcelApplication1.Range['A' + inttostr(i), 'J' + inttostr(i)].Bordersdo

begin

LineStyle := xlContinuous;
Weight := xlThin;
ColorIndex := xlAutomatic;
end;

end;


ExcelWorkBook1.SaveCopyAs(GetCurpath + 'temp.xls');
ExcelApplication1.DisplayAlerts[LCID] := False;
ExcelApplication1.Quit;
ExcelWorksheet1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;

end;


procedure TfrmMain.DoAdoSql(CurADODS: TADODataSet;
SSql: string);
var
oldcur: tcursor;

begin

sb1.Panels[0].Text := '正在查询数据……';
//sb1:Tstatusbar控件
sb1.Refresh;
oldcur := Screen.Cursor;
screen.cursor := crHourGlass;
try
if CurADODS.Active = true then
CurADODS.Close;
CurADODS.CommandText := SSql;
CurADODS.Open;
finally
screen.Cursor := oldcur;
end;

end;
 
请热水发资料到zhao_jw@263.net 另外大家能否从使用模板定义的方式定义一个表
并从程序中得到数据入手,做出一个可用户定制的报表呢?希望大家讨论
 
可以发给我一份吗?急需多谢了。
EAMIL:xieyj@esquel.com
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部