1. 功能预览

运行预览

模板样式


存储返参

导出的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导出功能的更多相关文章

  1. Atitit.excel导出 功能解决方案 php java C#.net版总集合.doc

    Atitit.excel导出 功能解决方案 php java C#.net版总集合.docx 1.1. Excel的保存格式office2003 office2007/2010格式1 1.2. 类库选 ...

  2. 用SpringMvc实现Excel导出功能

    以前只知道用poi导出Excel,最近用了SpringMvc的Excel导出功能,结合jxl和poi实现,的确比只用Poi好,两种实现方式如下: 一.结合jxl实现: 1.引入jxl的所需jar包: ...

  3. 利用Aspose.Cells完成easyUI中DataGrid数据的Excel导出功能

    我准备在项目中实现该功能之前,google发现大部分代码都是利用一般处理程序HttpHandler实现的服务器端数据的Excel导出,但是这样存在的问题是ashx读取的数据一般都是数据库中视图的数据, ...

  4. excel导出功能优化

    先说说优化前,怎么做EXCEL导出功能的: 1. 先定义一个VO类,类中的字段按照EXCEL的顺序定义,并且该类只能用于EXCEL导出使用,不能随便修改. 2. 将查询到的结果集循环写入到这个VO类中 ...

  5. excel导出功能原型

    本篇博客是记录自己实现的excel导出功能原型,下面我将简单介绍本原型: 这是我自制的窗体,有一个ListView和一个Button(导出)控件. 这是我在网上找到了使用exel需要引用的库. usi ...

  6. java利用EasyPoi实现Excel导出功能

    easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 语言( ...

  7. asp.net excel导出功能

    以下是我在项目开发中所做的关于Excel导出功能,不足之处还望大家指正,相互学习 protected void btn_Export_Click(object sender, EventArgs e) ...

  8. 使用NPOI实现简单的Excel导出功能

    [1]NPOI是啥? NPOI是指构建在POI 3.x版本之上的一个程序,NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作. POI是一个开源的Java读写Excel. ...

  9. C#导出带有格式的Excel(列宽,合并单元格,显示边框线,加背景颜色等)

    源地址:http://blog.sina.com.cn/s/blog_74f702e60101au55.html 导出excel相关设置:http://blog.csdn.net/wanmingtom ...

随机推荐

  1. web开发规范文档二

    头部        header\hd 内容块      content\con\bd text txt title 尾部        footer 导航        nav\menu sub-n ...

  2. java string split 怎么保留尾部空字符串

    # 不保留尾部空字符串 public class QQ { public static void main(String[] args) { String str = "a,b,c,d,&q ...

  3. 百度网盘,FTP上传异常、上传失败的解决办法

    若你的宽带上传上限速度为50KB,那么将百度网盘或FTP的上行速度调为50KB以下即可,就不会出现网络异常的情况了.

  4. 背包 || NOIP 2018 D1 T2 || Luogu P5020 货币系统

    题面:P5020 货币系统 题解: 显然要求的货币系统是当前货币系统的子集时答案会更优,于是考虑从当前货币系统中删数 一个大数如果能被其他小数表示出来,它就可以去掉 把数据排个序去个重,然后直接背包 ...

  5. jenkins之Extended Choice Parameter多选插件

  6. 在qt的QOpenGLWidget开启opengl的抗锯齿

    在QOpenGLWidget的构造函数添加下面几句代码即可 QSurfaceFormat surfaceFormat; surfaceFormat.setSamples();//多重采样 setFor ...

  7. 关键字static在标准C/C++的作用

    static总结:根据作用域,存储域,生命周期3点来说 static含义: 是C/C++中很常用的修饰符,它被用来控制变量的存储方式和可见性. (1)标准C语言中,static的最主要功能是隐藏,其次 ...

  8. python环境下安装opencv库的方法

    注意:安装opencv之前需要先安装numpy,matplotlib等 一.安装方法 方法一.在线安装 1.先安装opencv-python pip install opencv-python --u ...

  9. (Java多线程系列四)停止线程

    停止线程 停止线程的思路 ①使用退出标志,使线程正常退出,也就是当run()方法结束后线程终止. class Thread01 extends Thread { // volatile关键字解决线程的 ...

  10. PyCharm中Qt Designer+PyUIC配置

    本文环境配置: 系统=>windows10:64位 语言=>Python:3.7.1 第三方库PyQT5:5.11.3pyqt5-tools:5.11.3.1.4 工具=>PyCha ...