代码如下:
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
请专家帮忙解决,盼复,感激不尽!
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
请专家帮忙解决,盼复,感激不尽!
解决方案 »
- [Error] Unit5.pas(57): Illegal character in input file: '"' 为什么一直报这个错
- 请问如何设置Toolbar中按钮的大小? 在线等
- dbgrid的问题,大家帮忙看看
- 如何用编程为listbox1添加一项或多项内容?
- 欢迎光临!!!!!!!!!!!
- 吐血经验!!!delphi用BDE+ODBC的体制开发oralce先天性的缺陷!
- 如何取得本地计算机在互联网上的ip地址.
- 哪里下載delphi4.0和delphi5.0英文版?
- 安装QuickReport 4后---------卸载掉后出现的问题
- 大家帮我看一段小代码
- 用ADO連接MSSQL2000后.如何用ADOCommand進行查詢呢
- MEMO中光标位置发生了变化,会发生什么事件?如何把光标定位到指定的行或列?
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;