crystal report排序功能的实现

原创 2004年08月13日 09:18:00

整个报表由三部分组成

xxxx.asp //用来接收函数及从数据库中取值,基本上每个报表都有一个该文件,其中调用cyheader.aspcyfooter.asp

cyheader.asp

funcsort.inc

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

?

如果要使原有报表支持排序,经初步研究发现,有多种方法,但是理想中双击crystal report中列头实现排序在crystal report8.5 web版中不易实现。

为了减少实现排序功能时设计人员的工作量,使其工作量最少。

目前的排序不支持group,只支持正常的record

?

?

使用方法,在要实现排序的报表中:

(1)??? 在头部引入

(2)??? 插入以下程序段:

addSortField "姓名", "{VIW_RECRUIT_SIMPLY.APPNAME}", 0? '注意此时的SortOrder写该字段的最初排序类型就行了

addSortField "档案编号", "{VIW_RECRUIT_SIMPLY.RECORDCODE}", 0??????? '

?

'排序操作

processSort

?

?

实现过程描述如下:

为了便于排错,在主控文档中设置optioin explicite,强制变量声明

做以下工作:

1cyheader.aspserver端脚本增加以下部分:

public sortfieldcount

sortfieldcount = 0

public sortfields(10,3) '定义排序域名,最多十个

?

?

public SortField,SortDesc,SortOrder

public cSortDesc

?

public defaultSortField,defaultSortOrder,defaultSortDesc

defaultSortField = ""

?

defaultSortOrder = ""

?

public rfindSortField

rfindSortField = -1

?

2.在cyfooter.asptable之外(最上面),增加以下内容

?????? document.title = "<%=pagetitle%>";

?

<%

if (sortfieldcount > 0) then

%>

?????? currentSortField = "<%=SortField%>";

?????? currentSortOrder = "<%=SortOrder%>";

?????? currentSortDesc? = "<%=SortDesc%>";

?

?

//设置不同的排序字段并重新提交页面的方法

function submitSort(sortfield, sortorder, sortdesc){

?????? window.onunload = null;

?????? var para= "SortField=" + encodeURIComponent (sortfield) + "&SortOrder=" + sortorder + "&SortDesc=" + escape(sortdesc);

?????? var url = location.href;

?????? if (url.substring(url.indexOf('?') + 1) != para) {

????????????? document.sortform.action= "<%=aspfilename%>?" + para;

????????????? //alert(document.sortform.action);

????????????? document.sortform.submit();

?????? }

}

?

<%

end if

%>

3cyfooter.asp中增加输出排序按钮的程序段。

如果sortfieldcount大于0,则输出隐藏的域及排序按钮,如果没有设置,没不输出隐藏域及排序按钮。

增加程序段如下:

??????

?

<%

if sortfieldcount > 0 then

?????? dim bdesc,i

?

%>

?????? 当前排序:<%=cSortDesc%>

?????? <%

?????? dim fe

?????? For Each fe in Request.Form

?????? %>

??????

?????? <%Next

?????? for i = 1 to sortfieldcount

????????????? bdesc = "" & sortfields(i,1) & SortDir2Desc(sortfields(i, 3))

????????????? %>

?????????????

????????????? <%

?????? next

end if

%>

?????

4.新增function.inc函数库中增加如下函数

?

/*

1.NULL 关键字的支持,用来支持不排序情况

2.对于""无关键字的支持,此时分析crystal report中的自带的sort order

3.支持多字段排序

*/

?

?

/*报表排序系列函数

*/

?

//获得第ind个排序字段的名称及方向

function getSortField(rpt, <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />ind){

?????? rsf = "";

?????? rsflen = rpt.RecordSortFields.Count;

?????? //rw("报表的排序字段数为:" + rsflen);

?????? if ((rsflen > 0) && (ind >= 1) && (ind <= rsflen)) {

????????????? rsf = rpt.RecordSortFields.Item(ind).Field.Name + "," + rpt.RecordSortFields.Item(ind).SortDirection;

?????? }

?????? return rsf;

}

?

//获得第ind个排序字段的名称

function getSortFieldName(rpt, ind){

?????? rsf = "";

?????? rsflen = rpt.RecordSortFields.Count;

?????? if ((rsflen > 0) && (ind >= 1) && (ind <= rsflen)) {

????????????? rsf = rpt.RecordSortFields.Item(ind).Field.Name;

?????? }

?????? return rsf;

}

?

//获得第ind个排序字段的排序方向

function getSortFieldDir(rpt, ind){

?????? rsf = -1;

?????? rsflen = rpt.RecordSortFields.Count;

?????? if ((rsflen > 0) && (ind >= 1) && (ind <= rsflen)) {

????????????? rsf = rpt.RecordSortFields.Item(ind).SortDirection;

?????? }

?????? return rsf;

}

?

//只支持按一列进行排序

//rpt -- 要排序的报表

//fn -- FieldName 排序的列名

function setSortField(rpt,fn,order){

?????? clearSortField(rpt);

?????? addSortField(rpt,fn,order);

}

