請看如下程序,我想指定第5列導出時為字符型,請問怎么做啊?
//導出到EXCEL文件
procedure TfrmPrtDepartCharge.OutExcel(Title: String; DBGrid: TDBGrid);
  var
      MsExcel, WorkBook, WorkSheet: Variant;
      i,j:   Integer;
      Row,   Col:   Integer;
      FieldName:   string;
      DataSet:   TDataSet;
      S:   String;
  begin
      try
          MsExcel := CreateOleObject('Excel.Application');
          WorkBook := CreateOleObject('Excel.Sheet');
      except
          Application.MessageBox('您的PC中未安裝Microsoft Excel.','',32);
          Exit;
      end;      WorkBook := MsExcel.WorkBooks.Add;
      WorkSheet := MsExcel.WorkSheets.Add;      MsExcel.Visible := True;
      Col := 1;
      MsExcel.Cells(1,Col) := Title;
      Row := 3;      DataSet := DBGrid.DataSource.DataSet;
      for I := 0 to DBGrid.Columns.Count   -   1   do
      begin
          if DBGrid.Columns[I].Visible   then
          begin
             FieldName := DBGrid.Columns[I].Title.Caption;
             MsExcel.Cells(Row, Col) := FieldName;
             Col := Col + 1;
          end;
      end;//      WorkSheet.cells(5,Col).NumberFormatLocal := '@';      Row := Row + 1;      DataSet.First;
      while not DataSet.Eof   do
      begin
          Col := 1;
          for J := 0 to DBGrid.Columns.Count - 1   do
          begin
              FieldName := DBGrid.Columns[J].FieldName;
              MsExcel.Cells(Row, Col) :='   '+ DataSet.FieldByName(FieldName).AsString + '   ';
              Col := Col + 1;
          end;
          Row:= Row + 1;
          DataSet.Next;
      end;      Col := 1;
      For J:=0 to DBGrid.Columns.Count - 1 do
      begin          S := Char(64 + ((J+1) mod 26));
          if (J+1) > 26 then
          begin
               S := Char(65+(((J+1)-26) div 26)) + S;
          end;          if J = 0 then
          begin
             MsExcel.Cells(Row, Col):= '合計:';
          end
          else if DBGrid.Columns[J].Field.DataType in [ftInteger,ftSmallint,ftFloat,ftBCD] then
          begin
              FieldName := DBGrid.Columns[J].FieldName;
              MsExcel.Cells(Row, Col) := '=SUM('+ S +'4:'+S+IntToStr(Row-1)+')';
          end;
              Col:= Col + 1;
          end;     end;

解决方案 »

  1.   

    应该的EXCEL的列格式有关,将EXCEL中的第5列设定为文本格式,那么从DBGRID中导过来的数据也会以文本方式保存
      

  2.   

    解决方法打开被你注释掉的那句
    //      WorkSheet.cells(5,Col).NumberFormatLocal := '@';
      

  3.   

    ExcelWorkSheet1.Range['E1','E9999'].NumberFormatLocal:='@';
      

  4.   

    一个导入到EXCEL的工具,不防一试SQL导出到EXCEL http://www.onlinedown.net/soft/44040.htm