如题....

解决方案 »

  1. 是操作Ms Office的控件吗?
    此类控件一般都是对MS Office OLE 接口的封装,如D7中自带的Office XP控件(控件面板中默认在Servers页)当然完全可以直接操作OLE接口
      

  2. LZ大哥:isMsOffice是我上一次以ole方式寫ms excel定義的一個boolean變量,不是控件啊
    procedure TCQ_JBDAY_REPORT_F.ExportToMonthsReportToIndirect; //導出加班報表
    var
      iLoop,n:Integer;
      ExcelApp:Variant;
      isMsOffice:Boolean;
    begin
      try
        Application.ProcessMessages;
        ExcelApp:=CreateOleObject('Excel.Application');
        isMsOffice:=True;
      except
        try
          objServiceManager:=CreateOLEObject('com.sun.star.ServiceManager');
          objDesktop:=objServiceManager.createInstance('com.sun.star.frame.Desktop');
          Application.ProcessMessages;
          oDocument:=objDesktop.loadComponentFromURL('private:factory/scalc','_blank',0,VarArrayOf([]));
          oSheets:=oDocument.Sheets;
          isMsOffice:=False;
        except
          MessageDlg('對不起,你電腦上沒有安裝MicroSoftOffice Excel或OpenOffice Calc,所以不能導出報表!',mtWarning,[mbOK],0);
          Exit;
        end;
      end;  if isMsOffice then   //MSoffice
      begin
        try
          ExcelApp.Caption:='加班報表';
          ExcelApp.Visible:=False;
          ExcelApp.WorkBooks.Add;
          ExcelApp.WorkBooks[1].WorkSheets[1].name:='加班時數報表';
          ExcelApp.WorkBooks[1].WorkSheets[1].Activate;
          Screen.Cursor:=crSQLWait;
          Application.ProcessMessages;
          ExcelApp.cells.Interior.ColorIndex:=2;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[1,8].font.name:='新細明體';
          ExcelApp.WorkBooks[1].WorkSheets[1].cells.font.size:=9;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[1,8]:='xxx 公 司 '+ VarToStr(cx_bm.Text) + '  (部)課  '+ VarToStr(cx_xb.Text) + '  (組)線 '+ FormatDateTime('mm',cx_sdt.Date) +'  月  份 加 班 單';
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[1,8].font.bold:=true;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[1,8].font.size:=14;
          ExcelApp.Range['A1:AS1'].MergeCells:=True;
          ExcelApp.Range['A1:AS1'].HorizontalAlignment:=xlCenter;
          Application.ProcessMessages;      ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,1]:='序號';
          ExcelApp.Range['A2:A3'].MergeCells:=True;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,2]:='廠別';
          ExcelApp.Range['B2:B3'].MergeCells:=True;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,3]:='線別';
          ExcelApp.Range['C2:C3'].MergeCells:=True;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,4]:='工號';
          ExcelApp.Range['D2:D3'].MergeCells:=True;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,5]:='姓名';
          ExcelApp.Range['E2:E3'].MergeCells:=True;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,6]:='班次';
          ExcelApp.Range['F2:F3'].MergeCells:=True;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,7]:='直接區分';
          ExcelApp.Range['G2:G3'].MergeCells:=True;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,8]:='機種類別';
          ExcelApp.Range['H2:H3'].MergeCells:=True;
        
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,38]:='工作天數';
          ExcelApp.Range['I2:AM2'].MergeCells:=True;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,39]:='補上月平時';
          ExcelApp.Range['AN2:AN3'].MergeCells:=True;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,40]:='補上月周日';
          ExcelApp.Range['AO2:AO3'].MergeCells:=True;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,41]:='平時合計';
          ExcelApp.Range['AP2:AP3'].MergeCells:=True;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,42]:='周日合計';
          ExcelApp.Range['AQ2:AQ3'].MergeCells:=True;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,43]:='節日合計';
          ExcelApp.Range['AR2:AR3'].MergeCells:=True;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,44]:='合計';
          ExcelApp.Range['AS2:AS3'].MergeCells:=True;
          ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,45]:='加班合計';      ExcelApp.cells[3,9].value:='1';
          ExcelApp.ActiveSheet.Columns[9].ColumnWidth:=3;
          ExcelApp.cells[3,10].value:='2';
          ExcelApp.ActiveSheet.Columns[10].ColumnWidth:=3;
          ExcelApp.cells[3,11].value:='3';
          ExcelApp.ActiveSheet.Columns[11].ColumnWidth:=3;
          ExcelApp.cells[3,12].value:='4';
          ExcelApp.ActiveSheet.Columns[12].ColumnWidth:=3;
          ExcelApp.cells[3,13].value:='5';
          ExcelApp.ActiveSheet.Columns[13].ColumnWidth:=3;
          ExcelApp.cells[3,14].value:='6';
          ExcelApp.ActiveSheet.Columns[14].ColumnWidth:=3;
          ExcelApp.cells[3,15].value:='7';
          ExcelApp.ActiveSheet.Columns[15].ColumnWidth:=3;
          ExcelApp.cells[3,16].value:='8';
          ExcelApp.ActiveSheet.Columns[16].ColumnWidth:=3;
          ExcelApp.cells[3,17].value:='9';
          ExcelApp.ActiveSheet.Columns[17].ColumnWidth:=3;
          ExcelApp.cells[3,18].value:='10';
          ExcelApp.ActiveSheet.Columns[18].ColumnWidth:=3;
          ExcelApp.cells[3,19].value:='11';
          ExcelApp.ActiveSheet.Columns[19].ColumnWidth:=3;
          ExcelApp.cells[3,20].value:='12';
          ExcelApp.ActiveSheet.Columns[20].ColumnWidth:=3;
          ExcelApp.cells[3,21].value:='13';
          ExcelApp.ActiveSheet.Columns[21].ColumnWidth:=3;
          ExcelApp.cells[3,22].value:='14';
          ExcelApp.ActiveSheet.Columns[22].ColumnWidth:=3;
          ExcelApp.cells[3,23].value:='15';
          ExcelApp.ActiveSheet.Columns[23].ColumnWidth:=3;
          ExcelApp.cells[3,24].value:='16';
          ExcelApp.ActiveSheet.Columns[24].ColumnWidth:=3;
          ExcelApp.cells[3,25].value:='17';
          ExcelApp.ActiveSheet.Columns[25].ColumnWidth:=3;
          ExcelApp.cells[3,26].value:='18';
          ExcelApp.ActiveSheet.Columns[26].ColumnWidth:=3;
          ExcelApp.cells[3,27].value:='19';
          ExcelApp.ActiveSheet.Columns[27].ColumnWidth:=3;
          ExcelApp.cells[3,28].value:='20';
          ExcelApp.ActiveSheet.Columns[28].ColumnWidth:=3;
          ExcelApp.cells[3,29].value:='21';
          ExcelApp.ActiveSheet.Columns[29].ColumnWidth:=3;
          ExcelApp.cells[3,30].value:='22';
          ExcelApp.ActiveSheet.Columns[30].ColumnWidth:=3;
          ExcelApp.cells[3,31].value:='23';
          ExcelApp.ActiveSheet.Columns[31].ColumnWidth:=3;
          ExcelApp.cells[3,32].value:='24';
          ExcelApp.ActiveSheet.Columns[32].ColumnWidth:=3;
          ExcelApp.cells[3,33].value:='25';
          ExcelApp.ActiveSheet.Columns[33].ColumnWidth:=3;
          ExcelApp.cells[3,34].value:='26';
          ExcelApp.ActiveSheet.Columns[34].ColumnWidth:=3;
          ExcelApp.cells[3,35].value:='27';
          ExcelApp.ActiveSheet.Columns[35].ColumnWidth:=3;
          ExcelApp.cells[3,36].value:='28';
          ExcelApp.ActiveSheet.Columns[36].ColumnWidth:=3;
          ExcelApp.cells[3,37].value:='29';
          ExcelApp.ActiveSheet.Columns[37].ColumnWidth:=3;
          ExcelApp.cells[3,38].value:='30';
          ExcelApp.ActiveSheet.Columns[38].ColumnWidth:=3;
          ExcelApp.cells[3,39].value:='31';
          ExcelApp.ActiveSheet.Columns[39].ColumnWidth:=3;      Application.ProcessMessages;
          adoq_imp.Close;
          adoq_imp.SQL.Clear;
          adoq_imp.SQL.ADD (...);
          adoq_imp.Open;
          ProgressBar1.Min:=0;
          ProgressBar1.Max:=adoq_imp.RecordCount;
          adoq_imp.First;
          for iLoop:=0 to adoq_imp.RecordCount -1 do
          begin
          .......................
          ExcelApp.cells[4+iLoop,45].value:='=SUM(H4:AM'+ IntToStr(3+iLoop)+')';
          ProgressBar1.Position:=0;
          ExcelApp.Visible:=True;
          Screen.Cursor:=crDefault;
        except
          ExcelApp.ActiveWorkBook.Saved:=True;
          ExcelApp.WorkBooks.Close;
          ExcelApp.Quit;
        end;
      

  3. 補充一下:(學會變通)isMsOffice:Boolean;以上這句可以不要;因為我的功能是可以同時ms office和open office,所以是加以區分
      

aliyun

类似问题 »