这段代码当中,如果下面8个字段都不是空的就可以完全导出。
当其中某个字段是空的话就出现错误,请高手指点如何解决从dbgrid中把数据到到excel完整的处理过程。谢谢!!!
下面为本人的写的部分代码:
procedure Tkfb_query.Button8Click(Sender: TObject);
var
XL,Sheet: Variant;
FileName: String;
i,row: integer;begin if DM.kfb_hetongchaxun.Active =true then
begin
if DM.kfb_hetongchaxun.RecordCount >0 then
begin
Screen.Cursor:=crSqlWait;
try
FileName:=ExtractFilePath(Application.ExeName)+'合同信息.xls';
XL:=CreateOLEObject('Excel.Application');
XL.WorkBooks.open(FileName);
Sheet:=XL.WorkBooks[1].WorkSheets['合同信息'];
Sheet.Activate;
except
MessageDlg('【合同信息】文件不存在或已经被破坏!'+#13+'无法完成导出。',mtWarning,[mbOk],0);
Screen.Cursor:=crDefault;
Exit;
end;
try
row:=8;//the number of row
for i:=1 to DM.kfb_hetongchaxun.RecordCount do
begin
Sheet.Cells[row,1]:=Trim(DM.kfb_hetongchaxun.FieldValues['编号']);//
Sheet.Cells[row,2]:=Trim(DM.kfb_hetongchaxun.FieldValues['负责人']);//
Sheet.Cells[row,3]:=Trim(DM.kfb_hetongchaxun.FieldValues['校内单位']);//
Sheet.Cells[row,4]:=Trim(DM.kfb_hetongchaxun.FieldValues['合同编号']);//
Sheet.Cells[row,5]:=DM.kfb_hetongchaxun.FieldValues['项目名称'];//
Sheet.Cells[row,6]:=DM.kfb_hetongchaxun.FieldValues['项目性质'];//
Sheet.Cells[row,7]:=DM.kfb_hetongchaxun.FieldValues['合作单位'];//
Sheet.Cells[row,8]:=DM.kfb_hetongchaxun.FieldValues['合同额'];
row:=row+1;
DM.kfb_hetongchaxun.Next;//the next of master table
end;
XL.visible:=true;
Screen.Cursor:=crDefault;
except
MessageDlg('导出Excel错误!',mtError,[mbOk],0);
Screen.Cursor:=CrDefault;
end;
end;
end;
end;
当其中某个字段是空的话就出现错误,请高手指点如何解决从dbgrid中把数据到到excel完整的处理过程。谢谢!!!
下面为本人的写的部分代码:
procedure Tkfb_query.Button8Click(Sender: TObject);
var
XL,Sheet: Variant;
FileName: String;
i,row: integer;begin if DM.kfb_hetongchaxun.Active =true then
begin
if DM.kfb_hetongchaxun.RecordCount >0 then
begin
Screen.Cursor:=crSqlWait;
try
FileName:=ExtractFilePath(Application.ExeName)+'合同信息.xls';
XL:=CreateOLEObject('Excel.Application');
XL.WorkBooks.open(FileName);
Sheet:=XL.WorkBooks[1].WorkSheets['合同信息'];
Sheet.Activate;
except
MessageDlg('【合同信息】文件不存在或已经被破坏!'+#13+'无法完成导出。',mtWarning,[mbOk],0);
Screen.Cursor:=crDefault;
Exit;
end;
try
row:=8;//the number of row
for i:=1 to DM.kfb_hetongchaxun.RecordCount do
begin
Sheet.Cells[row,1]:=Trim(DM.kfb_hetongchaxun.FieldValues['编号']);//
Sheet.Cells[row,2]:=Trim(DM.kfb_hetongchaxun.FieldValues['负责人']);//
Sheet.Cells[row,3]:=Trim(DM.kfb_hetongchaxun.FieldValues['校内单位']);//
Sheet.Cells[row,4]:=Trim(DM.kfb_hetongchaxun.FieldValues['合同编号']);//
Sheet.Cells[row,5]:=DM.kfb_hetongchaxun.FieldValues['项目名称'];//
Sheet.Cells[row,6]:=DM.kfb_hetongchaxun.FieldValues['项目性质'];//
Sheet.Cells[row,7]:=DM.kfb_hetongchaxun.FieldValues['合作单位'];//
Sheet.Cells[row,8]:=DM.kfb_hetongchaxun.FieldValues['合同额'];
row:=row+1;
DM.kfb_hetongchaxun.Next;//the next of master table
end;
XL.visible:=true;
Screen.Cursor:=crDefault;
except
MessageDlg('导出Excel错误!',mtError,[mbOk],0);
Screen.Cursor:=CrDefault;
end;
end;
end;
end;
解决方案 »
- Delphi 移动指定文件到指定位置。高手进。
- 怎样实现dbgridEH选中多条记录,写到一个窗体(ADOQUERY2)中?
- 招聘人才(需较强开发能力和文档写作能力,深圳,月薪6000左右)
- 大家找女朋友的标准是什么?明天要见网上女友,这是mail
- 求工资管理系统源代码(有计算公式功能)
- 急问:怎么对数据库进行多线程操作??
- 请提供列车查询,有票价(包括卧铺)显示的网站,
- 怎么样将一个文本和图片混合的文件中的图片流数据提取出来并且保存到一个图片文件中
- 怎样把一字符串插入到TMemo中当前光标位置?
- 再没人回答我的问题,我就给你们送炸弹了!
- form排列,怎么实现层叠排列,水平排列,竖直排列,全部最小化?
- 请问两部计算机怎样通过ADSL进行远程通讯?
var
XL,Sheet: Variant;
FileName: String;
i,row: integer;begin if DM.kfb_hetongchaxun.Active =true then
begin
if DM.kfb_hetongchaxun.RecordCount >0 then
begin
Screen.Cursor:=crSqlWait;
try
FileName:=ExtractFilePath(Application.ExeName)+'合同信息.xls';
XL:=CreateOLEObject('Excel.Application');
XL.WorkBooks.open(FileName);
Sheet:=XL.WorkBooks[1].WorkSheets['合同信息'];
Sheet.Activate;
except
MessageDlg('【合同信息】文件不存在或已经被破坏!'+#13+'无法完成导出。',mtWarning,[mbOk],0);
Screen.Cursor:=crDefault;
Exit;
end;
try
row:=8;//the number of row
for i:=1 to DM.kfb_hetongchaxun.RecordCount do
begin
if varisnull(Trim(DM.kfb_hetongchaxun.FieldValues['编号'])) then
Sheet.Cells[row,1]:=''
else
Sheet.Cells[row,1]:=Trim(DM.kfb_hetongchaxun.FieldValues['编号']);//
if varisnull(DM.kfb_hetongchaxun.FieldValues['负责人']) then
Sheet.Cells[row,2]:=''
else
Sheet.Cells[row,2]:=Trim(DM.kfb_hetongchaxun.FieldValues['负责人']);//
if varisnull(DM.kfb_hetongchaxun.FieldValues['校内单位']) then
Sheet.Cells[row,3]:=''
else
Sheet.Cells[row,3]:=Trim(DM.kfb_hetongchaxun.FieldValues['校内单位'])//
if varisnull(DM.kfb_hetongchaxun.FieldValues['合同编号']) then
Sheet.Cells[row,4]:=''
else
Sheet.Cells[row,4]:=Trim(DM.kfb_hetongchaxun.FieldValues['合同编号']);//
if varisnull(DM.kfb_hetongchaxun.FieldValues['项目名称']) then
Sheet.Cells[row,5]:=''
else
Sheet.Cells[row,5]:=DM.kfb_hetongchaxun.FieldValues['项目名称'];//
if varisnull(DM.kfb_hetongchaxun.FieldValues['项目性质']) then
Sheet.Cells[row,6]:=''
else
Sheet.Cells[row,6]:=DM.kfb_hetongchaxun.FieldValues['项目性质'];//
if varisnull(DM.kfb_hetongchaxun.FieldValues['合作单位']) then
Sheet.Cells[row,6]:=''
else
Sheet.Cells[row,7]:=DM.kfb_hetongchaxun.FieldValues['合作单位'];//
if varisnull(DM.kfb_hetongchaxun.FieldValues['合同额']) then
Sheet.Cells[row,8]:=0
else
Sheet.Cells[row,8]:=DM.kfb_hetongchaxun.FieldValues['合同额'];
row:=row+1;
DM.kfb_hetongchaxun.Next;//the next of master table
end;
XL.visible:=true;
Screen.Cursor:=crDefault;
except
MessageDlg('导出Excel错误!',mtError,[mbOk],0);
Screen.Cursor:=CrDefault;
end;
end;
end;
end;
求高手再指点!!!!
//写入标题
range :=varexcel.workbooks[1].worksheets[1].columns;
for i:=0 to kfb_query.ListBox2.items.count -1 do
begin
varexcel.workbooks[1].worksheets[1].cell[2,i+1].value := kfb_query.ListBox2.items.strings[i];// varexcel.workbooks[1].worksheets[1].cell[2,i+1] :=Trim(kfb_query.ListBox2.items[i]); varexcel.workbooks[1].worksheets[1].cell[2,i+1].Font.bold :=true;
// range.columns[i+1].columnwidth := DM.kfb_hetongchaxun.FieldByName(Trim(kfb_query.ListBox2.items[i])).Displaywidth;
range.columns[i+1].columnwidth := DM.kfb_hetongchaxun.FieldByName(kfb_query.ListBox2.items.strings[i]).Displaywidth;
range.columns[i+1].HorizontalAlignment :=xlCenter;
end;