将EXCEL表数据导入数据库?(50分)

  • 主题发起人 主题发起人 okzjq
  • 开始时间 开始时间
O

okzjq

Unregistered / Unconfirmed
GUEST, unregistred user!
做一个学籍管理系统,采用ADO+ACCESS,数据库中有一个学生简况表XSJK,字段为:学号,
姓名,民族。。。。,现要将一个外部EXCEL文件的数据导入XSJK表(EXCEL文件第一行为
字段名,各字段号XSJK表相对应)。
请问各位:需要用到什么组件以及怎样导入,能否给点实例代码? 谢谢
 
首先是要取出EXCEL中的数据,再把它保存到数据库中。取出数据的方法有两种:
一种方法是用ADO连接EXCEL文件,把它当作数据库打开,方法如下:
设置属性ConnetionString
选择 Microsoft Jet 4.0 OLE DB provider
Select or enter a datasorce name -> 选择你要打开Excel文件
User name默认是Admin 密码默认为空,可以不用理会
Extended properties 设为:Excel 8.0
sql语句 select * from [yourtablename] (注意要有[])

第二种方法是用OLE方式打开excel并取数,这种方法操作EXCEL的一些例子代码如下(要uses comobj):
var ExcelApp:Variant;
begin
ExcelApp:=CreateOleObject('Excel.Application');
//ExcelApp.visible:=true;
ExcelApp.Caption:='应用程序调用 Microsoft Excel';
ExcelApp.WorkBooks.Add; //新增工作簿
//ExcelApp.workBooks.Open('C:/My Documents/Ca09lin1.xls'); //打开已存在工作簿
ExcelApp.Worksheets[2].activate; //打开第2个工作表
//ExcelApp.WorkSheets['第四章'].activate; //打开名为第四章的工作表
ExcelApp.Cells[1,4].Value:='第一行第四列';
ExcelApp.Cells[1,5].Value:='第一行第五列';
ExcelApp.ActiveSheet.Columns[4].ColumnWidth:=15;
ExcelApp.ActiveSheet.Rows[1].RowHeight:=15;
//ExcelApp.WorkSheets[1].Rows[8].PageBreak:=1; //设置分页符,但似无效
//Excelapp.ActiveSheet.Rows[8].PageBreak:=1; //同上
ExcelApp.ActiveSheet.Range['B3:D4'].Borders[2].Weight:=3;
ExcelApp.ActiveSheet.Range['B3:D4'].Borders[1].Weight:=3;
ExcelApp.ActiveSheet.Range['B3:D4'].Borders[3].Weight:=3;
ExcelApp.ActiveSheet.Range['B3:D4'].Borders[4].Weight:=3;
//ExcelApp.ActiveSheet.Range['B3:D4'].Borders[5].Weight:=3; //会直接在范围内的各Cell内加上斜杠|
//ExcelApp.ActiveSheet.Range['B3:D4'].Borders[6].Weight:=3; //与上句类似
//Bordrs:1-左 2-右 3-顶 4-底 5-斜( / ) 6-斜( / )
ExcelApp.Cells[3,2].Value:='三行二列';
ExcelApp.Cells[3,3].Value:='三行三列';
ExcelApp.Cells[3,4].Value:='三行四列';
ExcelApp.Cells[4,2].Value:='四行二列';
ExcelApp.Cells[4,3].Value:='四行三列';
ExcelApp.Cells[4,4].Value:='四行四列';
//ExcelApp.ActiveSheet.Range['B3:D4'].Value.CopyToClipBoard;
ExcelApp.activeSheet.Cells[1,4].ClearContents; //清除一行四列的内容,activeSheet可以省略
Excelapp.Rows[3].font.Name:='隶书'; //这里Rows前省略了activeSheet,但针对也只是当前工作表而非整个工作簿
ExcelApp.Rows[3].font.Color:=clBlue;
ExcelApp.Rows[3].Font.Bold:=True;
ExcelApp.Rows[3].Font.UnderLine:=True;
ExcelApp.Range['B3:D4'].Copy;
RichEdit1.PasteFromClipboard;
//ExcelApp.ActiveSheet.PageSetup.CenterFooter:='第$P页';
//所有页面设置(PageSetup的属性)都不能进行,不知为何
//ExcelApp.ActiveSheet.PrintPreview; //打印预览
//ExcelApp.ActiveSheet.PrintOut; //直接打印输出
//if not ExcelApp.ActiveWorkBook.Saved then //工作表保存:
// ExcelApp.ActiveSheet.PrintPreview;
//ExcelApp.SaveAs( 'C:/Excel/Demo1.xls' ); //工作表另存为
ExcelApp.ActiveWorkBook.Saved := True; // 放弃存盘
ExcelApp.WorkBooks.Close; //关闭工作簿
ExcelApp.Quit; //退出 Excel
end;
 
