怎樣在delphi中將excel表匯入SQL2005,求教啊。做畢業設計苦逼
uj5u.com熱心網友回復:
百度找的不行哦。提示一下錯誤,請問這代碼什么問題procedure TForm1.Button1Click(Sender: TObject);
var excelx,excely : string;
begin
try
ExcelApp := CreateOleObject('Excel.Application');
WorkBook := ExcelApp.WorkBooks.Open(opendialog.FileName);//使用opendialog對話框指定
//excel檔路徑
ExcelApp.Visible := false;
ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
for i := 1 to excelrowcount + 1 do
begin
excelx := excelapp.Cells[i,1].Value;
excely := excelapp.Cells[i,2].Value;
if ((excelapp.Cells[i,1].Value = '') and (ExcelApp.Cells[i,2].Value = '')) then
//指定excel檔的第 i 行 ,第 1,2(看情況而定)行如果為空就退出,這樣的設定,最好是你的檔案力這兩行//對應資料庫中不能為空的資料
exit
else
with query1 do
begin
close;
sql.clear;
sql.add(insert into test(name,address) values(:name,:address));
parambyname('name').asstring := excelx;//excel檔的第一列插入到test表的 name欄位;
parambyname('address').asstring := excely;//excel檔的第二列插入到test表的 address 欄位;
execsql;
end;
end;
finally
WorkBook.Close;
ExcelApp.Quit;
ExcelApp := Unassigned;
WorkBook := Unassigned;
end;
end;
uj5u.com熱心網友回復:
提示的錯誤
uj5u.com熱心網友回復:
use comobjvar
excelapp,workbook:variant;
uj5u.com熱心網友回復:
還是有錯呀,還有控制元件需要用到哪些,需要設定什么屬性嗎
uj5u.com熱心網友回復:
建議先匯入到tclientdataset表中處理。在上傳保存到資料庫。uj5u.com熱心網友回復:

