关于delphi和excel的问题(特急!!!)(100分)

  • 主题发起人 主题发起人 fuhe
  • 开始时间 开始时间
F

fuhe

Unregistered / Unconfirmed
GUEST, unregistred user!
各位delphi的高手们,本人现在正在为单位做一套“数据管理系统”,该系统每天
都有大量的数据需要录入。在做这套系统之前,单位一直在用excel来处理这些数据。单
位领导要我用20天的时间做一个软件来管理每天数据。我构思了一段时间,想用delphi
去控制excel。比方说,用delphi做一个“数据录入模块”,然后在把录入的数据按一定
的格式写入到excel中等等。
  本人对delphi如何控制excel知之甚少,手头又没有资料,望各位高手指点迷津。
  你的帮助是对我最大鼓励。
 
以后问问题前请先检索一下,可以省去你不少分.
给你一个检索结果:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=701040
 
这样的需求,还不如直接用excel的宏,自己编程实现,很简单,只要有VB基础。
 
哈,我有一个完全版的控制EXCEL的例子,是从数据库中读入的,如看不懂,发MAIL给我
procedure TrepForm.doit;
var
i,j,k1,k2,kkkk:integer;
memolist:tstringlist;
s_dj,s_zj:string;
tot:real;
begin
tot:=0;
try
MsExcel:=CreateOleObject('Excel.Application');
MsExcelWorkBook:=MsExcel.WorkBooks.Add;
MsExcelWorkSheet:=MsExcel.Sheets['Sheet1'];
except
showerror('无法打开 EXCEL 97');
exit;
end;
memolist:=tstringlist.Create;
MsExcel.Visible:=True;
MsExcelWorkSheet.Range['A2:E2'].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=3;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A2:E2'].value:='采购合同';
kkkk:=4;
MsExcelWorkSheet.Range['C'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['C'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='合同号码:'+maintable.fieldbyname('ht_id').asstring;
memolist.assign(maintable.fieldbyname('bftt'));
if memolist.count=0 then
memolist.add(maintable.fieldbyname('wfgs').asstring);
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':B'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':B'+inttostr(kkkk)].value:='买方:'+memolist.strings[0];
for i:=1 to (memolist.count-1) do
begin
MsExcelWorkSheet.Range['A'+inttostr(kkkk+i)+':B'+inttostr(kkkk+i)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+i)+':B'+inttostr(kkkk+i)].value:=kong(7)+memolist.strings;
end;
kkkk:=kkkk+memolist.count;
MsExcelWorkSheet.Range['C'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['C'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='合同日期:'+maintable.fieldbyname('dateis').asstring;
memolist.assign(maintable.fieldbyname('ttr'));
if memolist.count=0 then
memolist.add(maintable.fieldbyname('supplier').asstring);
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':B'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':B'+inttostr(kkkk)].value:='卖方:'+memolist.strings[0];
for i:=1 to (memolist.count-1) do
begin
MsExcelWorkSheet.Range['A'+inttostr(kkkk+i)+':B'+inttostr(kkkk+i)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+i)+':B'+inttostr(kkkk+i)].value:=kong(8)+memolist.strings;
end;
kkkk:=kkkk+memolist.count;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':B'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':B'+inttostr(kkkk)].value:='签约地点:'+maintable.fieldbyname('qddd').asstring;
MsExcelWorkSheet.Range['C'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['C'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='交货日期:'+maintable.fieldbyname('jhdate').asstring;
kkkk:=kkkk+2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='一、品名、规格、数量、金额:';
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)].Value:='货号';
MsExcelWorkSheet.Range['A'+inttostr(kkkk)].select;
MsExcel.selection.columnwidth:=15;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['B'+inttostr(kkkk)].Value:='品质说明';
MsExcelWorkSheet.Range['B'+inttostr(kkkk)].select;
MsExcel.selection.columnwidth:=30;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['C'+inttostr(kkkk)].Value:='数量';
MsExcelWorkSheet.Range['C'+inttostr(kkkk)].select;
MsExcel.selection.columnwidth:=10;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['D'+inttostr(kkkk)].Value:='单价';
MsExcelWorkSheet.Range['D'+inttostr(kkkk)].select;
MsExcel.selection.columnwidth:=12;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['E'+inttostr(kkkk)].Value:='总价';
MsExcelWorkSheet.Range['E'+inttostr(kkkk)].select;
MsExcel.selection.columnwidth:=10;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':'+'E'+inttostr(kkkk)].select;
msexcel.selection.Borders[7].LineStyle:=1;//left
msexcel.selection.Borders[8].LineStyle:=1;//top
msexcel.selection.Borders[9].LineStyle:=1;//bottom
msexcel.selection.Borders[10].LineStyle:=1;//right
msexcel.selection.Borders[11].LineStyle:=1;//内部垂直
if qtsql.active then
qtsql.close;
qtsql.sql.clear;
qtsql.sql.Add('select * from nxhtsheet99');
qtsql.sql.Add('where ht_id='+''''+maintable.fieldbyname('ht_id').asstring+'''');
qtsql.open;
qtsql.first;
j:=1;
while qtsql.eof=false do
begin
memolist.Assign(qtsql.fieldbyname('shm'));
if memolist.count=0 then
memolist.add('');
s_dj:=maintable.fieldbyname('hbdm').asstring+qtsql.fieldbyname('unite_price').asstring+'/'+qtsql.fieldbyname('dw1').asstring;
s_zj:=maintable.fieldbyname('hbdm').asstring+format('%.2f',[qtsql.fieldbyname('unite_price').asfloat*qtsql.fieldbyname('pcs').asfloat]);
tot:=tot+qtsql.fieldbyname('unite_price').asfloat*qtsql.fieldbyname('pcs').asfloat;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+j)].Value:=qtsql.fieldbyname('product_id').asstring;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['B'+inttostr(kkkk+j)].Value:=memolist.strings[0];
MsExcelWorkSheet.Range['B'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['C'+inttostr(kkkk+j)].Value:=qtsql.fieldbyname('pcs').asstring+qtsql.fieldbyname('dw1').asstring;
MsExcelWorkSheet.Range['C'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['D'+inttostr(kkkk+j)].Value:=s_dj;
MsExcelWorkSheet.Range['D'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['E'+inttostr(kkkk+j)].Value:=s_zj;
MsExcelWorkSheet.Range['E'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
k1:=j;
for i:=1 to (memolist.count-1) do
begin
j:=j+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+j)].Value:='';
MsExcelWorkSheet.Range['A'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['B'+inttostr(kkkk+j)].Value:=memolist.strings;
MsExcelWorkSheet.Range['B'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['C'+inttostr(kkkk+j)].Value:='';
MsExcelWorkSheet.Range['C'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['D'+inttostr(kkkk+j)].Value:='';
MsExcelWorkSheet.Range['D'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['E'+inttostr(kkkk+j)].Value:='';
MsExcelWorkSheet.Range['E'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
end;
k2:=j;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+k1)+':A'+inttostr(kkkk+k2)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=1;
// MsExcelWorkSheet.Range['B'+inttostr(kkkk+k1)+':B'+inttostr(kkkk+k2)].select;
// MsExcel.Selection.merge;
MsExcelWorkSheet.Range['C'+inttostr(kkkk+k1)+':C'+inttostr(kkkk+k2)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=1;
MsExcelWorkSheet.Range['D'+inttostr(kkkk+k1)+':D'+inttostr(kkkk+k2)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=1;
MsExcelWorkSheet.Range['E'+inttostr(kkkk+k1)+':E'+inttostr(kkkk+k2)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+k1)+':E'+inttostr(kkkk+k2)].select;
msexcel.selection.Borders[7].LineStyle:=1;//left
msexcel.selection.Borders[8].LineStyle:=1;//top
msexcel.selection.Borders[9].LineStyle:=1;//bottom
msexcel.selection.Borders[10].LineStyle:=1;//right
msexcel.selection.Borders[11].LineStyle:=1;//内部垂直
j:=j+1;
qtsql.next;
end;
kkkk:=kkkk+j;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':D'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=4;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':D'+inttostr(kkkk)].value:='金额合计:';
MsExcelWorkSheet.Range['E'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['E'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:=maintable.fieldbyname('hbdm').asstring+format('%.2f',[tot]);
kkkk:=kkkk+2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='二、质量技术标准:'+maintable.fieldbyname('zlyq').asstring;
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='三、交货地点:'+maintable.fieldbyname('fhyq').asstring;
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='四、包装要求:'+maintable.fieldbyname('bzyq').asstring;
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='五、质量检验:'+maintable.fieldbyname('jfcl').asstring;
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='六、结算方式:'+maintable.fieldbyname('jsfs').asstring;
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='七、运输要求:'+maintable.fieldbyname('ysfs').asstring;
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='八、损耗计算:'+maintable.fieldbyname('shjs').asstring;
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='九、配件供应:'+maintable.fieldbyname('pjgy').asstring;
memolist.assign(maintable.fieldbyname('memo'));
if memolist.count=0 then
memolist.add('');
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='十、合同备注:'+memolist.strings[0];
for i:=1 to (memolist.count-1) do
begin
MsExcelWorkSheet.Range['A'+inttostr(kkkk+i)+':E'+inttostr(kkkk+i)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+i)+':E'+inttostr(kkkk+i)].value:=kong(19)+memolist.strings;
end;
memolist.free;
end;
 
转贴
use ComObj,Excel97
全面控制 Excel

首先创建 Excel 对象,使用ComObj:
var ExcelID: Variant;

ExcelID := CreateOleObject( 'Excel.Application' );

1) 显示当前窗口:
ExcelID.Visible := True;

2) 更改 Excel 标题栏:
ExcelID.Caption := '应用程序调用 Microsoft Excel';

3) 添加新工作簿:
ExcelID.WorkBooks.Add;

4) 打开已存在的工作簿:
ExcelID.WorkBooks.Open( 'C:/Excel/Demo.xls' );

5) 设置第2个工作表为活动工作表:
ExcelID.WorkSheets[2].Activate;

ExcelID.WorksSheets[ 'Sheet2' ].Activate;

6) 给单元格赋值:
ExcelID.Cells[1,4].Value := '第一行第四列';

7) 设置指定列的宽度(单位:字符个数),以第一列为例:
ExcelID.ActiveSheet.Column[1].ColumnsWidth := 5;