用控件不知道,你可以读出excel内容再存到库里。
读excel

procedure TF_Main.ToolButtonExcelClick(Sender: TObject);
var
OpenDialog:TOpenDialog;
iCount:integer;
begin
if ShowIYN('是否真的要导入Excel数据,要生的数据将被删除?')<>IDCANCEL then
try
OpenDialog:=TOpenDialog.Create(Self);
OpenDialog.DefaultExt := 'xls';
OpenDialog.Filter := 'Microsoft Excel 文件 (*.xls)|*.xls';
OpenDialog.Execute;
if OpenDialog.FileName<>'' then
try
Screen.Cursor:=crSqlWait;
ADOConExcel.Close;
ADOConExcel.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data
Source='
+OpenDialog.FileName+';Extended
Properties=Excel 8.0;Persist Security Info=False';
ADOConExcel.Open;
with ADOQryExcel do
begin
Close;
Sql.Clear;
Sql.Add('SELECT 材料编号,百平米用量,预算价,最低价 FROM
[Sheet1$]');
Prepared:=true;
Open;
end;
iCount:=ADOQryExcel.RecordCount;
ADOConExcel.BeginTrans;
with F_DataModule.ADOCom do
begin
CommandText:='DELETE FROM MatPriCal WHERE
cYearMonth='+''''+FormatDateTime('yyyymm',DateTimePicker.DateTime)+'''';
Execute;
end;
ADOQryExcel.First;
while not ADOQryExcel.Eof do
begin
with F_DataModule.ADOCom do
begin
CommandText:='INSERT INTO MatPriCal
VALUES('+''''+FormatDateTime('yyyymm',DateTimePicker.DateTime)+''''+','+''
''+Trim(ADOQryExcel.FieldByName('材料编号
').AsString)+''''+','+Trim(ADOQryExcel.FieldByName('百平米用量
').AsString)+','+Trim(ADOQryExcel.FieldByName('预算价
').AsString)+','+Trim(ADOQryExcel.FieldByName('最低价').AsString)+')';
Execute;
end;
ADOQryExcel.Next;
F_Main.StatusBar.Refresh;
F_Main.StatusBar.Panels[2].text:='当前数
:'+IntToStr(ADOQryExcel.RecNo)+'(共'+IntToStr(iCount)+'条)';
end;
ADOConExcel.CommitTrans;
ShowWIE(2,'导入成功!');
except
ADOConExcel.RollbackTrans;
ShowWIE(2,'导入失败!');
end;
finally
ADOQryExcel.Close;
ADOConExcel.Close;
OpenDialog.Free;
Screen.Cursor:=crDefault;
F_Main.StatusBar.Panels[2].Text:='';
end;
end;

或用sql 自带的openrowset,参考程序
if ShowIYN('是否真的要导入Excel数据?')<>IDCANCEL then
try
ADOConInfoExcel.Open;
OpenDialog:=TOpenDialog.Create(Self);
OpenDialog.DefaultExt := 'xls';
OpenDialog.Filter := 'Microsoft Excel 文件 (*.xls)|*.xls';
OpenDialog.Execute;
if OpenDialog.FileName<>'' then
try
with ADOQryInfoExcel do
begin
Close;
Sql.Clear;
Sql.Add(' SELECT TOP 1 * FROM OPENROWSET');
Sql.Add('(');
Sql.Add('''MSDASQL.1''');
Sql.Add(',');
Sql.Add('''DRIVER=Microsoft Excel Driver (*.xls);');
Sql.Add('DBQ='+OpenDialog.FileName+'''');
Sql.Add(',');
Sql.Add('''SELECT * FROM [Sheet1$]''');
Sql.Add(')');
Open;
end;
if Copy(ADOQryInfoExcel.Fields[2].AsString,1,4)<>mUser.UserArea
then
begin
ShowWIE(1,'不能导入其它地区数据!');
ADOQryInfoExcel.Close;
ADOConInfoExcel.Close;
exit;
end;
//ADOConInfoExcel.BeginTrans;
with ADOComInfoExcel do
begin
CommandText:=' UPDATE C';
CommandText:=CommandText+' SET C.MNo=A.类别编码,C.MName=A.材料名称
,C.spec=A.材料规格,C.unit=A.材料单位,C.Factory=A.生产厂家,C.TradeMark=A.厂
牌,C.Price=A.材料单价,C.pingyin=A.材料拼音,C.hs=A.换算系数 FROM
InfoPrice'+mUser.UserArea;
CommandText:=CommandText+' C, OPENROWSET';
CommandText:=CommandText+'(';
CommandText:=CommandText+'''MSDASQL.1''';
CommandText:=CommandText+',';
CommandText:=CommandText+'''DRIVER=Microsoft Excel Driver
(*.xls);';
CommandText:=CommandText+'DBQ='+OpenDialog.FileName+'''';
CommandText:=CommandText+',';
CommandText:=CommandText+'''SELECT * FROM [Sheet1$]''';
CommandText:=CommandText+') A WHERE C.RDate=A.发布日期 AND
C.RNO=A.发布编号 AND C.Area=A.地区编码';
Execute;
CommandText:=' INSERT INTO InfoPrice'+mUser.UserArea;

CommandText:=CommandText+'(RDate,RNO,Area,MNo,MName,spec,unit,Factory,Trad
eMark,Price,pingyin,hs) ';
CommandText:=CommandText+' SELECT 发布日期,发布编号,地区编码,类别
编码,材料名称,材料规格,材料单位,生产厂家,厂牌,材料单价,材料拼音,换算系数
FROM OPENROWSET';
CommandText:=CommandText+'(';
CommandText:=CommandText+'''MSDASQL.1''';
CommandText:=CommandText+',';
CommandText:=CommandText+'''DRIVER=Microsoft Excel Driver
(*.xls);';
CommandText:=CommandText+'DBQ='+OpenDialog.FileName+'''';
CommandText:=CommandText+',';
CommandText:=CommandText+'''SELECT * FROM [Sheet1$]''';
CommandText:=CommandText+') A';
CommandText:=CommandText+' WHERE NOT EXISTS(SELECT 1 FROM
InfoPrice'+mUser.UserArea+' C';
CommandText:=CommandText+' WHERE C.RDate=A.发布日期 AND C.RNO=A.发
布编号 AND C.Area=A.地区编码)';
Execute;
end;
//ADOConInfoExcel.CommitTrans;
RefreshGridData;
ShowWIE(2,'导入成功!');
except
//ADOConInfoExcel.RollbackTrans;
ShowWIE(2,'导入失败!');
end;
finally
ADOQryInfoExcel.Close;
ADOConInfoExcel.Close;
OpenDialog.Free;
end;


 
首先感谢上面两位的指点!
我按上面的代码稍做改动后,如下:
procedure TForm1.Button1Click(Sender: TObject);
var
OpenDialog:TOpenDialog;
iCount:integer;
filename:String;
begin
if MessageDlg('是否真的导入Excel数据?',mtConfirmation,[mbYes,mbNo],0)
=mrYes then
try
openDialog:=TOpenDialog.Create(self);
openDialog.DefaultExt:='xls';
openDialog.Filter:='Microsof Excel 文件(*.xls)|*.xls';
openDialog.Execute;

if openDialog.FileName<>'' then
try
fileName:=ExtractFileName(OpenDialog.FileName);
Screen.Cursor:=crSqlWait;
ExcelConn.Close;
ExcelConn.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+openDialog.FileName+';Extended Properties=Excel 8.0;Persist Security Info=False';
Excelconn.Open();

With ExcelQuery do
begin
Close;
SQl.Clear;
sql.Add('select number,name from [Sheet1$]');
//上面一句改为sql.Add('select number,name from [filename]'),也出现同样的错误
Prepared:=true;
Open;
end;

iCount:=ExcelQuery.RecordCount;
ADOQuery1.Last;
Excelconn.BeginTrans;
ExcelQuery.First;

while not ExcelQuery.Eof do
begin
with adoCommand1 do
begin
CommandText:='Insert into xsjk values('
+''''+Trim(ExcelQuery.FieldByName('Number').AsString)+''''+','
+''''+Trim(ExcelQuery.FieldByName('Name').AsString)+''''+')';
Execute;
end;
ExcelQuery.Next;
form1.StatusBar1.Refresh;
statusbar1.Panels[1].Text:='当前数:'+IntToStr(ExcelQuery.RecNo)
+'(共'+IntToStr(iCount)+'条)';
end;

ExcelConn.CommitTrans;
MessageDlg('数据导入成功!',mtInformation,[mbOK],0);
except
ExcelConn.RollbackTrans;
MessageDlg('数据导入失败!',mtInformation,[mbOK],0);
end;

finally
ExcelQuery.Close;
ExcelConn.Close;
OpenDialog.Free;
screen.Cursor:=crDefault;
StatusBar1.Panels[1].Text:='';
end;

end;

//上面ExcelConn为ADOConnection组件名,ExcelQuery则为ADOQuery.但该程序运行时
显示:project1.exe raised exception class TDatabaseError with message"Missing
Connection or connectionString.".

请问我的程序中,哪里出错呢? 谢谢!!
 
谢谢两位的帮助
 
后退
顶部