如何把簡體的EXCEL表格的內容導入到數據庫的表中啊? ( 积分: 100 )

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

terran0775

Unregistered / Unconfirmed
GUEST, unregistred user!
我要在繁體中文的系統中把簡體的EXCEL表格的內導導進DELPHI的GRID中,讓用戶選擇導入哪些記錄,請問有何良法實現啊?
 
我要在繁體中文的系統中把簡體的EXCEL表格的內導導進DELPHI的GRID中,讓用戶選擇導入哪些記錄,請問有何良法實現啊?
 
procedure ExcelToDb;
var
Excel: Variant;
i, j: Integer;
begin
Excel := CreateOleObject('Excel.Application');
Excel.Add('C;/Book1.xls');
for i:=0 to 20 do //假设有20行和20列数据的Excel文件
for j:=0 to 20 do
StringGrid1.Cells[j,i] := Excel.Cells[i,j].Value;
Excel.Sheet.Close;
Excel.Quit;
end;
 
是要在繁體中文的系統中把簡體的EXCEL表格的內導導進DELPHI的GRID中,不是單純的數據導入啊,請問有什麼控件或方法嗎?
 
系统识别简体的话,就直接导,否则就得转化
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls,ComObj, ComCtrls,AdoConED;

type
TForm1 = class(TForm)
Button1: TButton;
ADOConnection1: TADOConnection;
OpenDialog1: TOpenDialog;
Edit1: TEdit;
Label1: TLabel;
ADOConnection2: TADOConnection;
ComboBox1: TComboBox;
Button3: TButton;
SBar: TStatusBar;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
OpenDialog1.Execute;
ADOConnection1.Connected := false;
ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source="'+opendialog1.FileName+'";Extended Properties=Excel 8.0;Persist Security Info=False';
ADOConnection1.GetTableNames(combobox1.items);
SBar.Panels[0].Text:=opendialog1.FileName;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
s:string;
begin
if edit1.Text ='' then
begin
showmessage('表名不能為空!');
exit;
end;
if combobox1.Text ='' then
begin
showmessage('請選擇要導入的表名!');
exit;
end;
with AdoConnection2 do
begin
if EditConnectionString(AdoConnection2) then
ADOConnection2.Connected :=false;
// ADOConnection2.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=dingxin;Data Source=AUDIT-龔波';
s:='select * into "'+edit1.Text+'" from OpenDataSource(''Microsoft.Jet.OLEDB.4.0'',''Data Source= "'+SBar.Panels[0].Text+'";Extended Properties=Excel 8.0'')...'+combobox1.Text+'';
try
ADOConnection2.Execute(s);
ShowMessage('導入成功');
except
ShowMessage('導入失敗!');
end;
end;
end;

end.
幫個忙。我想把excel導入到SQL用務器中,但是一直有錯誤,查了好久還是沒有找到
OLE DB provider 'Microsoft.Jet OLEDB.4.0' reported an error
 
你這樣導入,沒有指定對應的字段名稱,在導入數據類型不匹配會有錯誤啊
 
select * from opendatasource('Microsoft.Jet.OLEDB.4.0',
'Data Source="路径及文件名";User ID=admin;Password=;Extended properties=Excel 5.0;')...表名
 
還是不行
procedure TForm4.Button3Click(Sender: TObject);
var
s:string;
begin
{ if edit1.Text ='' then
begin
showmessage('表名不能為空!');
exit;
end;
if combobox1.Text ='' then
begin
showmessage('請選擇要導入的表名!');
exit;
end; }
// with AdoConnection2 do
// begin
// if EditConnectionString(AdoConnection2) then
OpenDialog1.Execute;
ADOConnection2.Connected :=false;
ADOConnection2.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=dingxin;Data Source=AUDIT-龔波';
s:='select * into SDF from OpenDataSource(''Microsoft.Jet.OLEDB.4.0'',''Data Source= "'+OpenDialog1.FileName+'" User ID=admin;Password=;Extended Properties=Excel 8.0'')...[A$]';
// select * from opendatasource('Microsoft.Jet.OLEDB.4.0',
//'Data Source="路?及文件名";User ID=admin;Password=;Extended properties=Excel 5.0;')...表名
try
ADOConnection2.Execute(s);
ShowMessage('導入成功');
except
ShowMessage('導入失敗!');
end;
end;
 
怎麼沒有人回答啊?
 
unit UitSQLsave;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, ComCtrls;

type
TForm4 = class(TForm)
SBar: TStatusBar;
ADOC1: TADOConnection;
OpenDialog1: TOpenDialog;
ADOC2: TADOConnection;
Button2: TButton;
Label3: TLabel;
Button4: TButton;
ADOQ2: TADOQuery;
ComboBox1: TComboBox;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Edit1: TEdit;
comexcel: TComboBox;
Button3: TButton;
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FindConnectionString( AADOConn: TADOConnection;AConStr: String);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form4: TForm4;
ConnString: String;

implementation

{$R *.dfm}

procedure TForm4.Button1Click(Sender: TObject);
var
connstr1:string;
begin
If openDialog1.Execute then
begin
connstr1:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=';
connstr1:=connstr1+OpenDialog1.FileName;
connstr1:=connstr1+';Mode=Read;Extended Properties=Excel 8.0;Persist Security Info=False';
ADOC1.ConnectionString:=connstr1;
ADOC1.Connected:=True;
ADOC1.GetTableNames(comexcel.Items,False);
SBar.Panels[1].Text:=opendialog1.FileName;
end;
end;

procedure TForm4.Button3Click(Sender: TObject);
var
s:string;
I:integer;
begin
if edit1.Text ='' then
begin
showmessage('表名不能為空!');
exit;
end;
if comexcel.Text ='' then
begin
showmessage('請選擇要導入的表名!');
exit;
end;
for i := 0 to combobox1.Items.Count-1 do
if trim(edit1.Text)=combobox1.Items.Strings then
begin
showmessage('表名已存在,請重新輸入');
exit;
end;
ADOC2.Connected :=false;
ADOC2.ConnectionString:=ConnString;
s:='select * into ['+trim(edit1.text)+'] from OpenDataSource(''Microsoft.Jet.OLEDB.4.0'',''Data Source="'+SBar.Panels[1].Text+'";Extended Properties=Excel 8.0'')...['+trim(comexcel.Text)+']';
try
ADOC2.Execute(s);
ShowMessage('導入成功');
except
ShowMessage('導入失敗!');
end;
end;

procedure TForm4.FindConnectionString(AADOConn: TADOConnection;
AConStr: String);
begin
try
try
ADOC2.Close;
ConnString:=PromptDataSource(Application.Handle,ADOC2.ConnectionString);
ADOC2.ConnectionString:= ConnString;
AConStr:=ConnString;
finally
ADOC2.Open();
SBar.Panels[0].Text:='數據庫連接成功!';
GroupBox1.Visible:=true;
end;
except
on E: Exception do
MessageBox(Application.Handle,PChar('錯誤信息:' + E.Message),'提示',MB_ICONINFORMATION);

end;
end;

procedure TForm4.Button2Click(Sender: TObject);
begin
FindConnectionString(ADOC2,'');
ADOC2.GetTableNames(combobox1.Items);
end;

procedure TForm4.Button4Click(Sender: TObject);
begin
close;
end;

procedure TForm4.FormClose(Sender: TObject; var Action: TCloseAction);
begin
action:=cafree;
form4:=nil;
end;


end.
這個是把excel導入sql服務器中。。。。。
 
還是沒有回答到點子上,我是要把簡體的EXCEL導入到繁體的SQL SERVER中啊
 
后退
顶部