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.
...全文
45 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengxiang5160 2003-09-30
  • 打赏
  • 举报
回复
在ExcelApplication1.Visible[0]:=True;之前加上
ExcelApplication1.disconnect;
excelapplication1.connect;
甜而不腻 2003-09-29
  • 打赏
  • 举报
回复
说明:
//***********************************************//
str1:=advstringgrid1.Cells[1,i-1]; //第一列
str2:=advstringgrid1.Cells[2,i-1]; //第二列
str3:=advstringgrid1.Cells[3,i-1]; //第三列
str4:=advstringgrid1.Cells[4,i-1]; //第四列
//**********************************************//
这里advstringgrid1如果不能显示第一行,调整一下 i-1或i-2
因为这里的行和列跟EXCEL里的正好相反.
甜而不腻 2003-09-29
  • 打赏
  • 举报
回复
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];
//***********************************************//
str1:=advstringgrid1.Cells[1,i-1]; //第一列
str2:=advstringgrid1.Cells[2,i-1]; //第二列
str3:=advstringgrid1.Cells[3,i-1]; //第三列
str4:=advstringgrid1.Cells[4,i-1]; //第四列

with AdoQuery1 do
begin
close;
sql.Clear;
sql.Add('insert into joy(field1,field2,field3,field4) values(:pam1,:pam2,:pam3,:pam4)') ;
ParamByName('pam1').AsString:=str1 ;
ParamByName('pam2').AsString:=str2;
ParamByName('pam3').AsString:=str3;
ParamByName('pam4').AsString:=str4;
Prepare;
execsql;
end;


//**********************************************//
end

说明:
str1,str2,str3,str4,变量
你的表里不一定全是字符型,所以你可以根据情况进行转换类型,在STRingGRID里的都是字符型的.
ADOQUERY1是ADO数据集.

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