您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Delphi >> 请专家帮忙:在将DBGRID数据导入EXCEL时怎么有些字段的值会不正确?

请专家帮忙:在将DBGRID数据导入EXCEL时怎么有些字段的值会不正确?

来源:网络整理     时间:2016/8/20 4:54:09     关键词:

关于网友提出的“ 请专家帮忙:在将DBGRID数据导入EXCEL时怎么有些字段的值会不正确?”问题疑问,本网通过在网上对“ 请专家帮忙:在将DBGRID数据导入EXCEL时怎么有些字段的值会不正确?”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 请专家帮忙:在将DBGRID数据导入EXCEL时怎么有些字段的值会不正确?
描述:

代码如下:
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: ExcelApplication
procedure 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;


以上介绍了“ 请专家帮忙:在将DBGRID数据导入EXCEL时怎么有些字段的值会不正确?”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3411979.html

相关图片

相关文章