8) 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例:
ExcelID.ActiveSheet.Rows[2].RowHeight := 1/0.035; // 1厘米

9) 在第8行之前插入分页符:
ExcelID.WorkSheets[1].Rows[8].PageBreak := 1;

10) 在第8列之前删除分页符:
ExcelID.ActiveSheet.Columns[4].PageBreak := 0;

11) 指定边框线宽度:
ExcelID.ActiveSheet.Range[ 'B3:D4' ].Borders[2].Weight := 3;

1-左 2-右 3-顶 4-底 5-斜( / ) 6-斜( / )

12) 清除第一行第四列单元格公式:
ExcelID.ActiveSheet.Cells[1,4].ClearContents;

13) 设置第一行字体属性:
ExcelID.ActiveSheet.Rows[1].Font.Name := '隶书';
ExcelID.ActiveSheet.Rows[1].Font.Color := clBlue;
ExcelID.ActiveSheet.Rows[1].Font.Bold := True;
ExcelID.ActiveSheet.Rows[1].Font.UnderLine := True;

14) 进行页面设置:

a.页眉:
ExcelID.ActiveSheet.PageSetup.CenterHeader := '报表演示';
b.页脚:
ExcelID.ActiveSheet.PageSetup.CenterFooter := '第&P页';
c.页眉到顶端边距2cm:
ExcelID.ActiveSheet.PageSetup.HeaderMargin := 2/0.035;
d.页脚到底端边距3cm:
ExcelID.ActiveSheet.PageSetup.HeaderMargin := 3/0.035;
e.顶边距2cm:
ExcelID.ActiveSheet.PageSetup.TopMargin := 2/0.035;
f.底边距2cm:
ExcelID.ActiveSheet.PageSetup.BottomMargin := 2/0.035;
g.左边距2cm:
ExcelID.ActiveSheet.PageSetup.LeftMargin := 2/0.035;
h.右边距2cm:
ExcelID.ActiveSheet.PageSetup.RightMargin := 2/0.035;
i.页面水平居中:
ExcelID.ActiveSheet.PageSetup.CenterHorizontally := 2/0.035;
j.页面垂直居中:
ExcelID.ActiveSheet.PageSetup.CenterVertically := 2/0.035;
k.打印单元格网线:
ExcelID.ActiveSheet.PageSetup.PrintGridLines := True;

