如何将银行账号等长数据改为文本格式(10)

  • 主题发起人 主题发起人 lljimo
  • 开始时间 开始时间
L

lljimo

Unregistered / Unconfirmed
GUEST, unregistred user!
银行账号一般都很长有23位的,导出到excel结果是 7.2042E+20 如何将其变成文本格式,我写的代码如下: 需要将第二列改成文本格式,第四列是数字格式 var ExcelApplication1:TExcelApplication; ExcelWorksheet1:TExcelWorksheet; ExcelWorkbook1:TExcelWorkbook; i,j:integer; begin try ExcelApplication1:=TExcelApplication.Create(Application); ExcelWorksheet1:=TExcelWorksheet.Create(Application); ExcelWorkbook1:=TExcelWorkbook.Create(Application); ExcelApplication1.Connect; ExcelApplication1.Visible[0]:=True; except Application.Messagebox('Excel没有安装!','Hello',MB_ICONERROR+mb_Ok); Abort; end; try ExcelApplication1.Workbooks.Add(EmptyParam,0); ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]); ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1]as _worksheet); DBGridEh1.DataSource.DataSet.First; for j:=0 to DBGridEh1.DataSource.DataSet.Fields.Count-1 do begin ExcelWorksheet1.Cells.item[1,j+1]:=DBGridEh1.Columns[j].Title.Caption;//导出DBGridEh1中显示的字段标题 //ExcelWorksheet1.Cells.item[3,j+1]:=DBGridEh1.DataSource.DataSet.Fields[j].DisplayLabel;//导出数据库中字段名 ExcelWorksheet1.Cells.item[1,j+1].font.size:='10'; end; for i:=2 to DBGridEh1.DataSource.DataSet.RecordCount + 1 do //从excel的第2行到最后 begin for j:= 0 to DBGridEh1.DataSource.DataSet.Fields.Count-1 do //从dbgrid4的第1列到最后 begin //ExcelWorksheet1.Cells.item[i,j+1]:=DBGridEh1.DataSource.DataSet.Fields[j].Asstring; //ExcelWorksheet1.Cells.item[i,j+1].font.size:='10'; ExcelWorksheet1.Cells.item[i,j+1]:=DBGridEh1.DataSource.DataSet.Fields[j].Asstring; ExcelWorksheet1.Cells.item[i,j+1].font.size:='10'; end; DBGridEh1.DataSource.DataSet.Next; end; ExcelWorksheet1.Columns.AutoFit; // ExcelWorksheet1.Cells.item[1,2]:='预测表'+Formatdatetime('YYYYmmdd',Now);//DBGridEh1.Columns[0].FieldName; //在excel中第一行显示标题,产品物料明晰表 ExcelWorksheet1.Cells.Item[1,2].font.size:='14'; finally ExcelWorkSheet1.Disconnect; ExcelWorkBook1.Disconnect; ExcelApplication1.Disconnect; end;应该如何修改
 
创建 Excel 时, 先设置好 帐号 对应列的 单元格格式 为字符串, 应该就可以了
 
try ExcelApplication1.Workbooks.Add(EmptyParam,0); ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]); ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1]as _worksheet); [red]ExcelApp.WorkSheets[1].Columns['R:R'].NumberFormatLocal:='@';//把excel的R列格式化为文本~~[/red] DBGridEh1.DataSource.DataSet.First;
 
在excel里面,有时候在长数据前面加‘ 来显示成文本
 
请高手指教
 
楼上两位已经说的很清楚了
 
来自:de410, 时间:2009-7-2 12:32:05, ID:3965999 | 编辑 try ExcelApplication1.Workbooks.Add(EmptyParam,0); ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]); ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1]as _worksheet); ********************************************************** ExcelApp.WorkSheets[1].Columns['B:B'].NumberFormatLocal:='@';//把excel的第二列格式化为文本~~ ********************************************************** DBGridEh1.DataSource.DataSet.First;
 
运行的时候 提示错误[Error] bank.pas(140): Undeclared identifier: 'Columns' 怎么回事
 
1233 3434 5454 这样肯定可以!
 
ExcelApp.ActiveSheet.Columns[2].NumberFormatLocal:='@';
 
还是那个提示错误
 
你把你导出的代码贴出来,我看看~~
 
