procedure TForm1.Button1Click(Sender: TObject);
var
msexcel,msbook,mssheet
levariant;
ma:variant;
userow,usecol,modelx,modely,Totalx,Totaly:integer;
i,j:integer;
arraytemp : array[1..37] of string ;
begin
opdlg.Filter:='Excel文件|*.xls';
if opdlg.Execute then
begin
Msexcel:=createoleobject('excel.application');
try
MSBook:=MsExcel.WorkBooks.Open(opdlg.FileName);
MsExcel.ActiveWorkbook.RunAutoMacros(2);
MsExcel.Visible := false;
Mssheet:=Msbook.Activesheet;
UseRow := MsExcel.ActiveSheet.UsedRange.Rows.Count;
UseCol := MsExcel.ActiveSheet.UsedRange.Columns.Count;
except
on E:Exception do
messagebox(handle,PChar(e.Message),'提示', MB_ICONINFORMATION);
end;
msexcel.activesheet.cells.find(what:='Model',after:=Msexcel.cells[1,1]).activate;
modelx:=msexcel.activecell.row;
modely:=msexcel.activecell.column;
msexcel.activesheet.cells.find(what:='Total',after:=Msexcel.cells[1,1]).activate;
Totalx:=msexcel.activecell.row;
Totaly:=msexcel.activecell.column;
for i:=modely to totaly do
begin
arraytemp
:=trim(MSexcel.activesheet.cells[modelx,i].value);
end;
with query do
begin
close;
sql.Clear;
sql.Text:='select top 0 * from T_IJPmonthPlan' ;
open;
end;
for j:=modelx+1 to userow do
begin
showmessage(inttostr(j));
ma:= MA.Range['A4'].MergeArea;;
showmessage(VarToStr(ma.cells[4,1].value));
if Msexcel.activesheet.rows[j].hidden=true then
continue;
if (trim(MsExcel.activesheet.cells[j,4].value)='D Input') or (trim(MsExcel.activesheet.cells[j,4].value)='N Input') then
for i:=modely to totaly-4 do
begin
if (arraytemp[i+4]='') or (arraytemp[i+4]='Total') then
continue;
query.Insert;
query.FieldByName('model').AsString:=trim(Msexcel.activesheet.cells[j,modely].value);
query.FieldByName('VerItem').AsString:=trim(Msexcel.activesheet.cells[j,modely+1]);
query.FieldByName('Version').AsString:=trim(Msexcel.activesheet.cells[j,modely+2].value);
query.FieldByName('ShifT').AsString:=trim(Msexcel.activesheet.cells[j,modely+3]);
query.FieldByName('Month/Date').AsDateTime:=strtodate(arraytemp[i+4]);
query.Post;
end
else
continue;
end;
end;
MsExcel.displayalerts:=false;
MsExcel.Quit;
MsExcel := Unassigned;
MsExcel := Null;
MSBook := Null;
MSSheet := Null;