RDLC报表实现二维码打印功能

虽然RDLC中的矩阵可以扩展列,但是无法满足我需要完成的功能。所以在一个报表中放置了多个矩阵,每个矩阵绑定一个数据集,再通过XML数据控制显示的行数列数及分配给的数据集。

代码:

分页:工具箱中拖一个列表过来,设置 列表-->行组-->组属性
常规-->组表达式=Int((RowNumber(Nothing)-1)/10)   
分页符-->勾选在组的结尾。工具箱中拖一个矩阵,放在列表中,然后可以对表进行随意设计,表中分组就会自动按照10条一页进行分页。

控制每个Matrix是否可见(实质控制每个Rectangle可见,可在Matrix中列表的空白区域右击有可见的选项,对应有XML的变化)

 private void LetSecondRecVisible()     //删除Visibility节点
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "MaterielManagement/MaterielOper/MaterielReport.rdlc");
            XmlNode recNodeList = xmlDoc.GetElementsByTagName("Rectangle").Item(1);
            XmlNodeList test = xmlDoc.GetElementsByTagName("Visibility");
            if (null != recNodeList && test.Count!=0)
            {
                XmlNode recVisibleNode = recNodeList.ChildNodes[2];
                recNodeList.RemoveChild(recVisibleNode);
                xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "MaterielManagement/MaterielOper/MaterielReport.rdlc");
            }           
        }


        private void LetSecondRecHidden()   //插入Visibility节点,将Hidden节点置为true   
        {       
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "MaterielManagement/MaterielOper/MaterielReport.rdlc");
            if (xmlDoc.GetElementsByTagName("Visibility") == null || xmlDoc.GetElementsByTagName("Visibility").Count==0)
            {
                XmlElement xmlElement = xmlDoc.CreateElement("Visibility",xmlDoc.DocumentElement.NamespaceURI);
                XmlNode xmlNode = xmlDoc.GetElementsByTagName("Rectangle").Item(1);/Rectangle节点     
                XmlNode keepTogtherNode = xmlNode.ChildNodes[1];
                XmlNode xmlVisibleNode = xmlNode.AppendChild(xmlElement);     /返回的插入的节点
                XmlElement xmlHiddenElement = xmlDoc.CreateElement("Hidden", xmlDoc.DocumentElement.NamespaceURI);
                xmlHiddenElement.InnerText = "true";
                xmlVisibleNode.PrependChild(xmlHiddenElement);
                xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "MaterielManagement/MaterielOper/MaterielReport.rdlc");   //插入Visibility节点    true 
            }
        }

控制每页显示行数:

   private void SetPageItems(string num)    /设置每页显示二维码的个数
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "MaterielManagement/MaterielOper/MaterielReport.rdlc");
                if ("one" == num || "two" == num)
                {
                    XmlNodeList groupNodeList = xmlDoc.GetElementsByTagName("TablixRowHierarchy");                  
                    foreach (XmlNode groupNode in groupNodeList)
                    {
                        XmlNode grouping= groupNode.FirstChild.FirstChild.FirstChild;
                        foreach (XmlAttribute xmlAttribute in grouping.Attributes)
                        {
                            if (xmlAttribute.Value != null && ("详细信息" == xmlAttribute.Value || xmlAttribute.Value == "详细信息1"))      每页显示一行
                            {
                                grouping.FirstChild.FirstChild.InnerText = "=Int((RowNumber(Nothing)-1)/1)";
                            }
                        }                      
                    }                 
                }
                if ("four" == num)
                {
                    XmlNodeList groupNodeList = xmlDoc.GetElementsByTagName("TablixRowHierarchy");
                    foreach (XmlNode groupNode in groupNodeList)
                    {
                        XmlNode grouping = groupNode.FirstChild.FirstChild.FirstChild;
                        foreach (XmlAttribute xmlAttribute in grouping.Attributes)
                        {
                            if (xmlAttribute.Value != null && ("详细信息" == xmlAttribute.Value || xmlAttribute.Value == "详细信息1"))    每页显示两行
                            {
                                grouping.FirstChild.FirstChild.InnerText = "=Int((RowNumber(Nothing)-1)/2)";
                            }
                        }
                    }
                }
                if ("six" == num)
                {
                    XmlNodeList groupNodeList = xmlDoc.GetElementsByTagName("TablixRowHierarchy");
                    foreach (XmlNode groupNode in groupNodeList)
                    {
                        XmlNode grouping = groupNode.FirstChild.FirstChild.FirstChild;
                        foreach (XmlAttribute xmlAttribute in grouping.Attributes)
                        {
                            if (xmlAttribute.Value!= null&&("详细信息" == xmlAttribute.Value || xmlAttribute.Value == "详细信息1"))    每页显示三行
                            {
                              grouping.FirstChild.FirstChild.InnerText = "=Int((RowNumber(Nothing)-1)/3)";
                            }
                        }
                    }
                }
                xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "MaterielManagement\\MaterielOper\\MaterielReport.rdlc");
                xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "MaterielManagement\\MaterielOper\\MaterielReport.rdlc");
            }
            catch (Exception ex)
            { 
            
            }
        }

加载两列数据,对两个数据集进行赋值:

   private void PrintTwoColumnsBarCodeXml()        /显示两列数据
        {
            dt = GetDataSource();
            string filePath = ConfigurationManager.AppSettings["FileDirName"];
            //站点文件存储路径
            string sysFilePath = Global.ServerFilePath.Replace("\\", "/");
            //文件类型
            string uploadPath = sysFilePath + "/BarCode/";
            DataTable anthorSource = new DataTable();
            DataTable firstSource = new DataTable();
            firstSource = dt.Clone();
            anthorSource = dt.Clone();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (0 != i % 2)i为奇数,matrix为第二列
                {
                    DataRow cRow = dt.Rows[i];
                    anthorSource.ImportRow(cRow);
                    string imagePath = Path.Combine(uploadPath, cRow["BarCode_Position"] == DBNull.Value ? "blank.png" : cRow["BarCode_Position"].ToString());
                    ReportParameter MatrixColumnSecond = new ReportParameter("MatrixColumnSecond", "file:///" + imagePath);
                    MaterielReportViewer.LocalReport.SetParameters(new ReportParameter[] {MatrixColumnSecond});
                }
                else     
                {
                    DataRow cRow = dt.Rows[i];
                    firstSource.ImportRow(cRow);
                    string imagePath = Path.Combine(uploadPath, cRow["BarCode_Position"] == DBNull.Value ? "blank.png" : cRow["BarCode_Position"].ToString());
                    ReportParameter MatrixColumnFirst = new ReportParameter("MatrixColumnFirst", "file:///" + imagePath);
                    MaterielReportViewer.LocalReport.SetParameters(new ReportParameter[] { MatrixColumnFirst });              
                }
            }
            ReportDataSource dataReport = new ReportDataSource("materielInfo", firstSource);
            ReportDataSource dataSecondReport = new ReportDataSource("ImageSecond", anthorSource);
            MaterielReportViewer.LocalReport.DataSources.Add(dataReport);
            MaterielReportViewer.LocalReport.DataSources.Add(dataSecondReport);
            MaterielReportViewer.LocalReport.Refresh();
        }

在Matrix中添加图片每行显示不同的图片,本人实验不能通过参数来引用外部图片,这样每行显示的图片都是同一个,必须通过将图片直接存入数据库中。如果有人成功,希望给出解决办法!!!


你可能感兴趣的:(RDLC报表实现二维码打印功能)