各位大俠請幫忙:我是剛學delphi不久,現在我有一個表是記錄身份證號的轉出到excel時,excel會把它當成數字型的值,故轉出的值与身份証號不符,請問我怎麼做才可以.轉excel的語句我也是從網上找的:
procedure TForm1.Button3Click(Sender: TObject);
 var     i,j,LCount:integer;
     fileName:string;
     begin
     SaveDialog1.DefaultExt:='.xls';
     SaveDialog1.Filter :='Excel(*.xls)|*.xls';
     if SaveDialog1.execute then
     fileName:=SaveDialog1.filename;
     try     ExcelApplication1:=TExcelApplication.create(Application);
     ExcelWorkSheet1:=TExcelWorkSheet.create(Application);
     ExcelWorkBook1:=TExcelWorkBook.create(Application);
     ExcelApplication1.Connect;
     except
     Application.MessageBox('Excel沒裝','Hello',MB_IconError+mb_Ok);
     Abort;
     end;     try
     Screen.Cursor := crHourGlass;
   adostoredproc1.Close;
  adostoredproc1.Parameters.ParamByName('@p1').Value :=edit1.Text+'-01';
  adostoredproc1.Parameters.ParamByName('@p2').Value :=combobox1.ItemIndex;
  adostoredproc1.Open;
     ExcelApplication1.Workbooks.add(emptyparam,0);
     ExcelWorkBook1.connectto(ExcelApplication1.Workbooks[1]);
     ExcelWorkSheet1.connectto(ExcelWorkBook1.Worksheets[1] as _worksheet);
     LCount:=adostoredproc1.FieldCount;
     if combobox1.ItemIndex =1 then
     begin
     ExcelWorkSheet1.Cells.item[2,3]:=
     ......
     end;
     if combobox1.ItemIndex =2 then
     begin
     ExcelWorkSheet1.Cells.item[1,1]:=' 轉出數據';
     
     end;
     if combobox1.ItemIndex =3 then
      begin
       ExcelWorkSheet1.Cells.item[1,1]:=
       ......
      end;         adostoredproc1.First;     j:=3;     while not adostoredproc1.eof do
     begin         for i:=0 to LCount-1 do
         begin
          ExcelWorkSheet1.Cells.item[j,i+1]:=adostoredproc1.Fields[i].Value;         end;
     inc(j);
     adostoredproc1.Next;
     end;
     ExcelWorkSheet1.Cells.item[j+1,i-1]:='總計:'+floattostr(adostoredproc1.RecordCount );
     //ExcelWorkSheet1.Cells.item[j+2,1]:='核非:審核:制表:';     ExcelWorkSheet1.columns.autofit;
     ExcelWorkSheet1.saveAs(filename);
     Application.MessageBox(pchar('數據成功轉出'+filename),'hello',mb_ok);
     finally
     ExcelApplication1.Disconnect;
     ExcelApplication1.quit;
     ExcelApplication1.free;
     ExcelWorkSheet1.free;
     ExcelWorkBook1.free;
     end;
     Screen.Cursor := crDefault;     end;end.

解决方案 »

  1.   

    导出时判断该字段是否是身份证号字段,如果是的话在前面加一符号  '
    如:
     ExcelWorkSheet1.Cells.item[j,i+1]:=''''+adostoredproc1.Fields[i].Value;
    ///注:加在这里的符号 ' 。在字段串中用 '''' 表示 符号 ' 。
      

  2.   

    for i:=0 to LCount-1 do
      begin
        if adostoredproc1.Fields[i].FieldName='XXX字段名' then  ExcelWorkSheet1.Cells.item[j,i+1]:=''''+adostoredproc1.Fields[i].Value
        else ExcelWorkSheet1.Cells.item[j,i+1]:=adostoredproc1.Fields[i].Value;
      end;