procedure TFormbank.Button2Click(Sender: TObject);var ExcelApplication1:TExcelApplication; ExcelWorksheet1:TExcelWorksheet; ExcelWorkbook1:TExcelWorkbook; i,j:integer; begin try ExcelApplication1:=TExcelApplication.Create(Application); ExcelWorksheet1:=TExcelWorksheet.Create(Application); ExcelWorkbook1:=TExcelWorkbook.Create(Application); ExcelApplication1.Connect; ExcelApplication1.Visible[0]:=True; except Application.Messagebox('Excel没有安装!','Hello',MB_ICONERROR+mb_Ok); Abort; end; try ExcelApplication1.Workbooks.Add(EmptyParam,0); ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]); ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1]as _worksheet); // ExcelApplication1.WorkSheets[1].Columns['B:B'].NumberFormatLocal:='@'; // ExcelApplication1.ActiveSheet.Columns[2].NumberFormatLocal:='@'; DBGridEh1.DataSource.DataSet.First; for j:=0 to DBGridEh1.DataSource.DataSet.Fields.Count-1 do begin ExcelWorksheet1.Cells.item[1,j+1]:=DBGridEh1.Columns[j].Title.Caption;//导出DBGridEh1中显示的字段标题 //ExcelWorksheet1.Cells.item[3,j+1]:=DBGridEh1.DataSource.DataSet.Fields[j].DisplayLabel;//导出数据库中字段名 ExcelWorksheet1.Cells.item[1,j+1].font.size:='10'; end; for i:=2 to DBGridEh1.DataSource.DataSet.RecordCount + 1 do //从excel的第2行到最后 begin for j:= 0 to DBGridEh1.DataSource.DataSet.Fields.Count-1 do //从dbgrid4的第1列到最后 begin //ExcelWorksheet1.Cells.item[i,j+1]:=DBGridEh1.DataSource.DataSet.Fields[j].Asstring; //ExcelWorksheet1.Cells.item[i,j+1].font.size:='10'; ExcelWorksheet1.Cells.item[i,j+1]:=DBGridEh1.DataSource.DataSet.Fields[j].Asstring; ExcelWorksheet1.Cells.item[i,j+1].font.size:='10'; ExcelWorksheet1.Cells.NumberFormatLocal:='@'; end; DBGridEh1.DataSource.DataSet.Next; end; ExcelWorksheet1.Columns.AutoFit; // ExcelWorksheet1.Cells.item[1,2]:='预测表'+Formatdatetime('YYYYmmdd',Now);//DBGridEh1.Columns[0].FieldName; //在excel中第一行显示标题,产品物料明晰表 ExcelWorksheet1.Cells.Item[1,2].font.size:='14'; finally ExcelWorkSheet1.Disconnect; ExcelWorkBook1.Disconnect; ExcelApplication1.Disconnect; end;end;end.
 
没有知道怎么解决吗
 
for i:=2 to DBGridEh1.DataSource.DataSet.RecordCount + 1 do //从excel的第2行到最后 begin for j:= 0 to DBGridEh1.DataSource.DataSet.Fields.Count-1 do //从dbgrid4的第1列到最后 begin //ExcelWorksheet1.Cells.item[i,j+1]:=DBGridEh1.DataSource.DataSet.Fields[j].Asstring; //ExcelWorksheet1.Cells.item[i,j+1].font.size:='10'; ExcelWorksheet1.Cells.item[i,j+1]:=DBGridEh1.DataSource.DataSet.Fields[j].Asstring;****************************************ExcelWorksheet1.Range[ExcelWorksheet1.Cells.item[1,2], ExcelWorksheet1.Cells.item[i,2]].NumberFormatLocal:='@';//第二列设为文本格式**************************************** ExcelWorksheet1.Cells.item[i,j+1].font.size:='10'; ExcelWorksheet1.Cells.NumberFormatLocal:='@'; end;
 
另外 问下 如果想要第四列变成数值格式 如 1,200.10 这种如何实现
 
ExcelWorksheet1.Range[ExcelWorksheet1.Cells.item[1,4], ExcelWorksheet1.Cells.item[i,4]].NumberFormatLocal:='#,###,###0.00';
 

Similar threads

I
回复
0
查看
855
import
I
I
回复
0
查看
619
import
I
I
回复
0
查看
850
import
I
I
回复
0
查看
664
import
I
后退
顶部