代码如下:
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
请专家帮忙解决,盼复,感激不尽!
解决方案 »
- 多个编号连着的edit如何同时清空里面的内容
- 我就是lovend,有个问题请教 aiirii(ari-淘金坑) ,
- delphi怎样连接mysql5.0
- 字符串问题? help me! TksTksTksTksTksTksTks
- dbcombobox下拉列表只可以显示当前记录值
- TIdTcpServer在连接事件和读数据事件都会触发onExecute事件,我如何判断是连接事件呢?
- 跳过槽的兄弟们进来一下,问几个问题!!!!走过路过的一定要帮忙!!先谢过!!!!
- 自定义公式问题
- 请问什么地方可以下载电脑书籍
- 请问有在南昌工作的吗?
- 用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;