Where is my way?

 

Delphi工作笔记-从wps表格导入数据更新

网上看了一篇文章.简直是坑爹!

uses comobject;
procedure TBaseMaterielPreForm.Button1Click(Sender: TObject);

const

BeginRow
= 3; BeginCol = 2;
//开始列 开始行
//列对应A B C D E F G
//行对应 1 2 3 4 4 5

var

Excel: OleVariant;

iRow,iCol : integer;

xlsFilename:
string;

buffer:PChar;

begin

OpenDialog1.Title :
= '请选择正确的excel文件';

OpenDialog1.Filter :
= 'Excel(*.xls)|*.xls';

if OpenDialog1.Execute then

begin

if ExtractFileExt(OpenDialog1.FileName) <> '.xls' then

begin

MessageBox(
0, '请选择正确的excel文件',PChar('提示'),MB_OK or MB_ICONWARNING);

exit;

end;

end;

xlsFilename :
= OpenDialog1.FileName;

try
//office Excel 表格
//Excel := CreateOLEObject('Excel.Application');

///except

// Application.MessageBox('excel没有安装', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);

// try
//wps表格
Excel :
= CreateOLEObject('et.Application');

except

Application.MessageBox(
'excel没有安装', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);

Exit;

// end;



end;

Excel.Visible :
= true;

Excel.WorkBooks.Open(xlsFilename);

try



iRow :
= BeginRow;

iCol :
= BeginCol;



while trim(Excel.WorkSheets['sheet1'].Cells[iCol,iRow].value) <> '' do

begin
[行,列
+偏移植]
cmd.Parameters[
1].Value := trim(Excel.WorkSheets[1].Cells[iCol,iRow].value);

//cmd.Parameters[2].Value := trim(Excel.WorkSheets[1].Cells[iRow,iCol+2].value);

cmd.Parameters[
2].Value := trim(Excel.WorkSheets[1].Cells[iCol,iRow+2].value);

cmd.Parameters[
0].Value := trim(Excel.WorkSheets[1].Cells[iCol,iRow+3].value);

cmd.Execute;

iCol :
= iCol + 1;

end;

Excel.Quit;

except

Application.MessageBox(
'导入数据出错', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);

Excel.Quit;

end;

MessageBox(GetActiveWindow(),
'数据导入成功', '提示信息', MB_OK +

MB_ICONWARNING);

inherited;



end;

  

posted on 2011-09-19 15:29  ManLoveGirls  阅读(2233)  评论(0编辑  收藏  举报

导航