Delphi7 导出Dbgrid数据到Excel 默认路径设置为我的文档
来源:互联网 发布:java 发联机报文 编辑:程序博客网 时间:2024/05/17 19:22
下面代码是自己写的,今天有个同学打电话问我
在Delphi7 导出Dbgrid数据到Excel 正好我最近写过
也许会有更多的网友也会遇到类似的问题,把代码发下,
大家可以参考,有什么不足及需要改进的地方,请指教。
让我们共同学习。
procedure TFPeopleMgr.N6Click(Sender: TObject);
var
j: integer;
filename: string;
MSExcel,xlsheet,xlBook : Variant; //ole ComObj
reg:Tregistry;
MYDocumentPath: string;
begin
IF DBGrid1.Fields[0].AsString='' then
begin
Application.MessageBox('列表为空,无法导出!','错误操作',MB_OK+MB_ICONError);
exit;
end;
//获得“我的文档”的路径
reg:=Tregistry.create;
reg.rootkey:=HKey_Current_User;
reg.openkey('Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',false);
MYDocumentPath:=reg.READString('Personal');//“我的文档”的路径
SaveDialog1.InitialDir:=MYDocumentPath;
reg.closekey;
reg.free;
//导出数据
if TreeView_Dep.Selected=nil then
begin
SaveDialog1.FileName:='人员信息 '+datetostr(now);
end
else
SaveDialog1.FileName:=TreeView_Dep.Selected.Text+'人员信息 '+datetostr(now);
if SaveDialog1.Execute then
begin
filename := concat(SaveDialog1.FileName,'.xls');
try
MSExcel:=CreateOLEObject('Excel.Application');
xlBook:=MSExcel.WorkBooks.Add;
xlsheet:=xlBook.Worksheets['sheet1'];
xlsheet.Cells.item[1,1]:='编号';
xlsheet.Cells.item[1,2]:='姓名';
xlsheet.Cells.item[1,3]:='考勤号';
xlsheet.Cells.item[1,4]:='单位';
xlsheet.Cells.item[1,5]:='职务';
j:=1;
qe_ren.First;
while not qe_ren.Eof do begin
DBGrid1.SelectedRows.CurrentRowSelected := True;
xlsheet.Cells.item[j+1,1]:=DBGrid1.Fields[0].AsString;
xlsheet.Cells.item[j+1,2]:=DBGrid1.Fields[1].AsString;
xlsheet.Cells.item[j+1,3]:=DBGrid1.Fields[2].AsString;
xlsheet.Cells.item[j+1,4]:=DBGrid1.Fields[3].AsString;
xlsheet.Cells.item[j+1,5]:=DBGrid1.Fields[4].AsString;
qe_ren.Next;
j:=J+1;
end;//while
xlBook.SaveAs(filename);
MSExcel.WorkBooks.close;
MSExcel.Quit;
MSExcel:=unassigned;
except
Application.Messagebox('Excel没有安装!','错误提示',MB_ICONERROR+mb_Ok);
exit;
end;//try
end;// end if SaveDialog1.Execute then
end;
转载请注明出处。
- xiaojun5556
- 等级:
- szto2003
- 等级:
- Delphi7 导出Dbgrid数据到Excel 默认路径设置为我的文档
- DBGrid数据导出到Excel表格
- DBGrid数据导出到Excel表格中
- dbgrid导出到excel
- DBGrid 导出到 Excel
- 将DBGrid的数据导出到Excel文件保存
- 将DBGrid的数据导出到Excel文件保存
- 将DBGrid的数据导出到word和excel中
- 将DBGrid的数据导出到word和excel中
- 将DBGrid中的数据导出到Word和Excel
- bcb}有关DBGRID导出到EXCEL问题
- DBGrid 中的数据导出到文本文件
- 将DBGrid中的数据导出到Word
- 如何将DBGrid中的数据原样导出到Excel表中
- Delphi DBGrid数据导出到excel时,出现不正确的变量类型,怎么回事啊?
- c++builder 6.0 将DBGrid中的数据导出到Word和Excel
- dbgrid导出excel
- dbgrid导出excel
- android json解析及简单例子
- sscanf(格式化字符串输入)
- js 相对路径转为绝对路径
- Eval 方法
- OpenCV 学习笔记(1)显示图像
- Delphi7 导出Dbgrid数据到Excel 默认路径设置为我的文档
- 解决ViewFlipper中的onClick事件和onFling事件冲突
- 第7章 内存管理
- Mac OS X 10.9编译Android源码
- 解决ScrollView中填充内容上下会有阴影的问题
- Android获得屏幕大小和方向
- 浅谈onInterceptTouchEvent、onTouchEvent与onTouch 事件的分发
- MongoDB与MySQL的插入、查询性能测试
- Android UI 动画效果Animation