急!!!TQUERY的数据怎样用execel打印出来?(100分)

  • 主题发起人 主题发起人 xywu
  • 开始时间 开始时间
X

xywu

Unregistered / Unconfirmed
GUEST, unregistred user!
如何把 TQUERY查询的数据用execel打印出来,
我知道是用server下的Execel组件,但不知道具体是哪个空间,
能不能说详细点,最好给点代码。
感谢万分!!
 
查询以前的贴子吧 很多很多
:))))
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=709974
 
uses comobj, excel97;

procedure TForm1.DBGridToExcel(Multiselect: Boolean; FileNam: string; DBGrid: TDBGrid);
var
MSExcel, MSExcelWorkBook, MSExcelWorkSheet: Variant;
ColumnRange : Variant;
LinesCount, FieldsCount, i, j: Integer;
ExportText : Variant;
SaveCursor : TCursor;
ColumnWidth : array of Integer;
function GetRangeAddr(const ColIndex, RowIndex: Integer): string;
var
i : integer;
begin
if ColIndex > 26 then
Result := Chr((ColIndex div 26) + 64);
i := ColIndex mod 26;
if i > 0 then Result := Result + Chr(i + 64);
Result := Result + IntToStr(RowIndex);
end;
begin
try
MSExcel := CreateOleObject('Excel.Application');
MSExcelWorkBook := MSExcel.WorkBooks.Add;
MSExcelWorkSheet := MSExcel.WorkSheets.Add;
except
ShowMessage('无法与Microsoft Excel连接!');
abort;
end;

try
with DBGrid do
begin
SaveCursor := Screen.Cursor;
Screen.Cursor := crHourGlass;
try
FieldsCount := DBGrid.Columns.Count;
with DataSource.DataSet do
begin
DisableControls;
try
LinesCount := 1;
First;
while not Eof do
begin
Inc(LinesCount);
Next;
end;
ExportText := VarArrayCreate([1, LinesCount, 1, FieldsCount], VarVariant); //創建二維數組
LinesCount := 1;
SetLength(ColumnWidth, FieldsCount);
for i := 0 to FieldsCount - 1 do
begin
ExportText[LinesCount, i + 1] := Columns.Title.Caption; //將Columns列标题写到第一行
ColumnWidth := Round(Columns.Width / 64 * 10); //保存Columns列宽以便在Excel中设置列宽
end;
if not MultiSelect then //如果不支持多重选择
begin
First;
while not Eof do
begin
Inc(LinesCount);
for i := 0 to FieldsCount - 1 do
ExportText[LinesCount, i + 1] := DBGrid.Columns.Field.Value;
Next;
end;
end
else
begin //如果支持多重选择
if DBGrid.SelectedRows.Count > 0 then
with DBGrid.DataSource.DataSet do
for i := 0 to DBGrid.SelectedRows.Count - 1 do
begin
GotoBookMark(Pointer(DBGrid.SelectedRows.Items));
Inc(LinesCount);
for j := 0 to FieldsCount - 1 do
ExportText[LinesCount, j + 1] := DBGrid.Columns.Field.Value;
end;
end;
MSExcelWorkSheet.Range['A1:' + GetRangeAddr(FieldsCount, LinesCount)].value := Exporttext;
ColumnRange := MSExcelWorkSheet.Columns;
for i := 1 to FieldsCount do
ColumnRange.Columns.ColumnWidth := ColumnWidth[i - 1]; //设置列宽
MSExcelWorkSheet.Range['A1:' + GetRangeAddr(FieldsCount, LinesCount)].Borders.LineStyle := 1; //設置邊界線
finally
EnableControls;
end;
end;
MSExcelWorkSheet.SaveAs(Filenam);
finally
Screen.Cursor := SaveCursor;
end;
end;
Application.BringToFront; //激活用程序
ShowMessage('所選資料已成功地存檔到'#13 + Filenam);
finally
try
MSExcel.Quit;
except
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
DBGridToExcel(False, 'c:/Book1.xls', DbGrid1);
end;
//也是从以前的贴子抄来的,我试了,好用
 
请问以下 MSExcel := CreateOleObject('Excel.Application')中的createoleobject
如何定义???
 
简单,全文搜索“Excel”关键字,有很多相关的帖子。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
后退
顶部