导出Excel类

这段Delphi代码实现了将班级、班主任、老师、班干部等数据导出到Excel的功能。代码中创建了Excel对象,若无法调用Excel会给出提示。接着将不同类型的数据依次写入Excel工作表的相应位置,最后恢复鼠标光标状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值