15) 拷贝操作:

a.拷贝整个工作表:
ExcelID.ActiveSheet.Used.Range.Copy;
b.拷贝指定区域:
ExcelID.ActiveSheet.Range[ 'A1:E2' ].Copy;
c.从A1位置开始粘贴:
ExcelID.ActiveSheet.Range.[ 'A1' ].PasteSpecial;
d.从文件尾部开始粘贴:
ExcelID.ActiveSheet.Range.PasteSpecial;

16) 插入一行或一列:

a. ExcelID.ActiveSheet.Rows[2].Insert;
b. ExcelID.ActiveSheet.Columns[1].Insert;


17) 删除一行或一列:

a. ExcelID.ActiveSheet.Rows[2].Delete;
b. ExcelID.ActiveSheet.Columns[1].Delete;

18) 打印预览工作表:
ExcelID.ActiveSheet.PrintPreview;

19) 打印输出工作表:
ExcelID.ActiveSheet.PrintOut;

20) 工作表保存:
if not ExcelID.ActiveWorkBook.Saved then
ExcelID.ActiveSheet.PrintPreview;

21) 工作表另存为:
ExcelID.SaveAs( 'C:/Excel/Demo1.xls' );

22) 放弃存盘:
ExcelID.ActiveWorkBook.Saved := True;

