看我写的这段程序,送分来把,还加了个画线,呵呵
Procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
PathName: string;
oldcur: tcursor;
FExcelApplication: TExcelApplication;
FExcelWorkbook: TExcelWorkbook;
FExcelWorksheet: TExcelWorksheet;
xl
levariant;
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;