Delphi实现带有格式的Excel导出功能
- 功能预览
运行预览

模板样式

存储返参

导出的Excel

2. 代码实现
//执行sql的函数
procedure TForm1.GetReportData(astrsql:string);
var
strSQL,err:string; i:integer;
begin
strSQL :='set QUOTED_IDENTIFIER off '+astrsql;
//strSQL := astrsql ;
//查询 可支持多个结果集的返回
DM.qryReport.Active:=False;
DM.qryReport.Close;
DM.qryReport.SQL.Clear;
DM.qryReport.SQL.Add(strSQL);
try
DM.qryReport.Open;
except on E: Exception do
begin
showmessage('执行SQL'+strsql+'异常!'+E.Message);
Exit;
end;
end;
end;
//获取桌面的路径
function TForm1.GetShellFolders(strDir: string): string;
const
regPath = '\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders';
var
Reg: TRegistry;
strFolders: string;
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey(regPath, false) then
begin
strFolders := Reg.ReadString(strDir);
end;
finally
Reg.Free;
end;
result := strFolders;
end;
//文件导出按钮的事件
procedure TForm1.btn1Click(Sender: TObject);
var
excel,sheet :variant;
iValidRows: Integer; // 工作表的有效行
iValidCols: Integer; // 工作表的有效列
i,j,k,affect:integer;
strTemp,FieldName,reportname : string;//FieldName:对应数据的FieldName,
isList,isReadRowCol,isListLine :boolean;//isList:是否包含list,isReadRowCol:是否已经读取到了list开始的行号和列号,isListLine:本行是不是包含List
listColBegin,listColEnd:integer;//list的开始列号和结束列号
aAdoDataSetMaster,aAdoDataSetList :TADODataSet;
begin
GetReportData(trim(cbb2.Text));
aAdoDataSetMaster :=TADODataSet.Create(Self);
aAdoDataSetList:=TADODataSet.Create(Self); aAdoDataSetMaster.Recordset :=DM.qryReport.Recordset; //master数据集
aAdoDataSetList.Recordset:=DM.qryReport.NextRecordset(affect);//list数据集 excel := createoleobject('Excel.Application');
excel.WorkBooks.open(trim(cbb1.Text));
try
sheet := excel.Application.ActiveSheet;//.WorkSheets['sheet1']; iValidRows := sheet.UsedRange.Rows.Count; // 有效行数
iValidCols := sheet.UsedRange.Columns.Count; // 有效列数
//ShowMessage(IntToStr(iValidRows) + ', ' + IntToStr(iValidCols)); listColEnd := ;
isReadRowCol := false;
if aAdoDataSetList.recordcount> then //list数据集有数据
begin
isList := true
end
else
isList := false; if isList then
begin
for i := to iValidRows do
begin
for j := to iValidCols do
begin
// 读工作表单元格
strTemp := trim(sheet.Cells.Item[i, j]);
if pos('(<list>)',strTemp)= then continue;
if pos('(<list>)',strTemp)> then
begin
if listColBegin= then listColBegin := j;
listColEnd := j;
isReadRowCol := true;
end;
end;
if isReadRowCol then
begin
for k:= to aAdoDataSetList.recordcount- do
begin
excel.ActiveSheet.Rows[i+].Insert;
for j := listColBegin to listColEnd do
begin
strTemp := trim(sheet.Cells.Item[i, j]);
sheet.Cells.Item[i+, j]:=strTemp;
end
end;
break;//只要找到一个就行了
end;
end;
end;
iValidRows := sheet.UsedRange.Rows.Count; // 有效行数
iValidCols := sheet.UsedRange.Columns.Count; // 有效列数
//ShowMessage(IntToStr(iValidRows) + ', ' + IntToStr(iValidCols));
aAdoDataSetMaster.first;
aAdoDataSetList.first;
for i := to iValidRows do
begin
isListLine := false;
for j := to iValidCols do
begin
// 读工作表单元格
strTemp := trim(sheet.Cells.Item[i, j]);
if (pos('(<field>)',strTemp)=) and (pos('(<list>)',strTemp)=) then continue;
if pos('(<field>)',strTemp)> then
begin
FieldName := copy(strTemp,pos('(<field>)',strTemp)+length('(<field>)'),pos('(</field>)',strTemp)-pos('(<field>)',strTemp)-length('(<field>)'));
strTemp := stringreplace(strTemp,strTemp,aAdoDataSetMaster.FieldByName(FieldName).AsString,[rfReplaceAll]);
sheet.Cells.Item[i, j] := strTemp;
end
else if pos('(<list>)',strTemp)> then
begin
FieldName := copy(strTemp,pos('(<list>)',strTemp)+length('(<list>)'),pos('(</list>)',strTemp)-pos('(<list>)',strTemp)-length('(<list>)'));
strTemp := stringreplace(strTemp,strTemp,aAdoDataSetList.FieldByName(FieldName).AsString,[rfReplaceAll]);
sheet.Cells.Item[i, j] := strTemp;
isListLine := true;
end;
end;
if isListLine then aAdoDataSetList.Next;
end;
reportname:=GetShellFolders('Desktop')+'\外挂报表-Excel导出数据.xlsx';
sheet.SaveAs(reportname);
//excel.ActiveWorkBook.SaveAs('g:\aa.xls');
finally
excel.Quit;
sheet := Unassigned;
excel := Unassigned;
end;
showmessage('导出完成:'+reportname);
end;
Delphi实现带有格式的Excel导出功能的更多相关文章
- Atitit.excel导出 功能解决方案 php java C#.net版总集合.doc
Atitit.excel导出 功能解决方案 php java C#.net版总集合.docx 1.1. Excel的保存格式office2003 office2007/2010格式1 1.2. 类库选 ...
- 用SpringMvc实现Excel导出功能
以前只知道用poi导出Excel,最近用了SpringMvc的Excel导出功能,结合jxl和poi实现,的确比只用Poi好,两种实现方式如下: 一.结合jxl实现: 1.引入jxl的所需jar包: ...
- 利用Aspose.Cells完成easyUI中DataGrid数据的Excel导出功能
我准备在项目中实现该功能之前,google发现大部分代码都是利用一般处理程序HttpHandler实现的服务器端数据的Excel导出,但是这样存在的问题是ashx读取的数据一般都是数据库中视图的数据, ...
- excel导出功能优化
先说说优化前,怎么做EXCEL导出功能的: 1. 先定义一个VO类,类中的字段按照EXCEL的顺序定义,并且该类只能用于EXCEL导出使用,不能随便修改. 2. 将查询到的结果集循环写入到这个VO类中 ...
- excel导出功能原型
本篇博客是记录自己实现的excel导出功能原型,下面我将简单介绍本原型: 这是我自制的窗体,有一个ListView和一个Button(导出)控件. 这是我在网上找到了使用exel需要引用的库. usi ...
- java利用EasyPoi实现Excel导出功能
easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 语言( ...
- asp.net excel导出功能
以下是我在项目开发中所做的关于Excel导出功能,不足之处还望大家指正,相互学习 protected void btn_Export_Click(object sender, EventArgs e) ...
- 使用NPOI实现简单的Excel导出功能
[1]NPOI是啥? NPOI是指构建在POI 3.x版本之上的一个程序,NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作. POI是一个开源的Java读写Excel. ...
- C#导出带有格式的Excel(列宽,合并单元格,显示边框线,加背景颜色等)
源地址:http://blog.sina.com.cn/s/blog_74f702e60101au55.html 导出excel相关设置:http://blog.csdn.net/wanmingtom ...
随机推荐
- Apache提示You don't have permission to access / on this server 解决
本文链接:https://blog.csdn.net/Niu_Eva/article/details/90741894 Apache提示You don’t have permission to acc ...
- cmd 创建并写入文件
一.建立空文件的几种方法1.cd.>a.txtcd.表示改变当前目录为当前目录,即等于没改变:而且此命令不会有输出.>表示把命令输出写入到文件.后面跟着a.txt,就表示写入到a.txt. ...
- magento简化url多级分类去掉父目录
在Magento模板开发中,有时候需要将多级分类的url简化,Magento的URL默认是显示多级分类的http://afish.cnblogs.com/分类1/分类2/分类3现在需要简化为:分类2的 ...
- kotlin面向对象之抽象类、继承、多态
继承: 比较简单,直接看如何使用,这里以父亲与儿子的关系为例: 接着定义儿子并且继承父亲,如下: 是用":"号来继承的,但是此时报错了,看下错误提示: 在kotlin的类并非是人人 ...
- inode、软硬链接
关于inode是什么,可以看这篇文章:http://www.cnblogs.com/adforce/p/3522433.html 如何查看inode ll -di /boot / /app查看文件和文 ...
- 【BZOJ3143】【Luogu P3232】 [HNOI2013]游走 概率期望,图论
期望\(DP\)入门题目. 关键思想:无向边的转移作为有向边考虑.其他的就是直接上全期望公式.由于这个题目不是有向无环图,所以需要高斯消元搞一搞. 设每个点的期望经过次数是\(g(x)\),那么有 \ ...
- 京东POP店铺使用京东物流,如何拦截订单
先进入物流工作台:https://wl.jdwl.com/ 然后操作中心-销售订单查询 然后点击展开按钮 粘贴要查询的订单号 勾选订单,点击取消按钮
- js-10s倒计时后关闭窗口
效果: html: <h1 id="h1">10s后关闭窗口</h1> <a id="a1">启动</a> js ...
- Java多线程和并发(九),ReentrantLock(公平锁)
目录 1.ReentrantLock 2.ReentrantLock的实现 3.synchronized和ReentrantLock的区别 九.ReentrantLock(公平锁) 1.Reentra ...
- CF G. Indie Album AC自动机+fail树+线段树
这个套路挺有意思的. 把 $trie$ 和 $fail$ 树都建出来,然后一起跑一跑就好了~ #include <queue> #include <cstdio> #inclu ...