dbgrideh导出到execl 多表头(转自NPC)

本文介绍了一个用于导出Excel报表的Delphi程序,该程序能够处理多层级列标题,并支持多种导出格式,如TXT、CSV、HTML、RTF和XLS。文章详细描述了如何通过OLE Automation对象操作Excel应用,包括创建工作簿、设置单元格格式、合并单元格以及自定义标题和统计数据等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


//*****************************************************************************
// NPC Add This 2019-05-15 16:29:54
// ----------------------------------------------------------------------------
// 名 称:TfrmPublic.ExpDBGridEh_Execl
// ----------------------------------------------------------------------------
// 用 途:导出Execl ,带多头 , 最后后并列为特殊处理,
// ----------------------------------------------------------------------------
// 参 数:
// ZhiBR: 制表人
// ZhiBSJ: 制表时间
// MyTitle: 标题
// ShiJ1: 统计开始时间
// ShiJ2: 统计截止时间
// MyDBGridEh: 数据源
// TeSCL: 药房销售中草药报表专用 , 其他功能可以参考
// ----------------------------------------------------------------------------
// 返回值:无
// ----------------------------------------------------------------------------
// 备 注: 引用单元 ComObj //TeSCL = True 药房销售中草药报表专用 , 其他功能可以参考
//*****************************************************************************

procedure TfrmPublic.ExpDBGridEh_Execl(ZhiBR , ZhiBSJ , MyTitle , ShiJ1 , ShiJ2: string; MyDBGridEh: TDBGridEh ; TeSCL:Boolean = False);
var
ExpClass: TDBGridEhExportClass;
Ext: string;
HDataRow:integer;
eclApp,WorkBook:Variant; {声明为OLE Automation对象}
xlsFileName:String;
iCol , iRow:Integer;
MaxNumCol , i , z:Integer;
ColumnsTitleList:TStringList;
StartCol , EndCol:Integer;
FirstNR:String;
begin

SaveDialog1.FileName := MyTitle;
SaveDialog1.FilterIndex :=5;

if not SaveDialog1.Execute then
exit;

case SaveDialog1.FilterIndex of
1:
begin
ExpClass := TDBGridEhExportAsText;
Ext := 'txt';
end;
2:
begin
ExpClass := TDBGridEhExportAsCSV;
Ext := 'csv';
end;
3:
begin
ExpClass := TDBGridEhExportAsHTML;
Ext := 'htm';
end;
4:
begin
ExpClass := TDBGridEhExportAsRTF;
Ext := 'rtf';
end;
5:
begin
ExpClass := TDBGridEhExportAsXLS;
Ext := 'xls';
end;
else
ExpClass := nil;
Ext := '';
end;

if ExpClass = nil then
exit;

if UpperCase(Copy(SaveDialog1.FileName, Length(SaveDialog1.FileName) - 2, 3)) <> UpperCase(Ext) then
SaveDialog1.FileName := SaveDialog1.FileName + '.' + Ext;

try
ColumnsTitleList := TStringList.Create;
ColumnsTitleList.Delimiter := '|';

MyDBGridEh.DataSource.DataSet.DisableControls;
try
{创建OLE对象:Excel Application与WordBook}
eclApp := CreateOleObject('Excel.Application');
WorkBook:=CreateOleObject('Excel.Sheet');
Except
Application.MessageBox('你的机器没有安装Microsoft Excel', '使用Microsoft Excel',MB_OK+MB_ICONWarning);
Exit;
End;

HDataRow := 3 ;
xlsFileName:= SaveDialog1.FileName;

EclApp.Caption := '应用程序调用 Microsoft Excel1';

WorkBook:=eclApp.workbooks.Add;
EclApp.DisplayAlerts := False ; //不提示弹出对话框
EclApp.Cells(1,1):= MyTitle;

