已經3天了(10分)

  • 主题发起人 主题发起人 pllink_qyd
  • 开始时间 开始时间
P

pllink_qyd

Unregistered / Unconfirmed
GUEST, unregistred user!
數據庫導入excel問題:(或者誰提供一段正確的代碼阿。。感謝ING…)
請問我該引用的都引用了。。為什麼還一直提示我createoleobject沒定義。。一直停留在那行出錯。。
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,shellapi, Excel2000, OleServer, Buttons, DB, ADODB,comobj;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
ExcelID:Variant;
I:Integer;
begin
ADOQuery1.close;
ADOQuery1.sql.add('select * from Product where Pro_time>='''+Edit1.text+''' and Pro_time<='''+Edit2.text+''' ');
ADOQuery1.open;
if ADOQuery1.recordcount<>0 then
begin
if SD.Execute then
begin
try
ExcelID := Application.createoleobject('Excel.Application');
Except
Application.MessageBox('你的計算机可能沒有安裝EXCEL','提示',MB_OK+MB_ICONERROR);
exit;
end;
ExcelID.workBooks.add;
for I:=0 to ADOQuery1.recordcount do
begin
ExcelID.activesheet.Range['A'+IntToStr(I+1)].Value:=ADOQuery1.FieldByName('pro_time').Asstring;
ExcelID.ActiveSheet.Range['B'+IntToStr(I+1)].Value:=ADOQuery1.FieldByName('memo').Asstring;
ADOQuery1.Next;
end;
ExcelID.ActiveWorkbook.SaveAs(SD.FileName);
ExcelID.workBooks.close;
ExcelID.Quit;
Application.Message('成功','提示',MB_OK);
end;
end;

end.
 
uses ComObj;
procedure TForm1.FormCreate(Sender: TObject);
var
s:variant;
begin
ExcelID := Application.createoleobject('Excel.Application');
改成;
ExcelID := createoleobject('Excel.Application');

s:=CreateOleObject()
end;
 
to 蓝叶菱;
首先真的很感謝你,偶像。。我很多問題你都是第一個進來的。。你對新手很熱心。真的。鞠躬。
按你說的做。。出現新的問題了。。不是運行出錯。。是結果出錯。。excel很奇怪。。中間都是空白的。。就頭跟尾。。頭那邊就第一個字段值顯示在那邊。哎。。就是保存失敗拉。誰有更好的demo嗎?
 
www.delphibox.com上面有一个导出到EXCEL控件!
 
to jieking
謝謝,但我想知道怎麼做。誰能幫下我嗎?
 
while not ADOQuery1.eof do
begin
ExcelID.activesheet.Range['A'+IntToStr(I+1)].Value:=ADOQuery1.FieldByName('pro_time').Asstring;
ExcelID.ActiveSheet.Range['B'+IntToStr(I+1)].Value:=ADOQuery1.FieldByName('memo').Asstring;
ADOQuery1.Next;
end;
 
这两个是我最常用,都给你了

procedure TMain.ExportDBGrid(DBGrid1:TDBGrid;toExcel: Boolean);
var
bm: TBookmark;
col, row: Integer;
sline: String;
mem: TMemo;
ExcelApp: Variant;
begin
Screen.Cursor := crHourglass;
DBGrid1.DataSource.DataSet.DisableControls;
bm := DBGrid1.DataSource.DataSet.GetBookmark;
DBGrid1.DataSource.DataSet.First;

// create the Excel object
if toExcel then
begin
ExcelApp := CreateOleObject('Excel.Application');
ExcelApp.WorkBooks.Add(xlWBatWorkSheet);
ExcelApp.WorkBooks[1].WorkSheets[1].Name := 'Grid Data';
end;

// First we send the data to a memo
// works faster than doing it directly to Excel
mem := TMemo.Create(Self);
mem.Visible := false;
mem.Parent := Main;
mem.Clear;
sline := '';

// add the info for the column names
for col := 0 to DBGrid1.FieldCount-1 do
sline := sline + DBGrid1.Fields[col].DisplayLabel + #9;
mem.Lines.Add(sline);

// get the data into the memo
for row := 0 to DBGrid1.DataSource.DataSet.RecordCount-1 do
begin
sline := '';
for col := 0 to DBGrid1.FieldCount-1 do
sline := sline + DBGrid1.Fields[col].AsString + #9;
mem.Lines.Add(sline);
DBGrid1.DataSource.DataSet.Next;
end;

// we copy the data to the clipboard
mem.SelectAll;
mem.CopyToClipboard;

// if needed, send it to Excel
// if not, we already have it in the clipboard
if toExcel then
begin
ExcelApp.Workbooks[1].WorkSheets['Grid Data'].Paste;
ExcelApp.Visible := true;
end;

FreeAndNil(mem);
// FreeAndNil(ExcelApp);
DBGrid1.DataSource.DataSet.GotoBookmark(bm);
DBGrid1.DataSource.DataSet.FreeBookmark(bm);
DBGrid1.DataSource.DataSet.EnableControls;
Screen.Cursor := crDefault;
end;

///////////////////////////////////////////
procedure TCall.Button2Click(Sender: TObject);
var Col:Char;
Row,j:integer;
R:String;
empty:OleVariant;
begin
try
Excelapplication1.Connect;
except
Showmessage('Excel 可能没有安装。');
exit;
end;
ExcelApplication1.Visible[0] := True;
try
empty := EmptyParam;
ExcelApplication1.Workbooks.Add(empty,0);
ExcelApplication1.Caption:=FormatdateTime('yymmdd',DateTimePicker2.Date);
ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks.Item[
ExcelApplication1.Workbooks.Count]);
ExcelWorkBook1.Activate ;
ExcelWorksheet1.ConnectTo((ExcelWorkbook1.Worksheets[1] as _WorkSheet));
Excelworksheet1.Range['A1','A1'].Value2:='编号';
Excelworksheet1.Range['B1','B1'].Value2:='名称';
Excelworksheet1.Range['C1','C1'].Value2:='数量';
Excelworksheet1.Range['D1','D1'].Value2:='时间';
Excelworksheet1.Range['E1','E1'].Value2:='预留';
//Excelworksheet1.Cells.w
// for i:=0 to
with query1 do
begin
first;
Col :='A'; //第一列为A
Row := 2;
while not eof do
begin
Excelworksheet1.Cells.Item[row,1].NumberFormatLocal:='@';
Excelworksheet1.Cells.Item[row,4].NumberFormatLocal:='@';
for j :=0 to Fields.Count-2 do
begin
R := String(Col) + IntToStr(Row);
ExcelWorksheet1.Range[R,R].Value2 := Fields.Fields[j].Value ;
Col := Chr(Ord(Col)+1);
end;
Inc(Row);
Col := 'A';
Next;
end;
end;
Excelapplication1.Disconnect ;
except
ExcelWorkBook1.Close(xlDoNotSaveChanges);
ExcelApplication1.Disconnect;
end;
end;
 
to babibean;
不行阿。。
 
謝謝大家。。我的問題解決了。。結貼了。。散分了。不多。見笑了!
 
后退
顶部