.net mvc+fastreport打印報表

習慣使用lodop打印web,可是lodop打印html會出現意想不到的問題,最近作了打印員工轉正申請表功能,考慮使用fastreport設計表單並打印,網上入門資料較少,我也是根據提供的demo本身慢慢嘗試硬摳出來的.css

1.設計報表html

  打開Designer.exe,右側下拉Actions,選擇Save Dictionary,保存後打開該Dictionary.frd(默認)文件編輯數據項web

<?xml version="1.0" encoding="utf-8"?>
<Dictionary>
  <BusinessObjectDataSource Name="RegularEmpData" ReferenceName="RegularEmpData" DataType="System.Int32" Enabled="true">
    <Column Name="EmpName" DataType="System.String"/>
    <Column Name="DepName" DataType="System.String"/>
    <Column Name="EmpCode" DataType="System.String"/>
    <Column Name="PostName" DataType="System.String"/>
    <Column Name="Trial" DataType="System.String"/>
    <Column Name="FirstLeader" DataType="System.String"/>
    <Column Name="SeconLeader" DataType="System.String"/>
    <Column Name="DepManager" DataType="System.String"/>
    <Column Name="RegularTime" DataType="System.String"/>
  </BusinessObjectDataSource>
</Dictionary>

 

,再次下拉Actions選擇Open Dictionary選擇剛纔保存的Dictionary.以下圖示:app

選擇Dictionary後就加載了數據源.ide

接下來給報表添加綁定數據源,post

給單元格綁定數據spa

按下圖示挨個設計單元格.設計

2.後臺cs代碼(fastreport提供相關demo)code

設計完畢後,保存並複製到project中,代碼根據設計的數據源依次賦值,並使用fastreport的註冊數據方法給數據源綁定後臺數據,添加引用 FastReport.Web。orm

 public ActionResult PrintEmpRegular(string empCode)
        {
            WebReport webReport = new WebReport();
            webReport.Width = 800;
            webReport.Height = 950;
            EmpRegularData model = new EmpRegularData();
            oa_emp_regular model_ = new oa_emp_regular();
            List<EmpRegularData> regularData = new List<EmpRegularData>();

            try
            {
                using (OAEntities dbContext = new OAEntities())
                {
                    model_ = dbContext.oa_emp_regular.Where(t => t.job_code == empCode).FirstOrDefault();
                    if (model_ != null)
                    {
                        model.DepManager = model_.dept_manager;
                        model.DepName = model_.dept_name;
                        model.EmpCode = empCode;
                        model.EmpName = model_.apply_man;
                        model.FirstLeader = model_.gang_man_name;
                        model.PostName = model_.post_name;
                        model.RegularTime = model_.end_time.HasValue?model_.end_time.Value.ToString("yyyy-MM-dd"):"";
                        model.SecondLeader = string.IsNullOrWhiteSpace(model_.superior_name) ? "" : model_.superior_name;
                        model.Trial = (model_.start_time.HasValue? model_.start_time.Value.ToString("yyyy年MM月dd日"):"") + "" + (model_.end_time.HasValue? model_.end_time.Value.ToString("yyyy年MM月dd日"):"");
                    }
                    else
                    {
                        ViewBag.ErrorMessage = "系統不存在該員工轉正申請流程";
                        return View("Error");
                    }
                    var itemList = dbContext.oa_emp_regularItem.Where(t => t.flow_code == model_.flow_code && t.steptype == 1);
                  
                    regularData.Add(model);
   //關鍵代碼
                    webReport.Report.RegisterData(regularData, "RegularEmpData");
                    webReport.Report.Load(Server.MapPath("~/Template/emp_regular_form.frx"));
                    ViewBag.WebReport = webReport;
                    return View();
                }
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = ex.ToString();
                return View("Error");
            }
        }    
View Code

建立視圖,視圖中使用@ViewBag.WebReport.GetHtml()渲染html填充

@{
    ViewBag.Title = "PrintEmpRegular";
    Layout = "~/Views/Shared/_fastReportLayout.cshtml";
}
<div style="width:100%; margin:0px auto; text-align:center; padding:15px">@ViewBag.WebReport.GetHtml()</div>
View Code

layout.cshtml

@using FastReport.Web;
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title </title>
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />
    @Styles.Render("~/Content/css")

    @WebReportGlobals.Scripts()
    @WebReportGlobals.Styles()

</head>
<body>
    <div id="body">
        <section class="content-wrapper main-content clear-fix">
            @RenderBody()
        </section>
    </div>

</body>
</html>
View Code
相關文章
相關標籤/搜索