EXCEL表格与DELPHI(100分)

  • 主题发起人 主题发起人 txxhxxy
  • 开始时间 开始时间
T

txxhxxy

Unregistered / Unconfirmed
GUEST, unregistred user!
1、我用EXCEL制了一张表样,请问如何将数据导出到EXCEL表格中,
在不显示EXCEL情况下,将该表及数据打印出来,因本人很菜,最
好有源码,谢谢!
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls,
Forms, Dialogs, Excel97, OleServer, Db, DBTables,
Grids, DBGrids, StdCtrls;

type
TForm1 = class(TForm)
ExcelApplication1: TExcelApplication;
ExcelWorkbook1: TExcelWorkbook;
ExcelWorksheet1: TExcelWorksheet;
Table1: TTable;
Table1Name: TStringField;
Table1Capital: TStringField;
Table1Continent: TStringField;
Table1Area: TFloatField;
Table1Population: TFloatField;
button1: TButton;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;


var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.button1Click(Sender: TObject);
var
i,row,column:integer;
begin

Try
ExcelApplication1.Connect;
Except
MessageDlg('Excel may not be installed',
mtError, [mbOk], 0);
Abort;
end;

ExcelApplication1.Visible[0]:=False;
ExcelApplication1.Caption:='Excel Application';
ExcelApplication1.Workbooks.Add(Null,0);
ExcelWorkbook1.ConnectTo
(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo
(ExcelWorkbook1.Worksheets[1] as _Worksheet);
Table1.Open;
row:=1;
While Not(Table1.Eof)do

begin

column:=1;
for i:=1 to Table1.FieldCountdo

begin

ExcelWorksheet1.Cells.Item[row,column]:
=Table1.fields[i-1].AsString;
column:=column+1;
end;

Table1.Next;
row:=row+1;
end;

end;


procedure TForm1.Button2Click(Sender: TObject);
begin

ExcelWorksheet1.PrintPreview;
end;


procedure TForm1.Button3Click(Sender: TObject);
begin

ExcelWorksheet1.PrintOut;
end;


procedure TForm1.Button4Click(Sender: TObject);
begin

ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
end;


end.

 
unit OfficeForm;

interface

uses
SysUtils, Windows, Messages, Classes, Graphics,
Controls, Forms, DBCtrls, StdCtrls, DBTables,
ExtCtrls, Mask, Db, Dialogs, Excel97, Word97,
OleServer;

type
TFormOff = class(TForm)
DBEdit3: TDBEdit;
Label3: TLabel;
Label2: TLabel;
DBEdit2: TDBEdit;
DBEdit1: TDBEdit;
Label1: TLabel;
DBNavigator1: TDBNavigator;
Table1: TTable;
DataSource1: TDataSource;
BtnWord: TButton;
BtnExcel: TButton;
SaveDialog1: TSaveDialog;
ExcelApplication1: TExcelApplication;
WordDocument1: TWordDocument;
procedure BtnWordClick(Sender: TObject);
procedure BtnExcelClick(Sender: TObject);
end;


var
FormOff: TFormOff;

implementation

{$R *.DFM}

uses
ComObj, ActiveX;

procedure TFormOff.BtnWordClick(Sender: TObject);
var
Bookmark: TBookmark;
RangeW: Word97.Range;
v1: Variant;
ov1: OleVariant;
Row1: Word97.Row;
begin

WordDocument1.Activate;
// insert title
WordDocument1.Range.Text := 'American Capitals from ' + Table1.TableName;
WordDocument1.Range.Font.Size := 14;
// disable the UI
Table1.DisableControls;
try
// store the current position
Bookmark := Table1.GetBookmark;
try
// scan the database table
Table1.First;
while not Table1.EOFdo

begin

// send the two fields
WordDocument1.Range.InsertParagraphAfter;
WordDocument1.Paragraphs.Last.Range.Text :=
Table1.FieldByName ('Name').AsString + #9 +
Table1.FieldByName ('Capital').AsString;
Table1.Next;
end;

finally
// go back to the bookmark and destroy it
Table1.GotoBookmark (Bookmark);
Table1.FreeBookmark (Bookmark);
end;

finally
// re-enable the controls
Table1.EnableControls;
end;

RangeW := WordDocument1.Content;
v1 := RangeW;
v1.ConvertToTable (#9, 19, 2);
Row1 := WordDocument1.Tables.Item(1).Rows.Get_First;
Row1.Range.Bold := 1;
Row1.Range.Font.Size := 30;
Row1.Range.InsertParagraphAfter;
ov1 := ' ';
Row1.ConvertToText (ov1);
end;


procedure TFormOff.BtnExcelClick(Sender: TObject);
var
RangeE: Excel97.Range;
I, Row: Integer;
Bookmark: TBookmarkStr;
begin

// create and show
ExcelApplication1.Visible [0] := True;
ExcelApplication1.Workbooks.Add (NULL, 0);
// fill is the first row with field titles
RangeE := ExcelApplication1.ActiveCell;
for I := 0 to Table1.Fields.Count - 1do

begin

RangeE.Value := Table1.Fields .DisplayLabel;
RangeE := RangeE.Next;
end;

// add field data in following rows
Table1.DisableControls;
try
Bookmark := Table1.Bookmark;
try
Table1.First;
Row := 2;
while not Table1.EOFdo

begin

RangeE := ExcelApplication1.Range ['A' + IntToStr (Row),
'A' + IntToStr (Row)];
for I := 0 to Table1.Fields.Count - 1do

begin

RangeE.Value := Table1.Fields .AsString;
RangeE := RangeE.Next;
end;

Table1.Next;
Inc (Row);
end;

finally
Table1.Bookmark := Bookmark;
end;

finally
Table1.EnableControls;
end;

// format the section
RangeE := ExcelApplication1.Range ['A1', 'E' + IntToStr (Row - 1)];
RangeE.AutoFormat (3, NULL, NULL, NULL, NULL, NULL, NULL);
end;


initialization
CoInitialize (nil);
end.




--------------------------------------------------------------------------------


procedure TTumOgr.SpeedButton1Click(Sender: TObject);


var XApp:Variant;


sheet:Variant;


r,c:Integer;


row,col:Integer;


filName:Integer;


q:Integer;


begin


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


XApp.Visible:=true;


XApp.WorkBooks.Add(-4167);


XApp.WorkBooks[1].WorkSheets[1].Name:='Sheet1';


sheet:=XApp.WorkBooks[1].WorkSheets['Sheet1'];


for filName:=0 to DMod.OgrSiralaQuery.FieldCount-1do


begin


q:=filName+1;


sheet.Cells[1,q]:=DMod.OgrSiralaQuery.Fields[filName].FieldName;


end;



for r:=0 to DMod.OgrSiralaQuery.RecordCount-1do


begin


for c:=0 to DMod.OgrSiralaQuery.FieldCount-1do


begin


row:=r+2;


col:=c+1;


sheet.Cells[row,col]:=DMod.OgrSiralaQuery.Fields[c].AsString;


end;



DMod.OgrSiralaQuery.Next;


end;



XApp.WorkSheets['Sheet1'].Range['A1:AA1'].Font.Bold:=True;


XApp.WorkSheets['Sheet1'].Range['A1:K1'].Borders.LineStyle :=13;


XApp.WorkSheets['Sheet1'].Range['A2:K'+inttostr(DMod.OgrSiralaQuery.RecordCount-1)].Borders.LineStyle :=1;


XApp.WorkSheets['Sheet1'].Columns[1].ColumnWidth:=16;


XApp.WorkSheets['Sheet1'].Columns[2].ColumnWidth:=7;


XApp.WorkSheets['Sheet1'].Columns[3].ColumnWidth:=19;


XApp.WorkSheets['Sheet1'].Columns[4].ColumnWidth:=9;


XApp.WorkSheets['Sheet1'].Columns[5].ColumnWidth:=9;


XApp.WorkSheets['Sheet1'].Columns[6].ColumnWidth:=9;


XApp.WorkSheets['Sheet1'].Columns[7].ColumnWidth:=46;


XApp.WorkSheets['Sheet1'].Columns[8].ColumnWidth:=9;


XApp.WorkSheets['Sheet1'].Columns[9].ColumnWidth:=7;


XApp.WorkSheets['Sheet1'].Columns[10].ColumnWidth:=6;


XApp.WorkSheets['Sheet1'].Columns[11].ColumnWidth:=13;




end;

//一个完整的往EXCEL中写入数据的代码,你可以看一看.
 
该结束了,发分!
 
完颜兄:
  我按您的方法,打开了一个存在的excel表格,但运行后,提示“类未定义”,请赐教科,完了就开始发分。
 
应该是用错了Excel的方法,出错处仔细检查
 
user excel97,ComObj, OleServer

就可以了
 
请问如何在DELPHI调用EXCEL中,不预览直接进入选择打印机。
 
接受答案了.
 
后退
顶部