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 ...
随机推荐
- qa角色记一次测试过程回溯
一.测试过程简述 a项目依赖b项目新功能,ab项目一起提测 1.测试人员:两老一新 2.测试过程:第一轮,三人执行用例 第二轮,三人各自模块发散 第三轮,三人交叉测试 第四轮,两老投入b项目性能以及接 ...
- 【CF750E】New Year and Old Subsequence
题目大意:给定一个长度为 N 的字符串,定义一个字串是"好的",当且仅当字串中含有一个 "2017" 的子序列,且不含有 "2016" 的子 ...
- 简单使用logback日志框架
logback_config.xml 配置文件信息 <?xml version="1.0" encoding="UTF-8"?> <confi ...
- Acwing-101-最高的牛(差分)
链接: https://www.acwing.com/problem/content/103/ 题意: 有 N 头牛站成一行,被编队为1.2.3-N,每头牛的身高都为整数. 当且仅当两头牛中间的牛身高 ...
- vuex , 简单入(liao)门(jie)
vuex什么 ? 官方的说法就是 vuex是专门为vue.js应用程序开发的 状态管理模式 .并采用集中式存储 , 管理应用的所有组件的状态 ,并以相同的规则保证状态以一种可预估的方式发生变化. 自己 ...
- Python语法汇总
如果你之前学过任何一门编程语言,因为每种语言的基础语法要做的事情其实基本是相同的,只是表示方式或某些地方稍稍不同,因此在学Python的时候将它与其它你已经掌握的编程语言对比着学,这样学起来更快,效果 ...
- Huber loss<转发>
from https://blog.csdn.net/lanchunhui/article/details/50427055请移步原文
- Angular 如何修改启动的端口
在默认的情况下 Angular 启动使用的是端口 4200. 如果修改这个启动的端口,比如说我们希望再 4100 端口上启动? 可以在启动的时候添加端口参数 --port. 例如使用下面的启动命令: ...
- POJ 1995(有关快速幂运算的一道水题)
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9745 Accepted: ...
- sqlmap自动注入 --REQUEST
--delay=“参数” 每次http(s)请求之间的延迟时间,浮点数,单位为秒,默认无延迟 --timeout=“参数” 请求超时,浮点数,默认为30秒 --retries=“参数” http(s) ...