加了以上陳述句都成這樣
uj5u.com熱心網友回復:
那要怎么弄???給我詳細的程序,以及需要用到的控制元件
uj5u.com熱心網友回復:
WorkBook := ExcelApp.WorkBooks.Open('c:\aa.xls')直接丟檔案路徑
uj5u.com熱心網友回復:
使用TADOConnection和TADOTable控制元件;TADOConnection配置為:Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=I:\財經要聞.xls;Mode=Share Deny None;Extended Properties="Excel 8.0;HDR=YES;IMEX=1";Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB:Database Password="";Jet OLEDB:Engine Type=35;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False特別注意:Extended Properties="Excel 8.0;HDR=YES;IMEX=1";或Extended Properties="Excel 5.0;HDR=YES;IMEX=1";需要在配置時在【所有】面板進行修改。TADOTable中的tablename是xls檔案中的sheet,連接后可以下拉選擇。
另外和普通Delphi程式一樣。配置TDataSetProvider、tdatasource和dbgrid這類組件。資料可以直接匯入到tclientdataset表中,并在資料顯示控制元件中顯示和編輯了。
配置了tadoquery,不知道怎么回事總是提示from的表找不到和錯誤。
uj5u.com熱心網友回復:
另外補充下:tadotable的tabledirect屬性必須設定為trueuj5u.com熱心網友回復:
簡單高效,這是Delphi的優點,也是俺喜歡的原因。
uj5u.com熱心網友回復:
type
Tlenordtefr = class(TForm)
dxBarManager1: TdxBarManager;
dxBarManager1Bar1: TdxBar;
dxBarLargeButton1: TdxBarLargeButton;
dxBarLargeButton2: TdxBarLargeButton;
dxBarLargeButton3: TdxBarLargeButton;
StatusBar1: TStatusBar;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
ADOQuery2: TADOQuery;
DataSource1: TDataSource;
DataSource2: TDataSource;
ADOQuery3: TADOQuery;
OpenDialog1: TOpenDialog;
DBGridEh1: TDBGridEh;
procedure dxBarLargeButton1Click(Sender: TObject);
procedure dxBarLargeButton2Click(Sender: TObject);
procedure dxBarLargeButton3Click(Sender: TObject);
private
{ Private declarations }
a,b:Integer;
public
{ Public declarations }
connstr:Cardinal;
end;
var
lenordtefr: Tlenordtefr;
implementation
uses Syslike,syshome,Activex;
const conntime=1;
{$R *.dfm}
procedure Tlenordtefr.dxBarLargeButton1Click(Sender: TObject);
var
sFilename,NSql,CSQL:string;
i:integer;
sl:string;
begin
inherited;
if OpenDialog1.Execute then
begin
sFilename:= Trim(OpenDialog1.FileName) ;
ADOConnection1.Connected := false;
ADOConnection1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+
sFilename+
';Extended Properties=Excel 8.0;Persist Security Info=False';//"Excel 8.0;IMEX=1"
ADOConnection1.Connected := True;
a := GetTickCount;
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('Select * from [Sheet1$]');
ADOQuery1.Open;
b := GetTickCount;
DBGridEh1.DataSource:=DataSource1;
for i:=0 to DBGridEh1.Columns.Count-1 do
DBGridEh1.Columns.Items[i].Width := 100;//設定列寬
//ShowMessage(IntToStr(a));
//ShowMessage(IntToStr(b));
//ShowMessage(inttostr(ADOQuery1.RecordCount));
//StatusBar1.Panels[0].Text:=('讀取EXCEL表資料成功,讀取共:'+inttostr(ADOQuery1.RecordCount)+' 條,耗時:'+floattostr((b-a)/1000)+'秒');
sl:='讀取EXCEL表資料成功,讀取共:'+inttostr(ADOQuery1.RecordCount)+' 條,耗時:'+floattostr((b-a)/1000)+'秒';
Application.MessageBox(PWidechar(sl),'提示',32);
// ShowMessage('讀取EXCEL表資料成功,讀取共:'+inttostr(ADOQuery1.RecordCount)+' 條,耗時:'+floattostr((b-a)/1000)+'秒');
end
end;
procedure Tlenordtefr.dxBarLargeButton2Click(Sender: TObject);
var
i : integer;
sl:string;
begin
if messagebox(Self.Handle,'你確定要上傳表格的資料至服務器嗎?','系統提示',mb_yesno+mb_iconquestion)=idyes then
with ADOQuery2 do
begin
Close;
sql.Clear;
SQL.Add('select MAX(發貨倉庫) as YDNO from lenordte'); //lenordte判斷這個表內是否有資料
Open;
if FieldByName('YDNO').AsString<>'' then
begin
MessageBox(Self.Handle,'RDC掃描資料臨時匯入表還有未完成的資料,請稍后在提交資料或取消匯入!','系統提示',mb_iconquestion);
end
else
begin
adoquery2.close;
adoquery2.sql.text := 'select * from lenordte where 1=2'; //lenordte 為資料庫表名
adoquery2.open;
if inttostr(AdoQuery1.Fields.Count)=inttostr(AdoQuery2.Fields.Count) then
begin
AdoQuery1.FindFirst;
if not AdoQuery1.isEmpty then
begin
a := GetTickCount;
with AdoQuery1 do while not eof do
begin
AdoQuery2.Append;
for I := 0 to AdoQuery1.fieldCount -1 do
begin
AdoQuery2.fields[i].value := Fields[i].value;
end;
Next;
ADOQuery2.Post;
end;
b := GetTickCount;
//StatusBar1.Panels[0].Text:=('資料上傳成功,共上傳:'+inttostr(ADOQuery1.RecordCount)+' 條,耗時:'+floattostr((b-a)/1000)+'秒');
sl:='資料上傳成功,共上傳:'+inttostr(ADOQuery1.RecordCount)+' 條,耗時:'+floattostr((b-a)/1000)+'秒';
Application.MessageBox(PWidechar(sl),'提示',32);
//ShowMessage('資料上傳成功,共上傳:'+inttostr(ADOQuery1.RecordCount)+' 條,耗時:'+floattostr((b-a)/1000)+'秒');
//StatusBar1.Panels[0].Text:=('匯入資料量:'+inttostr(AdoQuery1.RecordCount));
end;
with ADOQuery3 do //處理代碼
begin
close;
sql.Clear;
sql.Add('exec P_lenord');
ExecSQL;
end;
with ADOQuery1 do
begin
close;
end;
end
else
begin
Application.MessageBox('Excel表欄位與服務器表欄位不一至,請核對后重新提交資料','系統提示', MB_OK + MB_ICONWARNING);
end;
end;
end;
end;
這個是我目前用的,有更好的希望分享一下,謝謝!
uj5u.com熱心網友回復:
告訴你吧:使用D自帶的控制元件,在不同的版本,代碼不一致。所以網上下載的代碼,有時不能實作。uj5u.com熱心網友回復:
請問這個是sql.Add('exec P_lenord');???? P_lenord是存盤程序?
uj5u.com熱心網友回復:
謝謝你的回復哦。你這個是用Access的?
uj5u.com熱心網友回復:
請問這個是sql.Add('exec P_lenord');???? P_lenord是存盤程序?這個存盤程序,只是處理導進資料庫后處理的代碼,這個你可以不用管!
uj5u.com熱心網友回復:

讀取有問題,這會是什么原因
uj5u.com熱心網友回復:
上傳到資料庫也有問題。。求教
uj5u.com熱心網友回復:
把你的原始碼上傳到csdn或地址地方,我給你修改一下
uj5u.com熱心網友回復:
本身是用在acess的。也可以用到excel。對TADOConnection組件進行配置即可。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/108814.html
標籤:語言基礎/算法/系統設計