EclApp.Range[EclApp.Cells[1,1],EclApp.Cells[1,MyDBGridEh.FieldCount]].MergeCells:=True; //合并
EclApp.Range[EclApp.Cells[1,1],EclApp.Cells[1,MyDBGridEh.FieldCount]].HorizontalAlignment :=3; //水平居中
EclApp.Range[EclApp.Cells[1,1],EclApp.Cells[1,MyDBGridEh.FieldCount]].VerticalAlignment := 3; //垂直居中
EclApp.Range[EclApp.Cells[1,1],EclApp.Cells[1,MyDBGridEh.FieldCount]].font.size:=20; //设置单元格的字体大小
EclApp.Range[EclApp.Cells[1,1],EclApp.Cells[1,MyDBGridEh.FieldCount]].font.name:='宋体'; //字体格式
EclApp.Range[EclApp.Cells[1,1],EclApp.Cells[1,MyDBGridEh.FieldCount]].Borders.LineStyle := 1;//加边框

EclApp.Cells(2,1):= '统计时间:' + ShiJ1 + '至' + ShiJ2;

EclApp.Range[EclApp.Cells[2,1],EclApp.Cells[2,MyDBGridEh.FieldCount]].MergeCells:=True; //合并
EclApp.Range[EclApp.Cells[2,1],EclApp.Cells[2,MyDBGridEh.FieldCount]].font.size:=11; //设置单元格的字体大小
EclApp.Range[EclApp.Cells[2,1],EclApp.Cells[2,MyDBGridEh.FieldCount]].font.name:='宋体'; //字体格式
EclApp.Range[EclApp.Cells[2,1],EclApp.Cells[2,MyDBGridEh.FieldCount]].Borders.LineStyle := 1;//加边框

MaxNumCol := 1;
//格式化多层列头
//计算出最大的多层列数
for i := 0 to MyDBGridEh.FieldCount - 1 do
begin
ColumnsTitleList.Clear;
ColumnsTitleList.DelimitedText := MyDBGridEh.Columns[i].Title.Caption;
if ColumnsTitleList.Count> MaxNumCol then
MaxNumCol := ColumnsTitleList.Count;
end;

//生成电子表格多层列
for iCol := 0 to MyDBGridEh.FieldCount - 1 do
begin
ColumnsTitleList.Clear;
ColumnsTitleList.DelimitedText := MyDBGridEh.Columns[iCol].Title.Caption;
for i := 0 to MaxNumCol - 1 do
begin
if i <= ColumnsTitleList.Count - 1 then
EclApp.Cells(HDataRow + i, iCol + 1):= ColumnsTitleList[i]
else
EclApp.Cells(HDataRow + i, iCol + 1):= '';
EclApp.Cells[HDataRow + i, iCol + 1].font.size:=11; //设置单元格的字体大小
EclApp.Cells[HDataRow + i, iCol + 1].font.name:='宋体'; //字体格式
EclApp.Cells[HDataRow + i, iCol + 1].Borders.LineStyle := 1;//加边框
end;
end;

//合并列头
iCol := 0;
StartCol := -1;
EndCol := -1;
for iCol := 0 to MyDBGridEh.FieldCount - 1 do
begin
ColumnsTitleList.Clear;
ColumnsTitleList.DelimitedText := MyDBGridEh.Columns[iCol].Title.Caption;
//如果只有一层,对1至最大层进行合并
if ColumnsTitleList.Count = 1 then
EclApp.Range[EclApp.Cells[HDataRow , iCol + 1] , EclApp.Cells[HDataRow + MaxNumCol - 1 , iCol + 1]].MergeCells:=True;
end;

for i := 0 to MaxNumCol - 1 do
begin
iCol := 0;
while iCol <= MyDBGridEh.FieldCount - 1 do
begin
ColumnsTitleList.Clear;
ColumnsTitleList.DelimitedText := MyDBGridEh.Columns[iCol].Title.Caption;
if ColumnsTitleList.Count = 1 then
begin
Inc(iCol);
continue;
end;
if i <= (ColumnsTitleList.Count - 1 - i) then
begin
StartCol := iCol + 1;
FirstNR := ColumnsTitleList[i];
for z := StartCol to MyDBGridEh.FieldCount - 1 do
begin
ColumnsTitleList.Clear;
ColumnsTitleList.DelimitedText := MyDBGridEh.Columns[z].Title.Caption;
if i <= (ColumnsTitleList.Count - 1 - i) then
begin
if FirstNR <> ColumnsTitleList[i] then
begin
EndCol := z;
iCol := z;
Break;
end;
end;
end;
EclApp.Range[EclApp.Cells[HDataRow + i , StartCol] , EclApp.Cells[HDataRow + i , EndCol]].MergeCells:=True;
EclApp.Range[EclApp.Cells[HDataRow + i , StartCol] , EclApp.Cells[HDataRow + i , EndCol]].HorizontalAlignment :=3; //水平居中
Continue;
end;
Inc(iCol);
end;
end;

