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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/h1534589653/article/details/62222507

虽然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节点 <Visibility>   <Hidden>true</Hidden> </Visibility>
            }
        }

控制每页显示行数:

   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报表打印

08-24

各位高手,RDLC打印中获取打印机的时候执行一段代码时候出现了一个问题,有相同使用 EMFStreamPrintDocument进行打印的高手帮忙看一下,谢谢大家了.rninternal static PRINTER_INFO_2[] EnumPrintersByFlag(PrinterEnumFlags Flags)rn rn uint cbNeeded = 0;rn uint cReturned = 0;rn bool ret = EnumPrinters(Flags, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);rnrn IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);rn ret = EnumPrinters(Flags, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned);rnrn if (ret)rn rn PRINTER_INFO_2[] Info2 = new PRINTER_INFO_2[cReturned];rnrn int offset = pAddr.ToInt32();rnrn for (int i = 0; i < cReturned; i++)rn rn Info2[i].pServerName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));rn offset += 4;rn Info2[i].pPrinterName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));rn offset += 4;rn Info2[i].pShareName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));rn offset += 4;rn Info2[i].pPortName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));rn offset += 4;rn Info2[i].pDriverName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));rn offset += 4;rn Info2[i].pComment = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));rn offset += 4;rn Info2[i].pLocation = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));rn offset += 4;rn Info2[i].pDevMode = Marshal.ReadIntPtr(new IntPtr(offset));rn offset += 4;rn Info2[i].pSepFile = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));rn offset += 4;rnrn ----------------------------------------------------------------rn执行下面的某一个设置属性就会出现 算数运算导致溢出,之后的情况就是无法获取到打印机了!rn----------------------------------------------------------------rnrn Info2[i].pPrintProcessor = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));rn offset += 4;rn Info2[i].pDatatype = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));rn offset += 4;rn Info2[i].pParameters = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));rn offset += 4;rn Info2[i].pSecurityDescriptor = Marshal.ReadIntPtr(new IntPtr(offset));rn offset += 4;rn Info2[i].Attributes = (uint )Marshal.ReadIntPtr(new IntPtr(offset));rn offset += 4;rn Info2[i].Priority = (uint)Marshal.ReadInt32(new IntPtr(offset));rn offset += 4;rn Info2[i].DefaultPriority = (uint)Marshal.ReadInt32(new IntPtr(offset));rn offset += 4;rn Info2[i].StartTime = (uint)Marshal.ReadInt32(new IntPtr(offset));rn offset += 4;rn Info2[i].UntilTime = (uint)Marshal.ReadInt32(new IntPtr(offset));rn offset += 4;rn Info2[i].Status = (uint)Marshal.ReadInt32(new IntPtr(offset));rn offset += 4;rn Info2[i].cJobs = (uint)Marshal.ReadInt32(new IntPtr(offset));rn offset += 4;rn Info2[i].AveragePPM = (uint)Marshal.ReadInt32(new IntPtr(offset));rn offset += 4;rnrn rnrn Marshal.FreeHGlobal(pAddr);rnrn return Info2;rnrn rn elsern rn return new PRINTER_INFO_2[0];rn rn 论坛

没有更多推荐了,返回首页