由于CSV檔案的格式比較特殊,用MEMO.LOADFROM無果以后。
換ADO操作,代碼如下,求大神指導!XP下使用無任何問題,但是在64位下就提示不能使用'';檔案已在使用中。
主要就是實作CSV匯入EXCEL或者SQL SERVER,匯入SQL里面也提示錯誤
procedure TForm1.Button6Click(Sender: TObject);
var
strfile,strdata,strdir,strconn2,strdatabase:string;
ExcelApp: Variant;
i:Integer;
begin
if (opendialog1.Execute) and fileExists(opendialog1.FileName) then
begin
strdata:=opendialog1.FileName;//獲得路徑
Edit2.Text:='';
Edit2.Text:=ExtractFileName(strdata); //獲得檔案名稱
strdir:=ExtractFileDir(opendialog1.FileName); //獲得上級目錄路徑
strfile:=ExtractFilePath(Application.Exename)+'data.xls';
strconn2:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='
+strdir+';Extended Properties="text;Hdr=No;imex=1";Persist Security Info=False';
//showmessage(strconn2);
strdatabase:='select * from ' + ChangeFileExt(ExtractFileName(strdata),'')+'#csv';
//showmessage(strdatabase);
ADOConnection2.ConnectionString:=strconn2;
ADOConnection2.LoginPrompt:=false;
ADOConnection2.Connected:=true;
i:=1;
try
ADOConnection1.Connected:=false;
ExcelApp := CreateOleObject('Excel.Application');
ExcelApp.Visible := False;
ExcelApp.WorkBooks.Open(strfile);
ExcelApp.WorkSheets[1].Activate;
ExcelApp.ActiveSheet.Rows['2:60000'].Delete;
ExcelApp.Application.DisplayAlerts := False;
with ADOQuery3 do
begin
close;
sql.Text:=strdatabase;
open;
first;
if Recordcount<=0 then
exit
else
begin
//showmessage(ADOQuery3.Fields[0].Value);
//showmessage(inttostr(conI));
while not eof do
begin
ExcelApp.WorkSheets[1].Cells[i+1,1].Value:=ADOQuery3.Fields[0].Value;
ExcelApp.WorkSheets[1].Cells[i+1,2].Value:=ADOQuery3.Fields[1].Value;
ExcelApp.WorkSheets[1].Cells[i+1,3].Value:=ADOQuery3.Fields[2].Value;
ExcelApp.WorkSheets[1].Cells[i+1,4].Value:=ADOQuery3.Fields[3].Value;
ExcelApp.WorkSheets[1].Cells[i+1,5].Value:=ADOQuery3.Fields[4].Value;
ExcelApp.WorkSheets[1].Cells[i+1,6].Value:=ADOQuery3.Fields[5].Value;
ExcelApp.WorkSheets[1].Cells[i+1,7].Value:=ADOQuery3.Fields[6].Value;
ExcelApp.WorkSheets[1].Cells[i+1,8].Value:=ADOQuery3.Fields[7].Value;
ExcelApp.WorkSheets[1].Cells[i+1,9].Value:=StrToInt(ADOQuery3.Fields[8].Value);
ExcelApp.WorkSheets[1].Cells[i+1,10].Value:=ADOQuery3.Fields[9].Value;
ExcelApp.WorkSheets[1].Cells[i+1,11].Value:= strtofloat(ADOQuery3.Fields[10].Value);
ExcelApp.WorkSheets[1].Cells[i+1,12].Value:= ADOQuery3.Fields[11].Value;;
ExcelApp.WorkSheets[1].Cells[i+1,13].Value:= strtofloat(ADOQuery3.Fields[12].Value);
ExcelApp.WorkSheets[1].Cells[i+1,14].Value:= ADOQuery3.Fields[13].Value;
ExcelApp.WorkSheets[1].Cells[i+1,15].Value:=ADOQuery3.Fields[14].Value;
ExcelApp.WorkSheets[1].Cells[i+1,16].Value:=ADOQuery3.Fields[15].Value;
ExcelApp.WorkSheets[1].Cells[i+1,17].Value:=ADOQuery3.Fields[16].Value;
ExcelApp.WorkSheets[1].Cells[i+1,18].Value:=ADOQuery3.Fields[17].Value;
ExcelApp.WorkSheets[1].Cells[i+1,19].Value:=ADOQuery3.Fields[18].Value;
ExcelApp.WorkSheets[1].Cells[i+1,20].Value:=ADOQuery3.Fields[19].Value;
ExcelApp.WorkSheets[1].Cells[i+1,21].Value:=ADOQuery3.Fields[20].Value;
ExcelApp.WorkSheets[1].Cells[i+1,22].Value:=ADOQuery3.Fields[21].Value;
ExcelApp.WorkSheets[1].Cells[i+1,23].Value:=ADOQuery3.Fields[22].Value;
ExcelApp.WorkSheets[1].Cells[i+1,24].Value:=ADOQuery3.Fields[23].Value;
next;
i:=i+1;
end;
end;
close;
ADOConnection2.Connected:=false;
end;
if not ExcelApp.ActiveWorkBook.Saved then ExcelApp.ActiveWorkBook.Save;
Try
ExcelApp.WorkBooks.Close;
ExcelApp.Quit;
finally
ExcelApp:= unassigned;
end;
Except
Try
ExcelApp.Quit;
finally
ExcelApp:= unassigned;
end;
end;
ADOConnection1.Connected:=true;
if not ADOQuery1.active then ADOQuery1.open;
if not ADOQuery2.active then ADOQuery2.open;
j:=1;
end;
end;
uj5u.com熱心網友回復:
?錯誤提示什么????uj5u.com熱心網友回復:
提示不能使用'';檔案已在使用中。
uj5u.com熱心網友回復:
ADOConnection設定為獨占模式 Share Exclusiveuj5u.com熱心網友回復:
感覺問題出在驅動上,你查一下XP和Win7 的資料連接驅動吧另外為什么操作CSV要繞這么遠呢,直接StringList加載進來然后用逗號分隔符逐個處理不好嗎?
uj5u.com熱心網友回復:
最開始 也是STRINGLIST處理資料,但是發現好像不能得到理想的要求。哪我再研究研究!
uj5u.com熱心網友回復:
CSV需要使用TEXT驅動獨占模式它跟EXCEL的格式是不一樣的,你在CONNECTION的所有屬性那里看看
uj5u.com熱心網友回復:
連接字串這樣寫應該就可以了:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\a.csv;Mode=Share Deny Read|Share Deny Write;Extended Properties=Text;Persist Security Info=False
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/42784.html
標籤:數據庫相關
