当前位置:主页>仓库管理软件> 列表

请问:从DBGrid生成到文件Excel时,如何避免Exc

财务软件版1楼: 有一个字段的数据,全是数字,但是用字符类型保存的,比如
200509200000899926667 ,有那么长,保存到Excel之后自动变成了科学计数法的
2.00509E+20,这样就错了,请问有没有什么方法可以是Excel不这样作??

就是如何控制Excel的单元格格式的问题,不用Server控件,用自动化的方法如何作呢???
急,请大家帮忙!!!

2楼: ExcelApp.cells.NumberFormat := ''@'';商品进销存表格下载

3楼: 我的这个好象没有你的现象哦
procedure Tfmjtjs.SpeedButton13Click(Sender: TObject);
var
i,row,column:integer;
len:integer;
str:array[1..100] of string;
begin
if adoquery1.RecordCount=0 then
begin
application.MessageBox(''没有数据,无法导入!'',''系统信息提示'',48);
exit;
end;
Try
ExcelApplication1.Connect;
Except
MessageDlg(''请检查您是否安装了Excel'',
mtError, [mbOk], 0);
Abort;
End;
ExcelApplication1.Visible[0]:=false;
ExcelApplication1.Caption:=''123'';


ExcelApplication1.Workbooks.Add(Null,0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
//Table1.Open;
row:=2;
For I := 1 To adoquery1.FieldCount Do
ExcelWorksheet1.Cells.Item[1,i]:=adoquery1.Fields[I-1].FieldName ;
ProgressBar1.Min :=0;
ProgressBar1.Max :=adoquery1.RecordCount -1;
adoquery1.First ;
While Not(adoquery1.Eof) do
begin
column:=1;
len:=1;
ProgressBar1.Position:=row;
label18.Caption:= inttostr(row-2);
//////////////////////////////////////////////////////////////////// //
for i:=0 to adoquery1.FieldCount-1 do //
begin // 自动适应大小
if length(str[1+i])str[1+i]:=adoquery1.fields[i].AsString;
if length(str[1+i])>length(adoquery1.Fields[i].FieldName) then
begin
ExcelWorksheet1.Cells.Item[1,1+i].ColumnWidth:=length(str[1+i]);


end
else
ExcelWorksheet1.Cells.Item[1,1+i].ColumnWidth:=length(adoquery1.Fields[i].FieldName);
end; //
//////////////////////////////////////////////////////////////////////

for i:=1 to adoquery1.FieldCount do
begin
application.ProcessMessages;
ExcelWorksheet1.Cells.Item[row,column]:=adoquery1.fields[i-1].AsString;
column:=column+1;
end;
adoquery1.Next;
row:=row+1;
len:=len+1;
end;
ProgressBar1.Position:=0;
ExcelApplication1.Visible[0]:=true;

end;

4楼: for i:=a to q1.RecordCount+1 do
begin
j:=1;
k:=1;
for j:=1 to clb1.Items.Count do
begin
if clb1.Checked[j-1] then
begin
if q1.FieldByName(cbFields.Items[j-1]).DataType=ftString then
Sheet.Cells[i,k].NumberFormatLocal:=''@''; //格式化為字符串
Sheet.Cells[i,k]:=q1.FieldByName(cbFields.Items[j-1]).AsString;
k:=k+1;
end;
end;
q1.Next;
end;

5楼: 需求把200509200000899926667在excel转化为文本就可以了
其实你在excel中直接输入200509200000899926667也会不正确的

6楼: 看起来是2.00509E+20,实际双击还是2.00509200000899926667。
只需要把EXCEL设置成文本格式就显示正常了。

财务软件版7楼: 只有通过ole的方式,设置Selection的NumberFormatLocal 为 "@",其他好像没有办法

8楼: procedure TCK_EXCEL_Form.FlatButton1Click(Sender: TObject);
var
MyExcel,Sheet,Range:Variant;
str:string;
i,j,k,a:integer;
begin
if not SaveDialog1.Execute then exit;
str:=SaveDialog1.FileName;
q1.First;
try
MyExcel:=CreateOleObject(''Excel.Application'');
except
Application.MessageBox(PChar(dm.GetErrorType(10182)),PChar(Application.Title),0);
Exit;
end;
MyExcel.Visible:=true;
MyExcel.workbooks.add(1);
Sheet:=MyExcel.workbooks[1].worksheets[1];
k:=1;
for i:=1 to clb1.Items.Count do
if clb1.Checked[i-1] then
begin
if q1.FieldByName(cbFields.Items[i-1]).DataType=ftString then
if q1.FieldByName(cbFields.Items[i-1]).Size>=Length(q1.FieldByName(cbFields.Items[i-1]).DisplayLabel) then
Sheet.Columns[k].ColumnWidth:=q1.FieldByName(cbFields.Items[i-1]).Size
else
Sheet.Columns[k].ColumnWidth:=Length(q1.FieldByName(cbFields.Items[i-1]).DisplayLabel);
if c1.Checked and (trim(Edit1.Text)<>'''') then
SHeet.Cells[2,k]:=q1.FieldByName(cbFields.Items[i-1]).DisplayLabel
else
SHeet.Cells[1,k]:=q1.FieldByName(cbFields.Items[i-1]).DisplayLabel;
k:=k+1;
end;
//數據

for i:=2 to q1.RecordCount+1 do
begin
j:=1;
k:=1;
for j:=1 to clb1.Items.Count do
begin
if clb1.Checked[j-1] then
begin
if q1.FieldByName(cbFields.Items[j-1]).DataType=ftString then
Sheet.Cells[i,k].NumberFormatLocal:=''@''; //格式化為字符串
Sheet.Cells[i,k]:=q1.FieldByName(cbFields.Items[j-1]).AsString;
k:=k+1;
end;


end;
q1.Next;
end;
myExcel.Activeworkbook.Saveas(str);
end;

9楼: 同意VGA和chenybin的方法,我刚才也碰到这个问题,倒出之前加上这句代码就OK了
ExcelWorksheet1.Cells.Item[i, j + 1].NumberFormatLocal := ''@'';

10楼: 可以用最笨的一个办法
就是在导入之前,在每个字符串之前添加一个‘符号,EXCEL会默认为数据型

11楼: procedure toexcel(dbgname: Tdbgrid);
var
iRowNum, iColNum, iColumn, iRow: Integer;
vExcelApp,vSheet : Variant;
begin
if VarIsEmpty(vExcelApp) then
vExcelApp := CreateOLEObject(''Excel.Application''); //--ComObj.pas
vExcelApp.WorkBooks.Add;
vExcelApp.WorkSheets[1].Activate; //default 3 Sheets shown in Excel !
vSheet := vExcelApp.WorkSheets[1];

dbgName.DataSource.DataSet.First; //toExcel the second time, should point the cursor to the first first(先^_^)

iRowNum := dbgName.DataSource.DataSet.RecordCount;
iColNum := dbgName.Columns.Count;

vExcelApp.ActiveSheet.Rows[1].Font.Bold := True;
vExcelApp.ActiveSheet.Rows[1].Font.Size := 9;
//----------------------------------------
//data formation
vSheet.Columns[1].NumberFormatLocal := ''@'';
vSheet.Columns[10].NumberFormatLocal := ''@'';
//----------------------------------------
for iColumn := 0 to iColNum - 1 do
vSheet.Cells[1, iColumn + 1].Value := dbgName.Columns[iColumn].Title.Caption;

for iRow := 0 to iRowNum - 1 do
begin
vExcelApp.ActiveSheet.Rows[iRow + 2].Font.Size := 9;
for iColumn := 0 to iColNum - 1 do
vSheet.Cells[iRow + 2, iColumn + 1].Value := dbgName.DataSource.DataSet.FieldByname(dbgName.Columns.Items[iColumn].FieldName).AsString; //dbgName.DataSource.DataSet.Fields.Fields[iColumn].AsString; //***
dbgName.DataSource.DataSet.Next;
end;
vExcelApp.ActiveWorkBook.Saved := True;
vExcelApp.Visible := True;
end;

调用的时候:toexcel(dbgrid1); 就OK了,我好象没碰到过你的问题

12楼: 加单引号吧客户管理软件破解版