////////////////////////////////////////////////////////////////////////////////////////

//生成列头 不处理多层列头
// iCol := 0;
// for iCol := 0 to MyDBGridEh.FieldCount - 1 do
// begin
// EclApp.Cells(HDataRow , iCol + 1):= MyDBGridEh.Columns[iCol].Title.Caption;
// EclApp.Cells[HDataRow , iCol + 1].font.size:=11; //设置单元格的字体大小
// EclApp.Cells[HDataRow , iCol + 1].font.name:='宋体'; //字体格式
// EclApp.Cells[HDataRow , iCol + 1].Borders.LineStyle := 1;//加边框
// end;


HDataRow := HDataRow + MaxNumCol - 1;

iRow := 1;//生成行数据
MyDBGridEh.DataSource.DataSet.First;
While not MyDBGridEh.DataSource.DataSet.Eof do
begin

iCol := 0;
for iCol := 0 to MyDBGridEh.FieldCount - 1 do
begin
EclApp.Cells(iRow + HDataRow,iCol + 1):= MyDBGridEh.Columns[iCol].DisplayText ;//MyDBGridEh.Fields[iCol].Value;
EclApp.Cells[iRow + HDataRow,iCol + 1].font.size:=11; //设置单元格的字体大小
EclApp.Cells[iRow + HDataRow,iCol + 1].font.name:='宋体'; //字体格式
EclApp.Cells[iRow + HDataRow,iCol + 1].Borders.LineStyle := 1;//加边框
end;

inc(iRow);
MyDBGridEh.DataSource.DataSet.Next;
end;

 

EclApp.Cells(iRow + HDataRow,1):= '制表人:' + ZhiBR;//MyDBGridEh.Fields[iCol].Value;
EclApp.Range[EclApp.Cells[iRow + HDataRow,1],EclApp.Cells[iRow + HDataRow,MyDBGridEh.FieldCount]].MergeCells:=True; //合并
EclApp.Range[EclApp.Cells[iRow + HDataRow,1],EclApp.Cells[iRow + HDataRow,MyDBGridEh.FieldCount]].HorizontalAlignment := $FFFFEFC8; //xlcenter //水平居中
EclApp.Range[EclApp.Cells[iRow + HDataRow,1],EclApp.Cells[iRow + HDataRow,MyDBGridEh.FieldCount]].font.size:=11; //设置单元格的字体大小
EclApp.Range[EclApp.Cells[iRow + HDataRow,1],EclApp.Cells[iRow + HDataRow,MyDBGridEh.FieldCount]].font.name:='宋体'; //字体格式
EclApp.Range[EclApp.Cells[iRow + HDataRow,1],EclApp.Cells[iRow + HDataRow,MyDBGridEh.FieldCount]].Borders.LineStyle := 1;//加边框

inc(iRow);
EclApp.Cells(iRow + HDataRow,1):= '制表日期:' + ZhiBSJ ;//MyDBGridEh.Fields[iCol].Value;
EclApp.Range[EclApp.Cells[iRow + HDataRow,1],EclApp.Cells[iRow + HDataRow,MyDBGridEh.FieldCount]].MergeCells:=True; //合并
EclApp.Range[EclApp.Cells[iRow + HDataRow,1],EclApp.Cells[iRow + HDataRow,MyDBGridEh.FieldCount]].HorizontalAlignment := $FFFFEFC8; //xlcenter //水平居中
EclApp.Range[EclApp.Cells[iRow + HDataRow,1],EclApp.Cells[iRow + HDataRow,MyDBGridEh.FieldCount]].font.size:=11; //设置单元格的字体大小
EclApp.Range[EclApp.Cells[iRow + HDataRow,1],EclApp.Cells[iRow + HDataRow,MyDBGridEh.FieldCount]].font.name:='宋体'; //字体格式
EclApp.Range[EclApp.Cells[iRow + HDataRow,1],EclApp.Cells[iRow + HDataRow,MyDBGridEh.FieldCount]].Borders.LineStyle := 1;//加边框

