excelapp.worksheets['山东'].activate;//将名为“山东”的工作簿 设为活动工作簿。这个是对的 
为什么用变量Str来代替 就出错呢。str的值已经赋值为 山东 了 
  excelapp.worksheets[Str].activate; 出错 
  excelapp.worksheets['Str'].activate;  出错
excelapp.worksheets['+Str+'].activate;   出错 该怎样赋值呢 

解决方案 »

  1.   

    excelapp.worksheets[i].activate;
    用序号试试看
      

  2.   

    供LZ參考procedure TWKO_PD_REPORT_F.cx_btnOKClick(Sender: TObject);
    var
      ExcelApp:variant;
      iLoop,i,k:Integer;
    begin
      if CheckBox1.Checked then
      begin
        if (cx_kssj.Text='')or(cx_jssj.Text='') then
        begin
          MessageDlg('導出報表開始日期和結束日期不能為空!',mtWarning,[mbOK],0);
          Exit;
        end;
      end
      else
      begin
        if (cx_planer.Text='')or(cx_kssj.Text='')or(cx_jssj.Text='') then
        begin
          MessageDlg('導出報表條件不能為空!',mtWarning,[mbOK],0);
          Exit;
        end;
      end;    
      
      try
        Application.ProcessMessages;
        ExcelApp:=CreateOleObject('Excel.Application');
      except
        MessageDlg('對不起,你電腦上沒有安裝Excel,請確認!',mtWarning,[mbOK],0);
        Exit;
      end;
      try
        ExcelApp.Caption:='昆盈生管週排程';
        ExcelApp.Visible:=False;
        ExcelApp.WorkBooks.Add;
        ExcelApp.WorkBooks[1].WorkSheets[1].name:='生管週排程報表';
        ExcelApp.WorkBooks[1].WorkSheets[1].Activate;
        Screen.Cursor:=crSQLWait;
        lab_age.Caption:='正在創建Excel格式';
        lab_age.Font.Color:=$00FF8000;
        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].Columns[11].ColumnWidth:=11.63;
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[1,8].font.name:='標楷體';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[1,8]:='昆 盈 生 管 週 排 程';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[1,8].font.size:=14;
        ExcelApp.Range['A1:N1'].MergeCells:=True;
        ExcelApp.Range['A1:N1'].HorizontalAlignment:=xlCenter;
        lab_age.Caption:='正在創建Excel表頭';
        lab_age.Font.Color:=$00FF8000;
        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.WorkBooks[1].WorkSheets[1].cells[3,6]:='PMC';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,7]:='實際工時';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,7]:='MFD';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,8]:='排程日期';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,8]:='PMC';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,9]:='上線日期';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,9]:='MFD';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,10]:='物料狀況';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,10]:='MAT';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,11]:='週數';
        ExcelApp.Range['K2:K3'].MergeCells:=True;
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,12]:='加總的標准工時';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,12]:='線別';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,13]:='週數';
        ExcelApp.Range['M2:O2'].MergeCells:=True;
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,13]:=inttostr(weekof(now))+'週';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,14]:=inttostr(weekof(now)+1)+'週';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,15]:='總計';    for i:=0 to DaysBetween(cx_kssj.Date,cx_jssj.Date) do
        begin
          lab_age.Caption:='開始導出"'+ FormatDateTime('yyyy/mm/dd',IncDay(cx_kssj.Date,i))+'"的數據';
          Application.ProcessMessages;
          if CheckBox1.Checked then
          begin
            adoq_wko_pdreport.Close;
            adoq_wko_pdreport.SQL.Text:='select * from WKO_SCHEDULE where WKO_SDATE between '''+ FormatDateTime('yyyy/mm/dd',cx_kssj.Date)+''' and '''+ FormatDateTime('yyyy/mm/dd',cx_jssj.Date)+''' order by WKO_XB,WKO_SDATE';
            adoq_wko_pdreport.Open;
          end
          else
          begin
            adoq_wko_pdreport.Close;
            adoq_wko_pdreport.SQL.Text:='select * from WKO_SCHEDULE where wko_planer='''+ trim(cx_planer.Text)+''' and WKO_SDATE between '''+ FormatDateTime('yyyy/mm/dd',cx_kssj.Date)+''' and '''+ FormatDateTime('yyyy/mm/dd',cx_jssj.Date)+''' order by WKO_XB,WKO_SDATE';
            adoq_wko_pdreport.Open;
          end;        pb1.Min:=0;
          pb1.Max:=adoq_wko_pdreport.RecordCount;      Application.ProcessMessages;
          adoq_wko_pdreport.First;
          for iLoop:=0 to adoq_wko_pdreport.RecordCount -1 do
          begin
            ExcelApp.cells[4+iLoop,1].RowHeight:=12;
            ExcelApp.cells[4+iLoop,1].value:=adoq_wko_pdreport.FieldByName('WKO_XB').AsString;
            ExcelApp.cells[4+iLoop,2].value:=adoq_wko_pdreport.FieldByName('WKO_ORDERNO').AsString;
            ExcelApp.cells[4+iLoop,3].value:=adoq_wko_pdreport.FieldByName('WKO_ITEMNO').AsString;
            ExcelApp.cells[4+iLoop,4].value:=adoq_wko_pdreport.FieldByName('WKO_CBZX').AsString;
            ExcelApp.cells[4+iLoop,5].value:=adoq_wko_pdreport.FieldByName('WKO_QTY').AsString;
            ExcelApp.cells[4+iLoop,6].value:=adoq_wko_pdreport.FieldByName('WKO_HS').AsString;
            ExcelApp.cells[4+iLoop,8].value:=adoq_wko_pdreport.FieldByName('WKO_SDATE').AsString;
            ExcelApp.cells[4+iLoop,11].value:=IntToStr(WeekOf(adoq_wko_pdreport.FieldValues['WKO_SDATE']));
            pb1.Position:=pb1.Position+1;
            Application.ProcessMessages;
            adoq_wko_pdreport.Next;
          end;
          adoq_wko_pdreport.Close;
          adoq_wko_pdreport.SQL.Text:='select distinct WKO_XB from WKO_SCHEDULE where WKO_XB IS NOT NULL';
          adoq_wko_pdreport.Open;
          Application.ProcessMessages;
          adoq_wko_pdreport.First;
          for k:=0 to adoq_wko_pdreport.RecordCount -1 do
          begin
            ExcelApp.cells[4+k,12].value:=adoq_wko_pdreport.FieldByName('WKO_XB').AsString;
            Application.ProcessMessages;
            adoq_wko_pdreport.Next;
          end;                                                end;    ExcelApp.Range['A1:O'+inttostr(ExcelApp.WorkBooks[1].WorkSheets[1].usedrange.rows.count)].HorizontalAlignment:=xlCenter ;
        ExcelApp.Range['A1:O'+inttostr(ExcelApp.WorkBooks[1].WorkSheets[1].usedrange.rows.count)].Borders.linestyle:=xlContinuous;
        Screen.Cursor:=crDefault;
        ExcelApp.Visible:=True;
        pb1.Position:=0;
        ModalResult:=mrOk;
        lab_age.Caption:='';
      except
        ExcelApp.ActiveWorkBook.Saved:=True;
        ExcelApp.WorkBooks.Close;
        ExcelApp.Quit;
      end;
    end;
      

  3.   

    供LZ參考procedure TWKO_PD_REPORT_F.cx_btnOKClick(Sender: TObject);
    var
      ExcelApp:variant;
      iLoop,i,k:Integer;
    begin
      if CheckBox1.Checked then
      begin
        if (cx_kssj.Text='')or(cx_jssj.Text='') then
        begin
          MessageDlg('導出報表開始日期和結束日期不能為空!',mtWarning,[mbOK],0);
          Exit;
        end;
      end
      else
      begin
        if (cx_planer.Text='')or(cx_kssj.Text='')or(cx_jssj.Text='') then
        begin
          MessageDlg('導出報表條件不能為空!',mtWarning,[mbOK],0);
          Exit;
        end;
      end;    
      
      try
        Application.ProcessMessages;
        ExcelApp:=CreateOleObject('Excel.Application');
      except
        MessageDlg('對不起,你電腦上沒有安裝Excel,請確認!',mtWarning,[mbOK],0);
        Exit;
      end;
      try
        ExcelApp.Caption:='昆盈生管週排程';
        ExcelApp.Visible:=False;
        ExcelApp.WorkBooks.Add;
        ExcelApp.WorkBooks[1].WorkSheets[1].name:='生管週排程報表';
        ExcelApp.WorkBooks[1].WorkSheets[1].Activate;
        Screen.Cursor:=crSQLWait;
        lab_age.Caption:='正在創建Excel格式';
        lab_age.Font.Color:=$00FF8000;
        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].Columns[11].ColumnWidth:=11.63;
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[1,8].font.name:='標楷體';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[1,8]:='昆 盈 生 管 週 排 程';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[1,8].font.size:=14;
        ExcelApp.Range['A1:N1'].MergeCells:=True;
        ExcelApp.Range['A1:N1'].HorizontalAlignment:=xlCenter;
        lab_age.Caption:='正在創建Excel表頭';
        lab_age.Font.Color:=$00FF8000;
        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.WorkBooks[1].WorkSheets[1].cells[3,6]:='PMC';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,7]:='實際工時';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,7]:='MFD';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,8]:='排程日期';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,8]:='PMC';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,9]:='上線日期';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,9]:='MFD';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,10]:='物料狀況';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,10]:='MAT';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,11]:='週數';
        ExcelApp.Range['K2:K3'].MergeCells:=True;
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,12]:='加總的標准工時';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,12]:='線別';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,13]:='週數';
        ExcelApp.Range['M2:O2'].MergeCells:=True;
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,13]:=inttostr(weekof(now))+'週';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,14]:=inttostr(weekof(now)+1)+'週';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,15]:='總計';    for i:=0 to DaysBetween(cx_kssj.Date,cx_jssj.Date) do
        begin
          lab_age.Caption:='開始導出"'+ FormatDateTime('yyyy/mm/dd',IncDay(cx_kssj.Date,i))+'"的數據';
          Application.ProcessMessages;
          if CheckBox1.Checked then
          begin
            adoq_wko_pdreport.Close;
            adoq_wko_pdreport.SQL.Text:='select * from WKO_SCHEDULE where WKO_SDATE between '''+ FormatDateTime('yyyy/mm/dd',cx_kssj.Date)+''' and '''+ FormatDateTime('yyyy/mm/dd',cx_jssj.Date)+''' order by WKO_XB,WKO_SDATE';
            adoq_wko_pdreport.Open;
          end
          else
          begin
            adoq_wko_pdreport.Close;
            adoq_wko_pdreport.SQL.Text:='select * from WKO_SCHEDULE where wko_planer='''+ trim(cx_planer.Text)+''' and WKO_SDATE between '''+ FormatDateTime('yyyy/mm/dd',cx_kssj.Date)+''' and '''+ FormatDateTime('yyyy/mm/dd',cx_jssj.Date)+''' order by WKO_XB,WKO_SDATE';
            adoq_wko_pdreport.Open;
          end;        pb1.Min:=0;
          pb1.Max:=adoq_wko_pdreport.RecordCount;      Application.ProcessMessages;
          adoq_wko_pdreport.First;
          for iLoop:=0 to adoq_wko_pdreport.RecordCount -1 do
          begin
            ExcelApp.cells[4+iLoop,1].RowHeight:=12;
            ExcelApp.cells[4+iLoop,1].value:=adoq_wko_pdreport.FieldByName('WKO_XB').AsString;
            ExcelApp.cells[4+iLoop,2].value:=adoq_wko_pdreport.FieldByName('WKO_ORDERNO').AsString;
            ExcelApp.cells[4+iLoop,3].value:=adoq_wko_pdreport.FieldByName('WKO_ITEMNO').AsString;
            ExcelApp.cells[4+iLoop,4].value:=adoq_wko_pdreport.FieldByName('WKO_CBZX').AsString;
            ExcelApp.cells[4+iLoop,5].value:=adoq_wko_pdreport.FieldByName('WKO_QTY').AsString;
            ExcelApp.cells[4+iLoop,6].value:=adoq_wko_pdreport.FieldByName('WKO_HS').AsString;
            ExcelApp.cells[4+iLoop,8].value:=adoq_wko_pdreport.FieldByName('WKO_SDATE').AsString;
            ExcelApp.cells[4+iLoop,11].value:=IntToStr(WeekOf(adoq_wko_pdreport.FieldValues['WKO_SDATE']));
            pb1.Position:=pb1.Position+1;
            Application.ProcessMessages;
            adoq_wko_pdreport.Next;
          end;
          adoq_wko_pdreport.Close;
          adoq_wko_pdreport.SQL.Text:='select distinct WKO_XB from WKO_SCHEDULE where WKO_XB IS NOT NULL';
          adoq_wko_pdreport.Open;
          Application.ProcessMessages;
          adoq_wko_pdreport.First;
          for k:=0 to adoq_wko_pdreport.RecordCount -1 do
          begin
            ExcelApp.cells[4+k,12].value:=adoq_wko_pdreport.FieldByName('WKO_XB').AsString;
            Application.ProcessMessages;
            adoq_wko_pdreport.Next;
          end;                                                end;    ExcelApp.Range['A1:O'+inttostr(ExcelApp.WorkBooks[1].WorkSheets[1].usedrange.rows.count)].HorizontalAlignment:=xlCenter ;
        ExcelApp.Range['A1:O'+inttostr(ExcelApp.WorkBooks[1].WorkSheets[1].usedrange.rows.count)].Borders.linestyle:=xlContinuous;
        Screen.Cursor:=crDefault;
        ExcelApp.Visible:=True;
        pb1.Position:=0;
        ModalResult:=mrOk;
        lab_age.Caption:='';
      except
        ExcelApp.ActiveWorkBook.Saved:=True;
        ExcelApp.WorkBooks.Close;
        ExcelApp.Quit;
      end;
    end;
      

  4.   

    供LZ參考procedure TWKO_PD_REPORT_F.cx_btnOKClick(Sender: TObject);
    var
      ExcelApp:variant;
      iLoop,i,k:Integer;
    begin
      if CheckBox1.Checked then
      begin
        if (cx_kssj.Text='')or(cx_jssj.Text='') then
        begin
          MessageDlg('導出報表開始日期和結束日期不能為空!',mtWarning,[mbOK],0);
          Exit;
        end;
      end
      else
      begin
        if (cx_planer.Text='')or(cx_kssj.Text='')or(cx_jssj.Text='') then
        begin
          MessageDlg('導出報表條件不能為空!',mtWarning,[mbOK],0);
          Exit;
        end;
      end;    
      
      try
        Application.ProcessMessages;
        ExcelApp:=CreateOleObject('Excel.Application');
      except
        MessageDlg('對不起,你電腦上沒有安裝Excel,請確認!',mtWarning,[mbOK],0);
        Exit;
      end;
      try
        ExcelApp.Caption:='昆盈生管週排程';
        ExcelApp.Visible:=False;
        ExcelApp.WorkBooks.Add;
        ExcelApp.WorkBooks[1].WorkSheets[1].name:='生管週排程報表';
        ExcelApp.WorkBooks[1].WorkSheets[1].Activate;
        Screen.Cursor:=crSQLWait;
        lab_age.Caption:='正在創建Excel格式';
        lab_age.Font.Color:=$00FF8000;
        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].Columns[11].ColumnWidth:=11.63;
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[1,8].font.name:='標楷體';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[1,8]:='昆 盈 生 管 週 排 程';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[1,8].font.size:=14;
        ExcelApp.Range['A1:N1'].MergeCells:=True;
        ExcelApp.Range['A1:N1'].HorizontalAlignment:=xlCenter;
        lab_age.Caption:='正在創建Excel表頭';
        lab_age.Font.Color:=$00FF8000;
        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.WorkBooks[1].WorkSheets[1].cells[3,6]:='PMC';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,7]:='實際工時';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,7]:='MFD';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,8]:='排程日期';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,8]:='PMC';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,9]:='上線日期';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,9]:='MFD';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,10]:='物料狀況';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,10]:='MAT';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,11]:='週數';
        ExcelApp.Range['K2:K3'].MergeCells:=True;
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,12]:='加總的標准工時';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,12]:='線別';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[2,13]:='週數';
        ExcelApp.Range['M2:O2'].MergeCells:=True;
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,13]:=inttostr(weekof(now))+'週';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,14]:=inttostr(weekof(now)+1)+'週';
        ExcelApp.WorkBooks[1].WorkSheets[1].cells[3,15]:='總計';    for i:=0 to DaysBetween(cx_kssj.Date,cx_jssj.Date) do
        begin
          lab_age.Caption:='開始導出"'+ FormatDateTime('yyyy/mm/dd',IncDay(cx_kssj.Date,i))+'"的數據';
          Application.ProcessMessages;
          if CheckBox1.Checked then
          begin
            adoq_wko_pdreport.Close;
            adoq_wko_pdreport.SQL.Text:='select * from WKO_SCHEDULE where WKO_SDATE between '''+ FormatDateTime('yyyy/mm/dd',cx_kssj.Date)+''' and '''+ FormatDateTime('yyyy/mm/dd',cx_jssj.Date)+''' order by WKO_XB,WKO_SDATE';
            adoq_wko_pdreport.Open;
          end
          else
          begin
            adoq_wko_pdreport.Close;
            adoq_wko_pdreport.SQL.Text:='select * from WKO_SCHEDULE where wko_planer='''+ trim(cx_planer.Text)+''' and WKO_SDATE between '''+ FormatDateTime('yyyy/mm/dd',cx_kssj.Date)+''' and '''+ FormatDateTime('yyyy/mm/dd',cx_jssj.Date)+''' order by WKO_XB,WKO_SDATE';
            adoq_wko_pdreport.Open;
          end;        pb1.Min:=0;
          pb1.Max:=adoq_wko_pdreport.RecordCount;      Application.ProcessMessages;
          adoq_wko_pdreport.First;
          for iLoop:=0 to adoq_wko_pdreport.RecordCount -1 do
          begin
            ExcelApp.cells[4+iLoop,1].RowHeight:=12;
            ExcelApp.cells[4+iLoop,1].value:=adoq_wko_pdreport.FieldByName('WKO_XB').AsString;
            ExcelApp.cells[4+iLoop,2].value:=adoq_wko_pdreport.FieldByName('WKO_ORDERNO').AsString;
            ExcelApp.cells[4+iLoop,3].value:=adoq_wko_pdreport.FieldByName('WKO_ITEMNO').AsString;
            ExcelApp.cells[4+iLoop,4].value:=adoq_wko_pdreport.FieldByName('WKO_CBZX').AsString;
            ExcelApp.cells[4+iLoop,5].value:=adoq_wko_pdreport.FieldByName('WKO_QTY').AsString;
            ExcelApp.cells[4+iLoop,6].value:=adoq_wko_pdreport.FieldByName('WKO_HS').AsString;
            ExcelApp.cells[4+iLoop,8].value:=adoq_wko_pdreport.FieldByName('WKO_SDATE').AsString;
            ExcelApp.cells[4+iLoop,11].value:=IntToStr(WeekOf(adoq_wko_pdreport.FieldValues['WKO_SDATE']));
            pb1.Position:=pb1.Position+1;
            Application.ProcessMessages;
            adoq_wko_pdreport.Next;
          end;
          adoq_wko_pdreport.Close;
          adoq_wko_pdreport.SQL.Text:='select distinct WKO_XB from WKO_SCHEDULE where WKO_XB IS NOT NULL';
          adoq_wko_pdreport.Open;
          Application.ProcessMessages;
          adoq_wko_pdreport.First;
          for k:=0 to adoq_wko_pdreport.RecordCount -1 do
          begin
            ExcelApp.cells[4+k,12].value:=adoq_wko_pdreport.FieldByName('WKO_XB').AsString;
            Application.ProcessMessages;
            adoq_wko_pdreport.Next;
          end;                                                end;    ExcelApp.Range['A1:O'+inttostr(ExcelApp.WorkBooks[1].WorkSheets[1].usedrange.rows.count)].HorizontalAlignment:=xlCenter ;
        ExcelApp.Range['A1:O'+inttostr(ExcelApp.WorkBooks[1].WorkSheets[1].usedrange.rows.count)].Borders.linestyle:=xlContinuous;
        Screen.Cursor:=crDefault;
        ExcelApp.Visible:=True;
        pb1.Position:=0;
        ModalResult:=mrOk;
        lab_age.Caption:='';
      except
        ExcelApp.ActiveWorkBook.Saved:=True;
        ExcelApp.WorkBooks.Close;
        ExcelApp.Quit;
      end;
    end;
      

  5.   

    这样赋值看看
    excelapp.worksheets[QuotedStr(Str)].activate;  
      

  6.   

    ExcelApp.worksheets[i].Activate;  用序号比较好点吧
      

  7.   

    excelapp.worksheets[Str].activate
    我也是同样用的,沒事啊。是不是給str值時的事,貼出代码吧
      

  8.   

    var F:TextFile;
    Str,p:String;
    begin
    assignFile(f,'d:\mm.txt');  //mm.txt里边只有山东 两个字符
    reset(f);
       read(f,p);
       Str:=Str+p;
    closeFile(f);
    ShowMessage(Str);//输出的是 山东 两个字符串 。可见赋值没问题。
    excelapp:=createoleobject('excel.application');
    excelapp.visible:=true; //显示当前窗口
    excelapp.workbooks.open('D:\客户列表.xls'); //打开一个已经存在的工作簿
    excelapp.worksheets[Str].activate;  //就是这个地方 *****
      

  9.   

    这是我的测试代码,好用procedure TForm1.Button1Click(Sender: TObject);
    var
      excelapp: variant;
      str:      string;
    begin
      excelapp:=createoleobject('excel.application');
      excelapp.visible:=true; //显示当前窗口
      excelapp.workbooks.open('D:\1.xls'); //打开一个已经存在的工作簿
      str:='山东';
      excelapp.worksheets[Str].activate;  //就是这个地方 *****
    end;
      

  10.   

    这样也测了,同样好用
    procedure TForm1.Button2Click(Sender: TObject);
    var F:TextFile;
    Str,p:String;
    excelapp: variant;
    begin
    assignFile(f,'d:\1.txt');  //mm.txt里边只有山东 两个字符
    reset(f);
      read(f,p);
      Str:=Str+p;
    closeFile(f);
    ShowMessage(Str);//输出的是 山东 两个字符串 。可见赋值没问题。
    excelapp:=createoleobject('excel.application');
    excelapp.visible:=true; //显示当前窗口
    excelapp.workbooks.open('D:\1.xls'); //打开一个已经存在的工作簿
    excelapp.worksheets[Str].activate;  //就是这个地方 *****end;