代码如下:
i:=1;
with DM_enroll.DataSource2.DataSet do
begin
  Open;
  DisableControls;
  for j:=0 to DBGridEh1.Columns.Count-1 do
    begin
      if DBGridEh1.Columns[j].Visible=true then
        begin
           w:=FieldByName(DBGridEh1.Columns[j].FieldName).DisplayWidth;
           worksheet.cells.item[1,j+1].ColumnWidth:=w;
           worksheet.cells[1,j+1]:=DBGridEh1.Columns[j].Title.Caption;
         end;
     end;
   first;
   while not Eof do
   begin
     inc(i);
     for j:=0 to DBGridEh1.Columns.Count-1 do
     begin
       if DBGridEh1.Columns[j].Visible=true then
        begin
          worksheet.cells[i,j+1]:=DBGridEh1.Columns[j].Field.AsString;
        end;
     end;
     next;
   end;
   EnableControls;
end;
程序运行后发现有几个字段的值不正确:
字段名  类型           原来的值              EXCEL中的值
no     VARCHAR(14)   01350100000001      1.3501E+12
sno    VARCHAR(18)   350103000000000001  3.50103E+17
phone  VARCHAR(20)   05111111111         5111111111 
请专家帮忙解决,盼复,感激不尽!

解决方案 »

  1.   

    1、需要设置Excel单元格类型格式,代码我再找找!
    2、帖段代码给你看看,一般都是控制数据集而不是操作DBGrid的!
       A: 用OLE
    var
      ExcelAppDetail :Variant;
      vPath :string;
      i :Integer;
    begin
      with SysDM.qryTaxDetail do
      begin
        if IsEmpty or (not Active) then
        begin
          Application.MessageBox('没有明细数据需要导出!','提示',MB_IconInformation+MB_OK);
          exit;
        end;
        ExcelAppDetail :=CreateOleObject('Excel.Application');
        ExcelAppDetail.Visible :=True;
        ExcelAppDetail.Caption :='税源分户明细报表';
        vPath :=ExtractFilePath(ParamStr(0));
        ExcelAppDetail.WorkBooks.Open(vPath+'Xls\TaxRep.Xlt');
        ExcelAppDetail.WorkSheets[1].Activate;
        first;
        for i:=0 to RecordCount-1 do
        begin
          ExcelAppDetail.ActiveSheet.Rows[i+7].Font.Name := '新宋体';
          ExcelAppDetail.ActiveSheet.Rows[i+7].Font.Color := clBlack;
          ExcelAppDetail.ActiveSheet.Rows[i+7].Font.Bold := False;
          ExcelAppDetail.ActiveSheet.Rows[i+7].Font.UnderLine := False;
          ExcelAppDetail.ActiveSheet.Rows[i+7].Font.Size := 9;
          ExcelAppDetail.ActiveSheet.Rows[i+7].RowHeight := 20;
          ExcelAppDetail.Cells[i+7,2].Value  :=FieldByName('DanWeiMC').Value;
          ExcelAppDetail.Cells[i+7,3].Value  :=FieldByName('YY_BenYueWC').Value;
          ExcelAppDetail.Cells[i+7,4].Value  :=FieldByName('YY_BenYueTQ').Value;
          ExcelAppDetail.Cells[i+7,5].Value  :=FieldByName('YY_BenYueZJE').Value;
          ExcelAppDetail.Cells[i+7,6].Value  :=FieldByName('YY_LeiJiWC').Value;
          //....
          Next;
        end;
      end;
    end;
       B: ExcelApplicationprocedure TOutCheckReportForm.ExportDataToExcel(aQryExport: TQuery;
      aDbgExport: TDBGrid);
    var
      i,j :Integer;
      Range :Excel97.Range;
    begin
      if not aQryExport.Active then Exit;
      Excel.Visible[0] :=True;
      Excel.Workbooks.Add(Null,0);
      Range :=Excel.ActiveCell;
      for i:=0 to aDbgExport.FieldCount-1 do
      begin
        Range.Value :=aDbgExport.Columns[i].Title.Caption;
        Range :=Range.Next;
      end;
      aQryExport.DisableControls;
      try
        j :=2;
        aQryExport.First;
        while not aQryExport.eof do
        begin
          Range := Excel.Range['A' + Inttostr(j),'A' + Inttostr(j)];
          for i:=0 to aDbgExport.FieldCount-1 do
          begin
            Range.Value :=aDbgExport.Fields[i].AsString;
            Range :=Range.Next;
          end;
          inc(j);
          aQryExport.Next;
        end;
      except
        aQryExport.EnableControls;
      end;
    end;
      

  2.   

    请问:如何通过程序设置EXCEL的单元格的格式为文本格式?
      

  3.   

    用excel改一下那列的单元格格式就行了