码迷,mamicode.com
首页 > 其他好文 > 详细

CxGrid导出Excel时清除颜色的设置

时间:2016-10-15 07:39:29      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

CxGrid导出Excel时清除颜色的设置

 (2011-04-25 16:33:23)
标签: 

it

 
分类: Delphi

http://www.radxe.com/?p=170

 

cxgrid导出到Excel是“所见即所得”模式的,应用程序中显示的颜色在导出时也会被设置到Excel,但很多时候导出的Excel是期望不包含色彩信息的,所以必须在导出时将cxgrid的颜色设置清除。cxgrid表格各个部分的颜色可以通过在view的styles中设置,未设置styles时cxgrid会使用默认色(灰色)渲染bandheader,footer等部分。所以要清除颜色除了将定义了style的表格部分的style清空外,还得为bandheader,footer等使用了默认色的部分设置一个合适的style。

以下是范例代码:

 

 //导出Excel
procedure TFrmBaseReport.SpbExcelClick(Sender: TObject);
begin
  try
    clearStyles;
    exportCXGridToExcel;
  finally
    resetStyles
  end;
end;
 
//清除样式
procedure TFrmBaseReport.clearStyles;
begin
  cxView.Styles.BandHeader := stWhite;
  cxView.Styles.Header := nil;
  cxView.Styles.ContentOdd := nil;
  cxView.Styles.ContentEven := nil;
  cxView.Styles.Footer := stWhite;
end;
 
//重置样式
procedure TFrmBaseReport.resetStyles;
begin
  cxView.Styles.BandHeader := stBandHeader;
  cxView.Styles.Header := stHeader;
  cxView.Styles.ContentOdd := stContentOdd;
  cxView.Styles.ContentEven := stContentEven;
  cxView.Styles.Footer := stFooter;
end;
 
//cxgrid数据导出到Excel
procedure TFrmBaseReport.exportCXGridToExcel(infoEmpty:boolean);
var
  fileName : String;
begin
  cxView.OptionsView.Header := false;
  try
    fileName := getTempFileName;
    ExportGrid4ToExcel(fileName,cxGrid);
    shellOpen(fileName+FILE_EXT_XLS);
  finally
    cxView.OptionsView.Header := true;
  end;
end;

备注:stBandHeader,stHeader,stContentOdd,stContentEven,stFooter,stWhite是表格各个部分对应的配色方案,可以TcxCustomGridView.styles中设置,stWhite是白色背景黑色文字的配色方案。Dev官网上对这个问题给出的方案是通过应用两个样式表实现的(TcxCustomGridView.Styles.StyleSheet,一个保存应用程序中外观的配色,另外一个保存导出时的配色),与上述方法类似。

CxGrid导出Excel时清除颜色的设置

标签:

原文地址:http://www.cnblogs.com/westsoft/p/5962677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!