这段代码当中,如果下面8个字段都不是空的就可以完全导出。
当其中某个字段是空的话就出现错误,请高手指点如何解决从dbgrid中把数据到到excel完整的处理过程。谢谢!!!
下面为本人的写的部分代码:
procedure Tkfb_query.Button8Click(Sender: TObject);
var
  XL,Sheet: Variant;
  FileName: String;
  i,row: integer;begin      if DM.kfb_hetongchaxun.Active =true then
       begin
          if DM.kfb_hetongchaxun.RecordCount >0 then
           begin
           Screen.Cursor:=crSqlWait;
        try
          FileName:=ExtractFilePath(Application.ExeName)+'合同信息.xls';
          XL:=CreateOLEObject('Excel.Application');
          XL.WorkBooks.open(FileName);
          Sheet:=XL.WorkBooks[1].WorkSheets['合同信息'];
          Sheet.Activate;
          except
         MessageDlg('【合同信息】文件不存在或已经被破坏!'+#13+'无法完成导出。',mtWarning,[mbOk],0);
        Screen.Cursor:=crDefault;
        Exit;
        end;
     try
        row:=8;//the  number of row
        for i:=1 to DM.kfb_hetongchaxun.RecordCount  do
         begin
          Sheet.Cells[row,1]:=Trim(DM.kfb_hetongchaxun.FieldValues['编号']);//
          Sheet.Cells[row,2]:=Trim(DM.kfb_hetongchaxun.FieldValues['负责人']);//
          Sheet.Cells[row,3]:=Trim(DM.kfb_hetongchaxun.FieldValues['校内单位']);//
          Sheet.Cells[row,4]:=Trim(DM.kfb_hetongchaxun.FieldValues['合同编号']);//
          Sheet.Cells[row,5]:=DM.kfb_hetongchaxun.FieldValues['项目名称'];//
          Sheet.Cells[row,6]:=DM.kfb_hetongchaxun.FieldValues['项目性质'];//
          Sheet.Cells[row,7]:=DM.kfb_hetongchaxun.FieldValues['合作单位'];//
          Sheet.Cells[row,8]:=DM.kfb_hetongchaxun.FieldValues['合同额'];
          row:=row+1;
          DM.kfb_hetongchaxun.Next;//the  next of master table
         end;
          XL.visible:=true;
         Screen.Cursor:=crDefault;
         except
         MessageDlg('导出Excel错误!',mtError,[mbOk],0);
         Screen.Cursor:=CrDefault;
        end;
    end;
    end;
end;

解决方案 »

  1.   

    procedure Tkfb_query.Button8Click(Sender: TObject);
    var
      XL,Sheet: Variant;
      FileName: String;
      i,row: integer;begin      if DM.kfb_hetongchaxun.Active =true then
           begin
              if DM.kfb_hetongchaxun.RecordCount >0 then
               begin
               Screen.Cursor:=crSqlWait;
            try
              FileName:=ExtractFilePath(Application.ExeName)+'合同信息.xls';
              XL:=CreateOLEObject('Excel.Application');
              XL.WorkBooks.open(FileName);
              Sheet:=XL.WorkBooks[1].WorkSheets['合同信息'];
              Sheet.Activate;
              except
             MessageDlg('【合同信息】文件不存在或已经被破坏!'+#13+'无法完成导出。',mtWarning,[mbOk],0);
            Screen.Cursor:=crDefault;
            Exit;
            end;
         try
            row:=8;//the  number of row
            for i:=1 to DM.kfb_hetongchaxun.RecordCount  do
             begin
              if varisnull(Trim(DM.kfb_hetongchaxun.FieldValues['编号'])) then
                 Sheet.Cells[row,1]:=''
              else
                 Sheet.Cells[row,1]:=Trim(DM.kfb_hetongchaxun.FieldValues['编号']);//
              if varisnull(DM.kfb_hetongchaxun.FieldValues['负责人']) then 
                 Sheet.Cells[row,2]:=''
              else 
                 Sheet.Cells[row,2]:=Trim(DM.kfb_hetongchaxun.FieldValues['负责人']);//
              if varisnull(DM.kfb_hetongchaxun.FieldValues['校内单位']) then
                 Sheet.Cells[row,3]:=''
              else
                 Sheet.Cells[row,3]:=Trim(DM.kfb_hetongchaxun.FieldValues['校内单位'])//
              if varisnull(DM.kfb_hetongchaxun.FieldValues['合同编号']) then
                 Sheet.Cells[row,4]:=''
              else
                 Sheet.Cells[row,4]:=Trim(DM.kfb_hetongchaxun.FieldValues['合同编号']);//
              if varisnull(DM.kfb_hetongchaxun.FieldValues['项目名称']) then
                 Sheet.Cells[row,5]:=''
              else        
                 Sheet.Cells[row,5]:=DM.kfb_hetongchaxun.FieldValues['项目名称'];//
              if varisnull(DM.kfb_hetongchaxun.FieldValues['项目性质']) then   
                 Sheet.Cells[row,6]:=''
              else
                 Sheet.Cells[row,6]:=DM.kfb_hetongchaxun.FieldValues['项目性质'];//
              if varisnull(DM.kfb_hetongchaxun.FieldValues['合作单位']) then
                 Sheet.Cells[row,6]:=''  
              else   
                 Sheet.Cells[row,7]:=DM.kfb_hetongchaxun.FieldValues['合作单位'];//
              if varisnull(DM.kfb_hetongchaxun.FieldValues['合同额']) then
                 Sheet.Cells[row,8]:=0
              else
                 Sheet.Cells[row,8]:=DM.kfb_hetongchaxun.FieldValues['合同额'];
              row:=row+1;
              DM.kfb_hetongchaxun.Next;//the  next of master table
             end;
              XL.visible:=true;
             Screen.Cursor:=crDefault;
             except
             MessageDlg('导出Excel错误!',mtError,[mbOk],0);
             Screen.Cursor:=CrDefault;
            end;
        end;
        end;
    end;
      

  2.   

    当我动态写标题事出现错误!!
    求高手再指点!!!!
               //写入标题
                  range :=varexcel.workbooks[1].worksheets[1].columns;
                  for i:=0 to kfb_query.ListBox2.items.count -1 do
                     begin
                  varexcel.workbooks[1].worksheets[1].cell[2,i+1].value := kfb_query.ListBox2.items.strings[i];//              varexcel.workbooks[1].worksheets[1].cell[2,i+1] :=Trim(kfb_query.ListBox2.items[i]);              varexcel.workbooks[1].worksheets[1].cell[2,i+1].Font.bold :=true;
    //              range.columns[i+1].columnwidth := DM.kfb_hetongchaxun.FieldByName(Trim(kfb_query.ListBox2.items[i])).Displaywidth;
                  range.columns[i+1].columnwidth := DM.kfb_hetongchaxun.FieldByName(kfb_query.ListBox2.items.strings[i]).Displaywidth;
                  range.columns[i+1].HorizontalAlignment :=xlCenter;
                   end;