web端 微軟 RDLC 報表插件 寬大於高 橫向打印失效 解決方案

 

原由於系統報表工具使用的RDLC,本地測試一直使用的縱向打印,未測試過橫向打印😭。前端

甲方提供的打印機爲HP1106,支持縱向打印,但!領導要求必須橫向打印😇,所以還拖延了實施進度😳。後端

 

先總結一下遇到的問題ide

微軟官方RDLC報表工具使用起來不太方便,也因單據複雜度而異。工具

在遇到這個問題時上網搜索發現,一致認爲此問題爲BUG,在CS端能夠解決,可是BS端未提供解決方案。測試

可是搜索時大部分文章標題爲」RDLC寬大於高 橫向打印失效「或反之。spa

因而將計就計,個人報表爲A5橫向,改成A4縱向,這樣打印時可打印A5橫向。設計

此行爲純屬偷懶行爲,若以爲不可用,可將解決方案分享或換一個報表工具。 3d

 

個人代碼以下code

    /// <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');
}
前端頁面代碼
相關文章
相關標籤/搜索