23) 关闭工作簿:
ExcelID.WorkBooks.Close;

24) 退出 Excel:
ExcelID.Quit;



有很多类似的问题了。再贴一次吧。
procedure TForm1.Button5Click(Sender: TObject);
var
ExcelApp,MyWorkBook,mysheet:Variant;
i :byte;
s: string;
begin
try
ExcelApp:=CreateOleObject('Excel.Application');
MyWorkBook:=CreateOleobject('Excel.Sheet');
//? MyWorkBook:=ExcelApp.workbooks.open('C:/Book1.xls');
except
on Exception do raise exception.Create('无法打开Xls文件,请确认已 经安装EXCEL')
end;
ExcelApp.Visible := true;
//? mysheet:=MyWorkBook.WorkSheets[1].name;

MyworkBook:=ExcelApp.workBooks.Add;

//在此处插入读数据库及写Excel文档的代码
//其中写Excel文档的关键语句如下:
Myworkbook.worksheets[1].range['A1:D1'].Merge(True);
Myworkbook.worksheets[1].range['A1:D2'].HorizontalAlignment := $FFFFEFF4;
MyWorkBook.WorkSheets[1].Cells[1,1].Value := 'YourTitle';

i := 2;
MyWorkBook.WorkSheets[1].Cells[i,1].Value := 'yourCaption1';
MyWorkBook.WorkSheets[1].Cells[i,2].Value := 'yourCaption2';
MyWorkBook.WorkSheets[1].Cells[i,3].Value := 'yourCaption3';
MyWorkBook.WorkSheets[1].Cells[i,4].Value := 'yourCaption4';
Myworkbook.worksheets[1].Range['A1:D2'].Font.Color := clBlue;
Myworkbook.worksheets[1].Range['A1:D1'].Font.Name := '隶书';
Myworkbook.worksheets[1].Range['A1:D1'].Font.Size := 18;


i := 3;
table1.close;
table1.open;
table1.First;
while not table1.eof do begin
MyWorkBook.WorkSheets[1].Cells[i,1].Value := table1.FieldByName('au_id').AsString;
MyWorkBook.WorkSheets[1].Cells[i,2].Value := table1.FieldByName('royaltyper').AsInteger;
MyWorkBook.WorkSheets[1].Cells[i,3].Value := table1.FieldByName('au_ord').AsInteger;
MyWorkBook.WorkSheets[1].Cells[i,4].Value := table1.FieldByName('title_id').AsString;
Inc(i);
table1.Next
end;
ExcelApp.Visible := true;

s := 'A3:D'+ IntToStr(i-1);

//设定字体. 栏宽等
s := 'A1:D'+ IntToStr(i-1);
Myworkbook.worksheets[1].Columns[1].ColumnWidth := 20;
Myworkbook.worksheets[1].Columns[4].ColumnWidth := 25;
Myworkbook.worksheets[1].Rows[1].RowHeight := 50;
Myworkbook.worksheets[1].Rows[1].VerticalAlignment := $FFFFEFF4;

Myworkbook.worksheets[1].Range.Font.Name := '仿宋';
s := 'A2:D'+ IntToStr(i-1);
Myworkbook.worksheets[1].Range.Borders.LineStyle := 1;
//页面设置
MyworkBook.WorkSheets[1].PageSetup.CenterHorizontally := true;
Myworkbook.worksheets[1].pagesetup.PrintTitleRows := 'A1';


//不能设置 papersize 属性, 原因不明
//MyworkBook.WorkSheets[1].PageSetup.PaperSize := $9;

try
MyWorkBook.saveas('c:/' + Edit1.Text + '.xls');
MyWorkBook.close;
except //当存为一个已有的文档而又不覆盖时将
MyWorkBook.close; //产生一个例外
end;
ExcelApp.Quit;
ExcelApp:=Unassigned; //释放VARIANT变量

end;
另外,你最好事先搜一下,这类答案很多.不清楚哪个具体地方再问.
还有下面这个网站也有许多资料
http://www.djpate.freeserve.co.uk/Automation.htm
 
