destructor TTargetFile.Destroy;
begin
GetSheets.Free;
GetColsNames.Free;
inherited;
end;
procedure TTargetFile.GetColNames(cols: integer);
var
FirstCharIndex,SecondCharIndex,i,j: integer;
begin
GetColsNames.Clear;
FirstCharIndex:= Cols div 26;
SecondCharIndex:= Cols mod 26;
for i:=0 to SecondCharIndex-1 do
GetColsNames.Add(chr(65+i));
if FirstCharIndex>0 then
begin
for i:=0 to FirstCharIndex-1 do
for j:=0 to SecondCharIndex-1 do
begin
GetColsNames.Add(chr(65+i)+chr(65+j));
end;
end;
end;
procedure TTargetFile.InsertExcel(NumColumns: integer; FileName: string);
var
vActiveWorkBook: Variant;
i: integer;
begin
try
try
if VarIsEmpty(vActiveWorkBook) = True then
ExcelApp := CreateOleObject( 'Excel.Application' );
except
ShowMessage('您的机器里未安装Microsoft Excel');
end;
ExcelApp.WorkBooks.Open(FileName);
vActiveWorkBook:=ExcelApp.ActiveWorkBook;
if not(SetActiveSheet='') then
ExcelApp.WorkSheets[SetActiveSheet].Activate;
ExcelApp.ActiveSheet.Columns[NumColumns].Insert;
GetFileRows:= ExcelApp.ActiveSheet.UsedRange.Rows.Count;
ExcelApp.Cells[1,1].Value:= '临时ID';
for i:=2 to GetFileRows do
ExcelApp.Cells[i,1].Value:= IntToStr(i-1);
finally
vActiveWorkBook.close ;
vActiveWorkBook:= Unassigned;
ExcelApp.Quit;
ExcelApp:=Unassigned;
end;
end;
procedure TTargetFile.Refresh(TargetFile: string);
var
vActiveWorkBook: Variant;
i: integer;
begin
GetTargetFile:= TargetFile;
try
try
if VarIsEmpty(vActiveWorkBook) = True then
ExcelApp := CreateOleObject( 'Excel.Application' );
except
ShowMessage('您的机器里未安装Microsoft Excel');
end;
ExcelApp.WorkBooks.Open(GetTargetFile);
vActiveWorkBook:=ExcelApp.ActiveWorkBook;
GetSheetsCount:= vActiveWorkBook.Sheets.Count;
Sheets.Clear;
for i:=1 to GetSheetsCount do
GetSheets.Add(vActiveWorkBook.Sheets[i].Name);
if not(SetActiveSheet='') then
ExcelApp.WorkSheets[SetActiveSheet].Activate;
GetFileCols:= ExcelApp.ActiveSheet.UsedRange.Columns.Count;
GetFileRows:= ExcelApp.ActiveSheet.UsedRange.Rows.Count;
//GetLastLine:=
GetColNames(GetFileCols);
finally
vActiveWorkBook.close ;
vActiveWorkBook:= Unassigned;
ExcelApp.Quit;
ExcelApp:=Unassigned;
end;
end;