怎么样在DEPHI里写程序实现excel里两个单元格的合并(100分)

  • 主题发起人 主题发起人 swallowljh
  • 开始时间 开始时间
S

swallowljh

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么样在DEPHI里写程序实现excel里两个单元格的合并
或向一个已经存在的EXCEL文件里面写入内容并保存为另一个文件。
 
转贴:
山泉 (2001-8-19 14:04:00)
看我写的这段程序,送分来把,还加了个画线,呵呵

Procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
PathName: string;
oldcur: tcursor;
FExcelApplication: TExcelApplication;
FExcelWorkbook: TExcelWorkbook;
FExcelWorksheet: TExcelWorksheet;
xl:olevariant;
begin

PathName := ExtractFilePath(Application.ExeName);
if FileExists(pathname + 'test.xls') then
try
DeleteFile(pathname + 'test.xls');
except
end;

if queryprint.Active = true then queryprint.Close;
queryprint.CommandText := 'select * from mainwork where 1=1';
queryprint.open;
begin
try
FExcelApplication := TExcelApplication.Create(nil);
FExcelWorkbook := TExcelWorkbook.Create(nil);
FExcelWorkSheet := TExcelWorksheet.Create(nil);

except
showmessage('对不起,您没有安装Excel 2000!');
abort;
end;
end;
begin

FExcelApplication.Visible[0] := True;
FExcelApplication.Connect;
FExcelApplication.Caption := 'test1';
FExcelApplication.Workbooks.Add(PathName + 'juannei.xls', 0);
FExcelWorkbook.ConnectTo(FExcelApplication.ActiveWorkbook);
FExcelWorksheet.ConnectTo(FExcelWorkbook.Activesheet as _WorkSheet);
end;

//写入execl
if Queryprint.RecordCount > 0 then
begin
for i := 1 to (Queryprint.RecordCount) do
begin
try
FExcelWorkSheet.cells.Item[i + 2, 2] := Queryprint.fieldbyname('文件编号').asstring;
FExcelWorkSheet.cells.Item[i + 2, 3] := Queryprint.fieldbyname('责任者').asstring;

//给单元格画上线条
with FExcelApplication.Range['A'+inttostr(i+1),'G'+inttostr(i+1)].Borders do
begin
LineStyle:=xlContinuous;
Weight:=xlThin;
ColorIndex:=xlAutomatic;
end;
//合并单元格子
FExcelApplication.Range['H'+inttostr(i+1),'J'+inttostr(i+1)].Merge(xl);
except
end;
Queryprint.Next;
end;
end;

// xl.Range['H7,H7'].Select;

// xl.ActiveWindow.SelectedSheets.HPageBreaks.add(FExcelApplication.ActiveCell);

FExcelApplication.Disconnect;
FExcelApplication.ScreenUpdating[0] := true;
FExcelWorkbook.Disconnect;
FExcelWorksheet.Disconnect;

FExcelApplication.Quit;

FExcelWorksheet.Free;
FExcelWorkbook.Free;
FExcelApplication.free;

end;

 
1. ExcelApp.ActiveSheet.Range['A1:G1'].merge(true);

2.ExcelApp.SaveAs(SaveDialog1.FileName);
 
多人接受答案了。
 
后退
顶部