上面说的不知道全不全(太长了,没看)。
给你介绍一个网址,很全
http://www.djpate.freeserve.co.uk/Automation.htm
再,这个我做过,告诉你个秘密,千万别答应你们头,说你可以做Excel的数据导入数据库,
太麻烦了。
 
用ADO控件也可以连上Excel文件。
 
以上几位高手提供的资料正是我寻找已久的。太感谢了。
 
哦,有一个问题:如何指定某个工作表(sheet)的名字?
 
kao i 服了 you,累不累呀?
我看了半天的代码。昏倒!
 
我给你个过程吧。用来把ADO查询出来的结果,输出到EXECL。
(Excel_str:是公共变量,保存文件名)
procedure Excel_name(AdoQuery: TADOQuery); //把数据导入Excel表格
var
Sheets, columnRange: VAriant;
i, j, z, k, m: integer;
dyh: string;
begin
dyh := '''';
try
begin
if ADOQuery.IsEmpty then
begin
application.messagebox('数据表为空,不能打印!', '提示', Mb_ok + MB_ICONERROR);
exit;
end
else
begin
XlApp := CreateOleObject('Excel.Application'); //创建ole对象
Xlapp.Visible := True;
Xlapp.Workbooks.Add(XlWbatWorkSheet);
Xlapp.Workbooks[1].Worksheets[1].Name := Excel_str; //Excel名
sheets := Xlapp.Workbooks[1].worksheets[Excel_str];
sheets.cells[2, 2] := '星河软件股份有限公司(' + Excel_str + ')表';
sheets.cells[2, 2].font.size := 26;
sheets.cells[2, 2].font.bold := true;

for j := 0 to ADOQuery.fieldcount - 1 do
begin
sheets.cells[5, j + 1] := ADOQuery.fields[j].DisplayLabel;
sheets.cells[5, j + 1].borders.lineStyle := XLContinuous;
end;
ColumnRange := Xlapp.workbooks[1].worksheets[Excel_str].columns;
for k := 0 to ADOQuery.fieldcount - 1 do //设置各列的宽度
begin
if ADOQuery.fields[k].DataType in [ftstring, ftbytes] then
Columnrange.columns[k + 1].columnWidth := ADoQuery.Fields[k].size + 2;
if ADOQuery.fields[k].DataType in [ftdate, fttime, ftdateTime] then
Columnrange.columns[k + 1].columnWidth := 16;
if ADOQuery.fields[k].DataType in [ftCurrency, ftfloat, ftBCD] then
Columnrange.columns[k + 1].columnWidth := 9;
if ADOQuery.fields[k].DataType in [ftinteger, ftsmallint] then
Columnrange.columns[k + 1].columnWidth := 5;
end;
ADOQuery.First;
for i := 0 to ADOQuery.recordcount - 1 do //导出数据
begin
for z := 0 to AdoQuery.FieldCount - 1 do
with ADOQuery do
begin
if ADOQuery.fields[z].DataType in [ftCurrency, ftfloat, ftBCD] then
sheets.cells[i + 6, z + 1] := fields[z].asstring
else
sheets.cells[i + 6, z + 1] := dyh + fields[z].asstring;
sheets.cells[i + 6, z + 1].borders.lineStyle := XLContinuous;
end;
ADOQuery.next;
end;
m := ADOQuery.recordcount;
sheets.cells[4, 1] := '打印日期:';
sheets.cells[4, 1].font.size := 10;
sheets.cells[4, 1].font.bold := true;
sheets.cells[4, 2] := dyh + datetostr(now);

sheets.cells[m + 7, 1] := '操作员:';
sheets.cells[m + 7, 1].font.size := 10;
sheets.cells[m + 7, 1].font.bold := true;
sheets.cells[m + 7, 2] := Current_User;
end;
end;
except
on EDatabaseError do
begin
Application.MessageBox('数据库出错', '错误', MB_OK + MB_ICONERROR);
Exit;
end;
else //try..except..on..else..
begin
Application.MessageBox('系统出错', '错误', MB_OK + MB_ICONERROR);
Exit;
end;
end;
end;
//使用:(在使用时,调用此过程,即可,如下:)
procedure TForm_sjxscx.Button_execlClick(Sender: TObject);
begin
Excel_str := '手机销售';
Excel_name(DataModule1.ADOQuery_sjxscx);
end;
//备注:ADO查询控件,放在DataModule1模板中。
 
实在是很感谢大家,这个问题我提了好多次了,现在总算找到解答了,谢谢。。。
太感谢了
 
后退
顶部