?

function clearSortField(rpt){

?????? rsflen = rpt.RecordSortFields.Count;

?????? for (i = rsflen; i >= 1; i--){

????????????? rpt.RecordSortFields.Delete(i); //删除已有排序

?????? }

}

?

//按新的排序方法进行排序,原有的主排序列则变为次排序列,原次排序列变为三排序列,依次类推

//rpt -- 要排序的报表

//fn -- FieldName 排序的列名

//CRSortDirection.crAscendingOrder

function addSortField(rpt ,fn ,order){

?????? var f = getField(rpt, fn);

?????? if (f != null){

????????????? rpt.RecordSortFields.Add(f, order); //向排序表中的增加一列

?????? }

}

?

//从报表中查找fn对应的field

//如果不存在,就去

function getField(rpt,fn){

?????? //遍历到该报表中数据表

?????? var i;

?????? var rf = null;

?????? var tlen = rpt.Database.Tables.Count;

?????? for (i =1; i <= tlen ; i++){

????????????? //遍历所有字段,找出与fn相同的字段

????????????? flen = rpt.Database.Tables.Item(i).Fields.Count;

????????????? for (j =1; j <= flen ; j++){

???????????????????? //rw(rpt.Database.Tables.Item(i).Fields.Item(j).Name);

???????????????????? if (rpt.Database.Tables.Item(i).Fields.Item(j).Name == fn) {

??????????????????????????? //rw("两者相等");

??????????????????????????? rf = rpt.Database.Tables.Item(i).Fields.Item(j); //返回该字段

??????????????????????????? return rf;

???????????????????? }

????????????? }

?????? }

?????? return rf; //返回undefined

}

?

//如果sortdir<>"1",返回0

function SortDir2Int(sortdir){

?????? var so = 0;

?????? if (sortdir == 1){

????????????? so = 1;

?????? }

?????? return so;

}

?

function SortDir2Desc(sortdir){

?????? var sod = "正向排序▲";

?????? if (sortdir == 1){

????????????? sod = "逆向排序▼";

?????? }

?????? return sod;

}

?

function contrary(sortdir){

?????? var so = 1;

?????? if (sortdir == 1){

????????????? so = 0;

?????? }

?????? return so;

}

?

function ddfindSortField(sf){

?????? var ind,i;

?????? //dim ind 'sfsortfields中的位置,如果sf不在sortfields中,则返回-1

?????? ind = -1;

?????? for (i = 1; i <= sortfieldcount; i++){

????????????? //rw("sortfields(" & CStr(i) + ",2)=[" & sortfields[i][2] + "]");

????????????? //rw("sf=[" & sf & "]");

????????????? if (sortfields(i, 2) = sf){

???????????????????? ind = i;

???????????????????? return ind;

????????????? }

?????? }

?????? //rfindSortField = ind

?????? return ind;

}

?

sub processSort

?

?????? 'if (sortfieldcount < 1) then

?????? '?? end sub

?????? 'end if

??????

'begin sort

'================================================================================

?????? 'URL的参数中读取是否设置排序

?????? '如果该参数存在,则按给定的参数进行排序

?????? SortField = Request.QueryString("SortField")

?????? SortOrder = CInt(Request.QueryString("SortOrder"))

?????? SortDesc = Request.QueryString("SortDesc")

?????? '排序的描述

?????? 'rw "Request.QueryString('SortField')" & SortField

?????? if (SortField = "") then '如果没有值则读取默认值

????????????? SortField = defaultSortField

????????????? SortOrder = defaultSortOrder

????????????? SortDesc = defaultSortDesc

?????? end if

?????? dim sFD

??????

?????? if (SortField = "") then '如果没有值,且也无默认值,则对空值进行处理

????????????? '2.对于""无关键字的支持,此时分析crystal report中的自带的sort order

????????????? sFD = getSortFieldName(Session("oRpt"), 1)

?

????????????? if (sFD = "") then

???????????????????? SortDesc = " 不进行排序 "

????????????? else

???????????????????? findSortField sFD

?

???????????????????? if (rfindSortField = -1) then

??????????????????????????? cSortDesc = " crystal report 内置的排序 "

???????????????????? else

??????????????????????????? SortField = sFD

??????????????????????????? SortOrder = getSortFieldDir(Session("oRpt"), 1)

??????????????????????????? SortDesc = sortfields(rfindSortField, 1)

??????????????????????????? cSortDesc = "" & SortDesc & SortDir2Desc(SortOrder)

??????????????????????????? addSortField SortDesc, sFD, contrary(SortOrder)

???????????????????? end if

????????????? end if

?????? else

????????????? '设置当前排序的显示内容

????????????? cSortDesc = "" & SortDesc & SortDir2Desc(SortOrder)

?

????????????? if (SortField = "{NULL}") then

???????????????????? clearSortField Session("oRpt")

???????????????????? if (SortDesc = "") then

??????????????????????????? SortDesc = " 不进行排序 "

