راه اندازی StimulSoft Report در ASP.NET MVC
نویسنده: علی یگانه مقدم
تاریخ: ۱۳۹۵/۰۸/۱۳ ۲۱:۳۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
طراحی فایل MRT
فایل MRT در واقع یک قالب (Template) خالی از مقادیر متغیر است که در StimulSoft Studio به طراحی آن میپردازیم و در برنامه خود، این مقادیر متغیر را با اطلاعات دلخواه خود جایگزین میکنیم. تصویر زیر یک نمونه از یک گزارش خالی است که ابتدا آن را طراحی کرده و سپس در برنامه آن را مورد استفاده قرار میدهیم:
برای اینکه فایل MRT بتواند دیتاهای لازمی را که به آن پاس میدهیم، بخواند و در جای مشخص شده قرار بدهد، باید یک BussinessObject برای آن ایجاد کنیم. بعد از اینکه یک گزارش جدید ایجاد کردید، در سمت راست به قسمت Dictionary بروید و در قسمت BussinessObject گزینه NewBussinessObject را انتخاب کنید. یک نام و نام مستعار که عموما هم یکی است، برای آن انتخاب کنید. در زیر همان پنجره شما میتوانید ستونهای اطلاعاتی خود را تعریف کنید. در اینجا من میخواهم اطلاعات یک راننده را به همراه خودروی وی، نشان دهم. برای همین، من دو موجودیت راننده و خودروی راننده را دارم. پس اسم Business Object را DriverReport میگذارم و ستونهای اطلاعاتی فقط راننده (بدون در نظر گرفتن خودروی وی) را وارد میکنم.
در همین کادر بالا شما میتوانید تصیم بگیرید که آیا میخواهید اطلاعات خودرو را به همراه دیگرستونهای اطلاعاتی راننده، ایجاد کنید یا اینکه برای خودرو یک نوع مجزا انتخاب کنید. اگر تنها یک خودرو برای راننده باشد، شاید راحتتر باشید همانند اطلاعات راننده با آن رفتار کنید. ولی اگر مثلا بخواهید خودرویهای گذشته راننده را هم جز لیست داشته باشید، بهتر است یک Business Object جدید متعلق و زیر مجموعه Business Object راننده ایجاد کنید. در اینجا چون تنها یک خودرو است، من آن اطلاعات آن را به همراه راننده، ارسال میکنم. شکل زیر ساختار درختی از گزارش بالاست:
شکل زیر هم یک ساختار دیگر از یک گزارش است که شامل Business objectهای مختلف میشود:
سپس همین فیلدها را به سمت صفحه خالی بکشانید. با دو بار کلیک روی فیلدهای قرار گرفته در صفحه، با نحوه بایند کردن مقادیر آشنا میشوید؛ هر فیلدی که قرار است دیتای آن بایند شود، باید به شکل زیر در بخش Expression پنجره باز شده، نوشته شود:
{driverReport.LastName} راه اندازی گزارش ساز در ASP.Net MVC
اولین کاری که میکنیم، ورود سه dll اصلی به پروژه است:
Stimulate.Base
Stimulate.Report
Stimulate.Report.MVC
در مرحله بعد یک متد ساخته و یک ویوو را برای صفحه گزارش گیری ایجاد میکنیم:
public ActionResult Report(int id)
{
return View();
} @Html.Stimulsoft().StiMvcViewer(new StiMvcViewerOptions()
{
Localization = "~/content/reports/fa.xml",
Actions =
{
GetReportSnapshot = "LoadReportSnapshot",
ViewerEvent = "ViewerEvent",
ExportReport = "ExportReport",
PrintReport = "PrintReport",
}
} @Html.Stimulsoft().StiMvcViewer(new StiMvcViewerOptions()
{
Localization = "~/content/reports/fa.xml",
ActionGetReportSnapshot = "LoadReportSnapshot",
ActionViewerEvent = "ViewerEvent",
ActionExportReport = "ExportReport",
ActionPrintReport = "PrintReport",
} var driver = new
{
FirstName = "علی",
LastName = "یگانه مقدم",
NationalCode = "12500000000",
FatherName = "حسین",
Model = "نام خودرو",
MotorNumber = 415244,
ProductionYear = 1394,
Capacity = 4
}; var driver = new
{
FirstName = "علی",
LastName = "یگانه مقدم",
NationalCode = "12500000000",
FatherName = "حسین",
car = new
{
Model = "نام خودرو",
MotorNumber = 415244,
ProductionYear = 1394,
Capacity = 4
}
}; var report = new StiReport();
report.Load(Server.MapPath("~/Content/Reports/driver.mrt"));
report.RegBusinessObject("driverReport", driver);
report.Dictionary.Variables.Add("today", DateTime.Today.ToPersianString(PersianDateTimeFormat.Date)); var report = new StiReport();
report.RegBusinessObject("driverReport", driver);
report.Dictionary.Variables.Add("today", DateTime.Today.ToPersianString(PersianDateTimeFormat.Date));
report.Load(Server.MapPath("~/Content/Reports/driver.mrt")); public ActionResult LoadReportSnapshot()
{
var driver = new
{
FirstName = "علی",
LastName = "یگانه مقدم",
NationalCode = "12500000000",
FatherName = "حسین",
Model = "نام خودرو",
MotorNumber = 415244,
ProductionYear = 1394,
Capacity = 4
};
var report = new StiReport();
report.Load(Server.MapPath("~/Content/Reports/driver.mrt"));
report.RegBusinessObject("driverReport", driver);
report.Dictionary.Variables.Add("today", DateTime.Today.ToPersianString(PersianDateTimeFormat.Date));
return StiMvcViewer.GetReportSnapshotResult(HttpContext, report);
} public virtual ActionResult ViewerEvent()
{
return StiMvcViewer.ViewerEventResult();
} public virtual ActionResult PrintReport()
{
return StiMvcViewer.PrintReportResult(this.HttpContext);
} public virtual ActionResult ExportReport()
{
return StiMvcViewer.ExportReportResult(this.HttpContext);
}
.stiMvcViewerReportPanel table{
direction:ltr !important;
}
public virtual ActionResult Report(int id)
{
TempData["id"]=id;
return View();
}
public virtual ActionResult LoadReportSnapshot()
{
var driverId = (int)TempData ["id"];
//.....
} public virtual ActionResult Report(int id)
{
return View();
}
public virtual ActionResult LoadReportSnapshot(int id)
{
//.....
} Server =
{
GlobalReportCache = false
} TempData["id"]=id;
report.RegBusinessObject("driverReport", driver); report.Load(Server.MapPath("~/Content/Reports/driver.mrt")); @Html.Stimulsoft().StiMvcViewer(new StiMvcViewerOptions()
{
Localization = "~/content/reports/fa.xml",
.
.
.
} public virtual ActionResult Report(Guid personId)
{
return View();
}
public virtual ActionResult LoadReportSnapshot(Guid personId)
{
var person = _bossService.GetPersonViewModel(personId); http://localhost:14425/tableproduct/printtable?reporttype=21
/tableproduct/PrintTable?reporttype=21&mvcviewer_resource=scripts&mvcviewer_id=MVCViewer1&mvcviewer_theme=Office2013&mvcviewer_loc= fi9Db250ZW50L1N0aW11bHNvZnQvTG9jYWxpemF0aW9uL2ZhLnhtbA%3d%3d&mvcviewer_version=2015.2
@Html.Stimulsoft().StiMvcViewer("MVCViewer1", new StiMvcViewerOptions()
{
Actions = new StiMvcViewerOptions.ActionOptions()
{
GetReportSnapshot =
Url.Action(
MVC.TableProduct.Report.ActionNames.CustomReportSnapshot,
MVC.TableProduct.Report.Name, new { area = MVC.TableProduct.Name }, "http"),
ViewerEvent =
Url.Action(
MVC.TableProduct.Report.ActionNames.CustomViewerEvent,
MVC.TableProduct.Report.Name, new { area = MVC.TableProduct.Name }, "http"),
PrintReport =
Url.Action(
MVC.TableProduct.Report.ActionNames.CustomPrintReport,
MVC.TableProduct.Report.Name, new { area = MVC.TableProduct.Name },"http"),
},
Server = new StiMvcViewerOptions.ServerOptions()
{
PassFormValues = true,
GlobalReportCache = false,
Controller = MVC.TableProduct.Report.Name
},
Exports = new StiMvcViewerOptions.ExportOptions()
{
ShowExportToPdf = true,
ShowExportToCsv = true
},
Toolbar = new StiMvcViewerOptions.ToolbarOptions()
{
ShowDesignButton = false,
ShowEditorButton = false,
ShowParametersButton = false,
ShowViewModeButton = true
},
Localization = "~/Content/Stimulsoft/Localization/fa.xml",
Width = Unit.Percentage(100),
Theme = StiTheme.Office2013,
}) GetReportSnapshot = Url.Action(result: MVC.Report.CustomReportSnapshot()), //...
GetReport = Url.Action("GetPuFish", "Report", null, "http") @section styles{
<style>
@@font-face {
font-family: 'Shiraz';
src: url('../fonts/BShiraz.eot');
src: url('../fonts/BShiraz.eot?#iefix') format('embedded-opentype'), url('../fonts/BShiraz.woff') format('woff'), url('../fonts/BShiraz.ttf') format('truetype');
font-weight: normal;
}
</style>
} StiFontCollection.AddFontFile("d:\\before_the_rain.ttf"); @font-face {
font-family: myFirstFont;
src: url(sansation_light.woff);
} Stimulsoft.Base.StiFontCollection.AddFontFile
Stimulsoft.Report.StiLogService.LogEnabled = true; StiLogService service = StiConfig.Services.GetService(typeof(StiLogService)) as StiLogService; service.LogPath = "Report.log"; // مسیر کامل و Mappath شده
[
{
"day" : "۱۳۹۸/۱۰/۲۵",
"plan" : "موردی",
"rId" : 1,
"records" : [
{
"help" : "بسته غذایی",
"rId" : 1
},
{
"help" : "کمک مالی",
"rId" : 1
}
]
},
{
"day" : "۱۳۹۸/۱۰/۱۶",
"plan" : "مادرانه",
"rId" : 1002,
"records" : [
{
"help" : "بسته غذایی",
"rId" : 1002
},
{
"help" : "تدریس",
"rId" : 1002
}
]
}
] public class PlanModel
{
public int RId { get; set; }
public string Day { get; set; }
public string Plan { get; set; }
public List<RecordModel> Records { get; set; }
}
public class RecordModel
{
public int RId { get; set; }
public string Help { get; set; }
}
public class CustomerServiceReport
{
public string ReportTitle { get; set; }
public string ReportDate { get; set; }
//...
public List<CustomerRow> Customers { get; set; } = new List<CustomerRow>();
}
public class CustomerRow
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public string Phone { get; set; }
//...
public List<ServiceRow> Services { get; set; } = new List<ServiceRow>();
}
public class ServiceRow
{
public int ServiceId { get; set; }
public string ServiceName { get; set; }
public int Count { get; set; }
public long Price { get; set; }
}
var data = new CustomerServiceReport
{
ReportDate = "1399/09/04",
ReportTitle = "گزارش سرویسهای مشتریان"
};
for (int i = 0; i < 5; i++)
{
var customer = new CustomerRow
{
CustomerId = i + 1,
CustomerName = $"مشتری شماره {i + 1}",
Phone = "001122"
};
for (int j = 0; j < 3; j++)
{
var service = new ServiceRow
{
ServiceId = j + 1,
ServiceName = $"سرویس شماره {j + 1}",
Count = (j + 1) * 10,
Price = (j + 1) * 10000
};
customer.Services.Add(service); //اضافه کردن سرویس به هر مشتری
}
data.Customers.Add(customer); // اضافه کردن مشتری به گزارش
}
var report = new StiReport();
report.RegBusinessObject("CustomerServiceReport", data);
report.Dictionary.SynchronizeBusinessObjects(); public IActionResult ViewerEvent()
{
return StiNetCoreViewer.ViewerEventResult(this);
} System.ArgumentException: 'Must specify valid information for parsing in the string. '
report.Dictionary.SynchronizeBusinessObjects();
report.RegBusinessObject("CustomerServiceReport", data);
report.Save(StiNetCoreHelper.MapPath(this, "Reports/CustomerServiceReportNoSync.mrt"));
report.Dictionary.SynchronizeBusinessObjects();
report.Save(StiNetCoreHelper.MapPath(this, "Reports/CustomerServiceReportSync.mrt"));
report.Dictionary.SynchronizeBusinessObjects(0);
report.Save(StiNetCoreHelper.MapPath(this, "Reports/CustomerServiceReportSync0.mrt"));
report.Dictionary.SynchronizeBusinessObjects(1);
report.Save(StiNetCoreHelper.MapPath(this, "Reports/CustomerServiceReportSync1.mrt"));
report.Dictionary.SynchronizeBusinessObjects(2);
report.Save(StiNetCoreHelper.MapPath(this, "Reports/CustomerServiceReportSync2.mrt"));
Sync1
Sync2
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy /y /d $(ProjectDir)Packages\*.* $(OutDir)" />
</Target> public static class StimulSoftLicense
{
public static void LoadLicense(IWebHostEnvironment environment)
{
var contentRoot = environment.ContentRootPath;
var licenseFile = System.IO.Path.Combine(contentRoot,"Reports", "license.key");
Stimulsoft.Base.StiLicense.LoadFromFile(licenseFile);
}
} public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
StimulSoftLicense.LoadLicense(env);
} public IActionResult RequestReport(int id)
{
return View();
}
public IActionResult LoadReportData(int id)
{
StiReport report = new StiReport();
report.Load(StiNetCoreHelper.MapPath(this, "Reports/Requests/RequestInfo.mrt"));
var landDetailsReport = GetOwnerReportData(id);
report.RegBusinessObject("Land", landDetailsReport);
return StiNetCoreViewer.GetReportResult(this, report);
}
public IActionResult ViewerEvent()
{
return StiNetCoreViewer.ViewerEventResult(this);
} <div style="direction: ltr">
@Html.StiNetCoreViewer(new StiNetCoreViewerOptions()
{
Theme = StiViewerTheme.Office2007Silver,
Appearance =
{
RightToLeft = true,
//ShowTooltips = false,
ShowTooltipsHelp = false,
},
Localization = "~/Reports/fa.xml",
Actions =
{
GetReport = "LoadReportData",
ViewerEvent = "ViewerEvent",
},
Toolbar =
{
ShowAboutButton = false,
ShowOpenButton = false,
ShowSaveButton = true,
ShowFindButton = false,
ShowEditorButton = false,
ShowDesignButton = false,
ShowBookmarksButton = false,
ShowResourcesButton = false,
ShowParametersButton = false,
ShowPinToolbarButton = false
},
Exports =
{
DefaultSettings =
{
ExportToPdf =
{
CreatorString = "SANA"
}
},
ShowExportDialog = false,
ShowExportToDocument = false,
ShowExportToExcel = false,
ShowExportToExcel2007 = false,
ShowExportToHtml = false,
ShowExportToHtml5 = false,
ShowExportToImageBmp = false,
ShowExportToImageJpeg = false,
ShowExportToImagePcx = false,
ShowExportToImagePng = false,
ShowExportToImageMetafile = false,
ShowExportToImageTiff = false,
ShowExportToOpenDocumentCalc = false,
ShowExportToMht = false,
ShowExportToOpenDocumentWriter = false,
ShowExportToXps = false,
ShowExportToSylk = false,
ShowExportToRtf = false,
ShowExportToExcelXml = false,
ShowExportToText = false,
ShowExportToWord2007 = false,
ShowExportToImageGif = false,
ShowExportToCsv = false,
ShowExportToDbf = false,
ShowExportToDif = false,
ShowExportToImageSvg = false,
ShowExportToImageSvgz = false,
ShowExportToPowerPoint = false,
ShowExportToXml = false,
ShowExportToJson = false
}
})
</div> .stiJsViewerClearAllStyles
{
font-family: "IRANSans" !important;
}
//مخصوص دیالوگ تنظیمات خروجی در صورت فعال بودن
.stiJsViewerGroupPanelContainer{
direction: rtl !important;
}
.stiJsViewerFormButtonDefault{
direction: rtl !important;
}
.stiJsViewerFormButtonOver{
direction: rtl !important;
}
.stiJsViewerFormButtonDefault>table>tbody>tr>td{
text-align: right !important;
}
.stiJsViewerFormButtonOver>table>tbody>tr>td{
text-align: right !important;
}
.stiJsViewerFormHeader{
direction: rtl;
}
.stiJsViewerFormHeader>table>tbody>tr>td{
text-align: right !important;
}
.stiJsViewerCheckBox{
direction:rtl !important;
}
.stiJsViewerDropdownPanel{
direction:rtl !important;
}
.stiJsViewerFormContainer td.stiJsViewerClearAllStyles{
direction:rtl !important;
}
.stiMvcViewerReportPanel table{
direction:ltr !important;
}