導出Excel類

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;

發佈了39 篇原創文章 · 獲贊 9 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章