web端 微软 RDLC 报表插件 宽大于高 横向打印失效 解决方案

 

起因于系统报表工具使用的RDLC,本地测试一直使用的纵向打印,未测试过横向打印😭。

甲方提供的打印机为HP1106,支持纵向打印,但!领导要求必须横向打印😇,因此还拖延了实施进度😳。

 

先总结一下遇到的问题

微软官方RDLC报表工具使用起来不太方便,也因单据复杂度而异。

在遇到这个问题时上网搜索发现,一致认为此问题为BUG,在CS端可以解决,但是BS端未提供解决方案。

但是搜索时大部分文章标题为”RDLC宽大于高 横向打印失效“或反之。

于是将计就计,我的报表为A5横向,改为A4纵向,这样打印时可打印A5横向。

此行为纯属偷懒行为,若觉得不可用,可将解决方案分享或换一个报表工具。 

 

我的代码如下

    /// <summary>
    /// RDLC报表帮助类
    /// </summary>
    public static class RDLCHelper
    {
        /// <summary>
        /// 下载打印文件
        /// </summary>
        /// <param name="option">报表设置项</param>
        /// <param name="str_dataSource">报表数据集名称</param>
        /// <param name="data">数据源</param>
        /// <param name="renderedBytes">报表二进制流</param>
        /// <param name="mimeType">报表mimeType</param>
        public static void DownloadFile<T>(ReportOption option, string str_dataSource, List<T> data, out byte[] renderedBytes, out string mimeType)
        {
            LocalReport localReport = new LocalReport();
            localReport.EnableExternalImages = true;

            localReport.ReportPath = HttpContext.Current.Server.MapPath(option.ReportServerUrl);

            ReportDataSource reportDataSource = new ReportDataSource(str_dataSource, data);
            localReport.DataSources.Add(reportDataSource);
            string encoding;
            string fileNameExtension;

            string deviceInfo =
                "<DeviceInfo>" +
                "<OutPutFormat>" + option.OutPutFormat + "</OutPutFormat>" +
                "<PageWidth>" + option.PageWidth + "</PageWidth>" +
                "<PageHeight>" + option.PageHeight + "</PageHeight>" +
                "<MarginTop>" + option.MarginTop + "</MarginTop>" +
                "<MarginLeft>" + option.MarginLeft + "</MarginLeft>" +
                "<MarginRight>" + option.MarginRight + "</MarginRight>" +
                "<MarginBottom>" + option.MarginBottom + "</MarginBottom>" +
                "</DeviceInfo>";
            Warning[] warnings;
            string[] streams;

            renderedBytes = localReport.Render(
                option.OutPutFormat,
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings
                );
        }

        /// <summary>
        /// 报表设置项
        /// </summary>            
        public class ReportOption
        {
            /// <summary>
            /// 报表文件路径
            /// </summary>
            public string ReportServerUrl { get; set; }
            /// <summary>
            /// 文件类型
            /// </summary>
            public string OutPutFormat { get; set; }
            /// <summary>
            /// 页宽,不包含单位(cm厘米,in英寸)
            /// </summary>
            public string PageWidth { get; set; }
            /// <summary>
            /// 页高,不包含单位(cm厘米,in英寸)
            /// </summary>
            public string PageHeight { get; set; }
            /// <summary>
            /// 外边距顶部值,设计页面外部累加值,不包含单位(cm厘米,in英寸)
            /// </summary>
            public string MarginTop { get; set; }
            /// <summary>
            /// 外边距底部值,设计页面外部累加值,不包含单位(cm厘米,in英寸)
            /// </summary>
            public string MarginBottom { get; set; }
            /// <summary>
            /// 外边距左部值,设计页面外部累加值,不包含单位(cm厘米,in英寸)
            /// </summary>
            public string MarginLeft { get; set; }
            /// <summary>
            /// 外边距右部值,设计页面外部累加值,不包含单位(cm厘米,in英寸)
            /// </summary>
            public string MarginRight { get; set; }
        }
    }
RDLC报表帮助类
        public ActionResult Print(string DEVICE_ID, string type)
        {
            List<bill> bill = new List<bill>();
            RDLCHelper.ReportOption option = new RDLCHelper.ReportOption
            {
                ReportServerUrl = "~/Template/bill.rdlc",
                OutPutFormat = type,
                PageWidth = "21cm",//注意此处设置的A4纵向格式
                PageHeight = "29.7cm",//但实际报表为A5横向
                MarginTop = "0cm",
                MarginBottom = "0cm",
                MarginLeft = "0cm",
                MarginRight = "0cm"
            };
            byte[] renderedBytes;
            string mimeType;
            RDLCHelper.DownloadFile(option, "billData", bill, out renderedBytes, out mimeType);
            return File(renderedBytes, mimeType);
        }
MVC后端方法
function Print() {
    window.open("/bbbb/billm/bill?type=pdf&DEVICE_ID=" + DeviceId , 'newwindow', 'height=800, width=1200, top=0,left=0, toolbar=no,  menubar=no, scrollbars=no, resizable=no,location=no, status=no');
}
前端页面代码

 

posted @ 2019-05-30 14:42  听雨的人  阅读(532)  评论(0编辑  收藏  举报