//药房销售中草药报表专用 , 其他功能可以参考
if TeSCL then
begin
//处理合并功能
i := 0;
iRow := 1;//生成行数据
MyDBGridEh.DataSource.DataSet.First;
i := MyDBGridEh.DataSource.DataSet.FieldByName('XH').AsInteger;
StartCol := iRow + HDataRow;
iCol := MyDBGridEh.FieldCount;

While not MyDBGridEh.DataSource.DataSet.Eof do
begin
if i <> MyDBGridEh.DataSource.DataSet.FieldByName('XH').AsInteger then
begin
EndCol := iRow + HDataRow - 1;
EclApp.Range[EclApp.Cells[StartCol , iCol] , EclApp.Cells[EndCol , iCol]].MergeCells:=True;
StartCol := iRow + HDataRow;
i := MyDBGridEh.DataSource.DataSet.FieldByName('XH').AsInteger;
end;
inc(iRow);
MyDBGridEh.DataSource.DataSet.Next;
end;

EndCol := iRow + HDataRow - 1;
EclApp.Range[EclApp.Cells[StartCol , iCol] , EclApp.Cells[EndCol , iCol]].MergeCells:=True;
end;
//EclApp.Rows.EntireColumn.AutoFit;//excel自动调整列

try
//保存文件
WorkBook.SaveAS(xlsFileName);
MSGMessage('保存完成!');
except
on e:exception do
begin
Showmessage(e.Message);
end;
end;
finally
FreeAndNil(ColumnsTitleList);
MyDBGridEh.DataSource.DataSet.EnableControls;
WorkBook.close;
EclApp.Quit;
{释放Variant变量}
WorkBook := Unassigned;
eclApp:=Unassigned;
end;

end;

转载于:https://www.cnblogs.com/maweiwei/p/10870419.html

【基于QT的调色板】是一个使用Qt框架开发的色彩选择工具,类似于Windows操作系统中常见的颜色选取器。Qt是一个跨平台的应用程序开发框架,广泛应用于桌面、移动和嵌入式设备,支持C++和QML语言。这个调色板功能提供了横竖两种渐变模式,用户可以方便地选取所需的颜色值。 在Qt中,调色板(QPalette)是一个关键的类,用于管理应用程序的视觉样式。QPalette包含了一系列的颜色角色,如背景色、前景色、文本色、高亮色等,这些颜色可以根据用户的系统设置或应用程序的需求进行定制。通过自定义QPalette,开发者可以创建具有独特视觉风格的应用程序。 该调色板功能可能使用了QColorDialog,这是一个标准的Qt对话框,允许用户选择颜色。QColorDialog提供了一种简单的方式来获取用户的颜色选择,通常包括一个调色板界面,用户可以通过滑动或点击来选择RGB、HSV或其他色彩模型中的颜色。 横渐变取色可能通过QGradient实现,QGradient允许开发者创建线性或径向的色彩渐变。线性渐变(QLinearGradient)沿直线从一个点到另一个点过渡颜色,而径向渐变(QRadialGradient)则以圆心为中心向外扩散颜色。在调色板中,用户可能可以通过滑动条或鼠标拖动来改变渐变的位置,从而选取不同位置的颜色。 竖渐变取色则可能是通过调整QGradient的方向来实现的,将原本水平的渐变方向改为垂直。这种设计可以提供另一种方式来探索颜色空间,使得选取颜色更为直观和便捷。 在【colorpanelhsb】这个文件名中,我们可以推测这是与HSB(色相、饱和度、亮度)色彩模型相关的代码或资源。HSB模型是另一种常见且直观的颜色表示方式,与RGB或CMYK模型不同,它以人的感知为基础,更容易理解。在这个调色板中,用户可能可以通过调整H、S、B三个参数来选取所需的颜色。 基于QT的调色板是一个利用Qt框架和其提供的色彩管理工具,如QPalette、QColorDialog、QGradient等,构建的交互式颜色选择组件。它不仅提供了横竖渐变的色彩选取方式,还可能支持HSB色彩模型,使得用户在开发图形用户界面时能更加灵活和精准地控制色彩。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值