导出Excel类

发表于:2007-06-11来源:作者:点击数: 标签:
procedure TClassForm.ClassToExcel; var ExcelObj, Excel, WorkBook, Sheet: OleVari ant ; OldCursor:TCursor; Row,Col: Integer; begin if not (adsClasses.Active and adsMaster.Active and adsTeachers.Active and adsCadres.Active) then exit; OldCur

procedure TClassForm.ClassToExcel;

var

ExcelObj, Excel, WorkBook, Sheet: OleVariant;

OldCursor:TCursor;

Row,Col: Integer;

begin

if not (adsClasses.Active and adsMaster.Active and adsTeachers.Active and adsCadres.Active) then

exit;

OldCursor:=Screen.Cursor;

Screen.Cursor:=crHourGlass;

try

ExcelObj := CreateOleObject('Excel.Sheet');

Excel := ExcelObj.Application;

Excel.Visible := True;

WorkBook := Excel.Workbooks.Add ;

Sheet:= WorkBook.Sheets[1];

except

MessageBox(GetActiveWindow,'无法调用Mircorsoft Excel! '+chr(13)+chr(10)+

'请检查是否安装了Mircorsoft Excel。','提示',MB_OK+MB_ICONINFORMATION);

Screen.Cursor:=OldCursor;

Exit;

end;

try

//班级

Row := 1;

Col := 1;

StringToExcelSheet(DataModuleStudents.GetClassName,Row,Col+1,Sheet);

//班主任

Row := Row + 1;

StringToExcelSheet('班主任:',Row,Col,Sheet);

Col := Col + 1;

StringToExcelSheet(DataModuleStudents.adsMaster.FieldByName('班主任姓名').AsString,Row,Col,Sheet);

//老师

Row := Row + 1;

Col := 1;

StringToExcelSheet('老师:',Row,Col,Sheet);

Row := Row + 1;

DataSetToExcelSheetEx(DataModuleStudents.adsTeachers,Row,Col,Sheet);

//班干部

Row := Row + DataModuleStudents.adsTeachers.RecordCount + 1;

StringToExcelSheet('班干部:',Row,Col,Sheet);

Row := Row + 1;

DataSetToExcelSheetEx(DataModuleStudents.adsCadres,Row,Col,Sheet);

//备注

Row := Row + DataModuleStudents.adsCadres.RecordCount + 1;

StringToExcelSheet('备注:',Row,Col,Sheet);

Row := Row + 1;

MemoFieldtoExcelSheet(DataModuleStudents.adsClasses.FieldByName('备注'),Row,Col,Sheet);

finally

Screen.Cursor := OldCursor;

end;

end;



原文转自:http://www.ltesting.net

...