ingener大哥,請進
beris 2003-09-29 06:32:48 ingener大哥,下面是我實現將excel導入到stringgrid的完整代碼,我的excel文檔和數據庫中的表均是字段名稱相同,且字段數為3.我現在的 問題就是不知道怎麼將stringgrid的值賦給sql server中的目的表joy.老是運行出錯.
我的qq 68434489
mail:beris_fu@163.com
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, Excel2000, OleServer, BaseGrid, AdvGrid;
type
TForm1 = class(TForm)
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
Button1: TButton;
OpenDialog1: TOpenDialog;
AdvStringGrid1: TAdvStringGrid;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var i,j:integer;
begin
opendialog1.InitialDir:=ExtractFileDir(paramstr(0));
opendialog1.Execute;
Try
ExcelApplication1.Connect;
Except
MessageDlg('Excel may not be installed',mtError, [mbOk], 0);
Abort;
End;
ExcelApplication1.Visible[0]:=True;
ExcelApplication1.Caption:='Excel Application';
try
excelapplication1.Workbooks.Open(opendialog1.FileName,
null,null,null,null,null,null,null,null,null,null,null,null,0);
except
begin
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;showmessage('沒有安裝excel');
exit;
end;
end;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
for i:=1 to excelworksheet1.UsedRange[1].rows.Count do
for j:=1 to 4 do
begin
if trim(excelworksheet1.cells.item[i+1,1])<>'' then
begin
advstringgrid1.rowCount:=i+1;
advstringgrid1.Cells[j,i]:=ExcelWorksheet1.Cells.Item[i+1,j];
end
else
begin
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
exit;
end;
end;
end;
end.