???????????????????? end if

????????????? else

???????????????????? '如果设置了排序按钮的数量,将要根据当前排序字段调整排序按钮的类型

???????????????????? 'contrary SortOrder=1,返回0,否则返回1

???????????????????? addSortField SortDesc, SortField, contrary(SortOrder)

?

???????????????????? if (sortfield <> "") then

???????????????????? ? setSortField? Session("oRpt"), sortfield , SortDir2Int(SortOrder)

???????????????????? end if

????????????? end if

?

?????? end if

'================================================================================

'end sort

?

end sub

?

'添加排序列

'sortdesc, 排序的按钮名称及描述 sortdesc"身份证",则按钮显示“身份证正向排序”或“身份证逆向排序”

'sortfield, 排序的字段全名,参见Crystal Report中的字段名称,形如{table.field}

'sortorder 排序列方向 0,正向排序? 1,逆向排序

sub addSortField(sd, sf, so)

?????? dim sfc

?????? sfc = sortfieldcount

?????? findSortField? sf

?????? if (rfindSortField = -1) then '如果sf不在sortfields中,则新增排序按钮

????????????? sortfieldcount = sortfieldcount + 1

????????????? if (sortfieldcount > 9) then

????????????? ?? exit sub

????????????? end if

????????????? sfc = sortfieldcount

????????????? sortfields(sfc, 1) = sd

????????????? sortfields(sfc, 2) = sf

????????????? sortfields(sfc, 3) = so

?????? else '如果已经存在,则用so更新sortfields(i, 3)

????????????? sortfields(rfindSortField, 3) = so

?????? end if

?????? Response.flush

end sub

?

?

sub findSortField(sf)

?????? dim i

?????? dim ind 'sfsortfields中的位置,如果sf不在sortfields中,则返回-1

?????? ind = -1

?????? for i = 1 to sortfieldcount

????????????? if (sortfields(i, 2) = sf) then

???????????????????? ind = i

???????????????????? exit for

????????????? end if

?????? next

?????? rfindSortField = ind

end sub

?

sub printSortFields

?????? dim i

?????? for i = 1 to sortfieldcount

????????????? rw "sortfields(" & CStr(i) & ")=[" & sortfields(i, 1) & "],[" & sortfields(i, 2) & "],[" & sortfields(i, 3) & "]"

?????? next

end sub

?

?

[老贴]Crystal Report 学习笔记(1)

  学习资料:《用实例学 Crystal Report for Visual Studio.Net》章立民著1,  Crystal Report for vs.net以Crystal Report...
  • phinecos
  • phinecos
  • 2006-08-13 14:02:00
  • 975

Crystal Reports for VS.NET

  • 2008年02月11日 12:17
  • 529KB
  • 下载

Crystal Report制作使用

Crystal Report制作使用本文主要划分为以下六部分:一、Crystal Report for .NET 的功能二、Crystal Report总体结构三、报表数据访问执行模式四、报表类型五、...
  • jackeyabc
  • jackeyabc
  • 2007-10-25 09:12:00
  • 2697

Crystal Report 变量作用域简介

变量作用域 变量作用域用于定义某个公式的变量可在多大程度上用于其他公式。Crystal Reports 中的作用域有三个级别: Local Global Shared ...
  • decaizhang
  • decaizhang
  • 2011-08-12 17:32:01
  • 677

Crystal Report X在Visual C++ 2003/2005中的使用

方法一:(也可以不用namespace,看个人习惯)#include "craxddrt.h" //VC6中引入类型库中的类,并在生成的文件中添加名为CrystalReportWrapper的name...
  • hottomson
  • hottomson
  • 2006-05-27 12:41:00
  • 2143

为 Crystal Report 提供动态数据源

public class Totalaccount{    private string _kp_voucher_no;    private decimal  _kp_should_amt;    ...
  • tao19840705
  • tao19840705
  • 2010-08-23 09:50:00
  • 587

Crystal Report注册码

好东西啊:)注册号:6707437608 密码:AAP5GKS0000GDE100DS
  • chriswu
  • chriswu
  • 2005-04-29 16:09:00
  • 1015

制作水晶报表------crystal report designer 的使用和 安装

选择了微软开源的2010 crystal report designer     :CRforVS_13_0_2     使用外部发布的时候,注意把系统目录下的aspnet_clie...
  • a5251199
  • a5251199
  • 2015-11-12 11:02:42
  • 399

Eclipse 操作 Crystal Report 笔记

关于Eclipse 操作 Crystal Reporte笔记
  • u011910756
  • u011910756
  • 2014-06-16 15:20:46
  • 2866

Crystal report viewer 控件TCrystalActiveXReportViewer的使用

procedure TGL_CC_ReportCollection.OpenReport( const ReportFileName: string;                         ...
  • looyo
  • looyo
  • 2007-09-03 11:48:00
  • 3157
收藏助手
不良信息举报
您举报文章:crystal report排序功能的实现
举报原因:
原因补充:

(最多只允许输入30个字)