uses comobj
procedure MergeCells(FileName: string);
function GetRepRange(x, y: integer): string;
var fX, fY: string;
begin
if y <= 0 then
fX := 'A';
if y <= 26 then
fX := chr(64 + y);
if y > 26 then
fX := chr(64 + (y div 26)) + chr(64 + (y mod 26));
fY :=
IntToStr (x);
Result := fX + fY;
end;
var //
i, j: integer;
s1, s2: string;
Excel: Variant;
WorkBook: Variant;
xl: olevariant;
begin
Excel := CreateOLEObject('Excel.Application');
Excel.WorkBooks.Open(FileName);
Excel.WorkSheets[1].Activate;
WorkBook := Excel.ActiveWorkBook;
try
Excel.Application.ScreenUpdating := False;
Excel.Application.DisplayAlerts := False;
j := 1;
while
Trim (Excel.Cells[j, 1].Value) <> '' do begin
s1 := Trim(Excel.Cells[j, 1].Value);
s2 := Trim(Excel.Cells[j + 1, 1].Value);
i := j + 1;
while s1 = s2 do begin
Inc(i);
s2 := Trim(Excel.Cells[i, 1].Value);
end;
Dec(i);
Excel.Range[GetRepRange(j, 1) + ':' + GetRepRange(i, 1)].Merge(xl);
j := i + 1;
end;
finally
Excel.ActiveWorkBook.Save;
Excel.DisplayAlerts := False;
WorkBook.Close;
Excel.DisplayAlerts := False;
Excel.Quit;
Excel := Unassigned;
VarClear(Excel);
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
MergeCells('C:\01.xlsx');
end;