zoukankan      html  css  js  c++  java
  • Reporting Service报表设计常见技巧及问题解法

    1、 对Table中的详细内容,以不同的背景色间隔开相邻的两行:
    A:选择Table的Detail行,选择属性中的BackgroundColor,值选择表达式,输入:=iif(RowNumber(Nothing) Mod 2, "White", "Beige")。组内设置不同底色则将Nothing改为相应的组名,如“Table1_Group1”;

    2、仅在组的外边框设置线颜色的方法(纵向合并单元格):
    A:设置BorderStyle-Bottom表达式为:=iif(RowNumber("Table1_Group1") = CountRows("Table1_Group1") , "White", "Beige");

    3、报表常常需要用到参数起始日期和结束日期,并且需要将起始日期默认为本月第一天,结束日期默认为本月最后一天:
    参数OccurDateFrom的默认值设置为表达式=DateAdd("d", 1-Day(Today), Today);参数OccurDateTo的默认值设置为表达式=DateAdd("d", -1, DateSerial(IIf(Month(Today)=12,Year(Today)+1,Year(Today)), IIf(Month(Today)=12,1,Month(Today)+1), 1))就能满足需求咯;

    4、在同一张报表上显示数据报表和图表(如直方图),并且根据“显示直方图”参数切换:
    新增参数IsShowChart(Boolean型),默认值=false;然后将报表(Table1)和图表(Chart1)的Location设置为一样;
    Table1的Visibility.Hidden设置为表达式=Parameters!IsShowChart.Value;Chart1的Visibility.Hidden设置为表达式=Not Parameters!IsShowChart.Value就可以实现根据参数切换报表和图表了;

    5、设置每页显示Table表头或表尾:
    A:选择Table Header或Table Footer,将属性中的RepeatOnNewpage设为True;

    6、在每页都显示放入的图片或标题头等信息:
    A:只须在Table Header中加行数,把你要显示的内容放到单元格中,然后再按第 5 点方式设置后就可以了;

    7、报表插入图片的方法:
    A:需要将插入的图像报表项的Source属性设置为extern,且将Value设置文件全路径(且以file://打头),譬如:file://E:/VSTS/SSRS/ReportServiceClient/Wave.jpg;并且需设置reportViewer1.LocalReport.EnableExternalImages = true;

    B:对于动态获取图片则需要将Source属性设置为DataBase,Value=System.Convert.FromBase64String(Fields!BarCode.Value),上面示例是按数据源的属性BarCode(string类型),类属性代码见下面:

    public string BarCode
    {
    get
    {
    BarcodeLib.Barcode b
    = new BarcodeLib.Barcode();
    b.IncludeLabel
    = true;
    Image img
    = b.Encode(BarcodeLib.TYPE.CODE39, BillNo);
    return Convert.ToBase64String(BitmapToBytes(img as Bitmap));
    }
    }
    private byte[] BitmapToBytes(Bitmap Bitmap)
    {
    MemoryStream ms
    = null;
    try
    {
    ms
    = new MemoryStream();
    Bitmap.Save(ms, ImageFormat.Gif);
    byte[] byteImage = new Byte[ms.Length];
    byteImage
    = ms.ToArray();
    return byteImage;
    }
    catch (ArgumentNullException ex)
    {
    throw ex;
    }
    finally
    {
    ms.Close();
    }
    }

    8、获取参数列表的方法
    首先添加web引用:http://server/reportserver/reportservice.asmx

            private RS2005.ReportParameter[] GetReportParameters(string url, string reportPath)
            {
                
    //获取参数的方法之二(不能获取元数据)
                RS2005.ReportingService rService = new RS2005.ReportingService();//创建报表服务实例
                rService.Credentials = System.Net.CredentialCache.DefaultCredentials;//设置默认系统凭据

                rService.Url 
    = url;
                
    string historyID = null;
                
    bool forRendering = true;
                RS2005.ParameterValue[] values 
    = null;
                RS2005.DataSourceCredentials[] credentials 
    = null;
                RS2005.ReportParameter[] parameters;
                parameters 
    = rService.GetReportParameters(reportPath, historyID, forRendering, values, credentials);
                
    return parameters;
            }

    9、获取参数及其元数据(如参数的默认值表达式等):

    private Dictionary<stringstring> GetReportParamtersDefaultValue(string url, string reportPath)
    {
        Report report 
    = null;
        RS2005.ReportingService rService 
    = new RS2005.ReportingService();//创建报表服务实例
        rService.Credentials = System.Net.CredentialCache.DefaultCredentials;//设置默认系统凭据
        rService.Url = url;
        
    byte[] bytes = rService.GetReportDefinition(reportPath);
        
    if (bytes != null)
        {
            XmlSerializer serializer 
    = new XmlSerializer(typeof(Report));
            
    using (MemoryStream stream = new MemoryStream(bytes))
            {
                report 
    = (Report)serializer.Deserialize(stream);
            }
        }
        List
    <ItemsChoiceType37> reportItems = new List<ItemsChoiceType37>(report.ItemsElementName);
        
    int index = reportItems.IndexOf(ItemsChoiceType37.ReportParameters);
        ReportParametersType parametersType 
    = report.Items[index] as ReportParametersType;
        Dictionary
    <stringstring> ps = new Dictionary<stringstring>();
        
    foreach (ReportParameterType item in parametersType.ReportParameter)
        {
            ps.Add(item.Name, 
    string.Empty);

            List
    <ItemsChoiceType33> rptItems = new List<ItemsChoiceType33>(item.ItemsElementName);
            index 
    = rptItems.IndexOf(ItemsChoiceType33.DefaultValue);
            DefaultValueType dvt 
    = item.Items[index] as DefaultValueType;

            ValuesType vt 
    = dvt.Items[0as ValuesType;
            ps[item.Name] 
    = vt.Value[0];
        }
        
    return ps;
    }

    10、遍历报表项的方法:
    首先添加web引用:http://server/reportserver/reportservice.asmx

    ReportingService rService = new ReportingService();//创建报表服务实例
    rService.Credentials = System.Net.CredentialCache.DefaultCredentials;//默认系统凭据
    CatalogItem[] catalogItems;
    catalogItems 
    = rService.ListChildren("/"true);//对根路径检索
    foreach(CatalogItem item in catalogItems)
    {
        
    if(item.Type ==ItemTypeEnum.Folder)
      {
        
    //遍历报表文件夹
      }
    }
     
    foreach(CatalogItem item in catalogItems)
    {
      
    if(item.Type ==ItemTypeEnum.Report)
      {
        
    //遍历报表
      }
    }

    //搜索报表
    ReportService.SearchCondition[] condition=new SearchCondition[1];
    condition[
    0]=new SearchCondition();
    condition[
    0].Name ="Name";
    condition[
    0].Value =this.TextBox1.Text ;
    catalogItems
    =rs.FindItems ("/",WebReportSample.ReportService .BooleanOperatorEnum .And ,condition);

    //发布报表
    byte[] reportData;
    System.IO .FileStream fs
    =System.IO .File.OpenRead ("c:\\Report1.rdl");
    reportData
    =new byte [fs.Length ];
    fs.Read (reportData,
    0,fs.Length );
    rs.CreateReport (
    "New Report","/",false,reportData,null);

    //删除报表
    rs.DeleteItem ("报表名称");

     11、子报表部署问题:
    由于主报表中跳转到子报表或嵌入子报表时仅设置子报表名称,所以部署时必须确保子报表与主报表的路径一致;

    12、使用LocalReport.LoadSubreportDefinition 方法应注意的问题是如果主报表是通过ReportPath属性获取的则该方法将失效,原因是设置ReportPath的报表通过文件系统获取报表定义元数据时也一并加载了它需要的子报表定义元数据。可以看看MSDN的备注,换言之,使用该方法前必须使用LocalReport.LoadReportDefinition 方法,且不能设置ReportPath。
    原文描述如下:The ReportViewer control requires the definitions for all subreports before it can process a report. If the local report was loaded from the file system by specifying the ReportPath property, the ReportViewer control automatically loads the subreports from the file system. In cases where the local report was not loaded from the file system, these methods may be used to load report definitions for subreports.

    13、报表的无会话打印问题,报表定义文件没有设置“打印方向”的属性,实际上如果需要打印横向报表需要在创建PrintDocument时设置打印机参数,并且读取deviecInfo信息时需要将报表定义文件的PageWidth和PageHeight互换下,参考代码如下:

    public class EmfPrintDocument : PrintDocument
    {
        
    private EmfPrintDocument(PrinterInfo printerInfo)
        {
            
    base.PrinterSettings.PrinterName = printerInfo.PrinterName;
            
    base.DefaultPageSettings.Landscape = printerInfo.Landscape;
            
    base.PrinterSettings.DefaultPageSettings.Landscape = printerInfo.Landscape;
        }

            
    public static EmfPrintDocument Create(PrinterInfo printerInfo)
            {
                
    if (string.IsNullOrEmpty(printerInfo.PrinterName))
                {
                    printerInfo.PrinterName 
    = Printer.GetDeaultPrinterName();
                }
                
    else
                {
                    
    bool founded = false;

                    
    foreach (string pname in PrinterSettings.InstalledPrinters)
                    {
                        
    if (pname.Equals(printerInfo.PrinterName, StringComparison.CurrentCultureIgnoreCase))
                        {
                            printerInfo.PrinterName 
    = pname;
                            founded 
    = true;
                            
    break;
                        }
                    }

                    
    if (!founded)
                    {
                        
    throw new NotSupportedException(string.Format("找不到打印机 {0}" + printerInfo.PrinterName));
                    }
                }
                
    return new EmfPrintDocument(printerInfo);
            }
    }

    ReportPrintService的代码:

    Code

    使用实例:

    Code
    using (ReportPrintService rps = new ReportPrintService())
    {
        rps.Print(lp, printerInfo);
    }

    14、服务器报表的方法SetParameters() 需要注意的问题:
    (1)多值参数的类型是StringCollection而不是一个用逗号间隔的串;
    (2)传入的参数值如果在服务器报表的参数数据源中不存在该方法将报错(报表的所有参数赋默认值);
    (3)ReportViewer控件使用过程中如果发现展现出来的报表背景色全部变成了黑色,则需要将承载该控件的WindowsFormsHost设置其Background属性设置为White;
    (4)导出pdf时可能遇到导出的内容为????,需要修改下字体为“宋体”或其他基本字体;如果报表包含子报表则子报表无法导出内容;

     

  • 相关阅读:
    getaddrinfo()函数详解
    nm 命令 程序符号信息查看
    C# WebRequest处理Https请求
    正则表达式
    poj2762 Going from u to v or from v to u?
    Coroutine,你究竟干了什么?
    介绍几篇很有意思的计算机科普文章
    1005 Number Sequence(HDU)
    为什么读大学时做学术比搞项目重要?
    gcc中-pthread和-lpthread的区别
  • 原文地址:https://www.cnblogs.com/chriskwok/p/1568173.html
Copyright © 2011-2022 走看看