原創(chuàng)|使用教程|編輯:我只采一朵|2018-02-05 14:22:35.000|閱讀 1142 次
概述:今天的文章中,我們來看一個教你如何在Linux上使用FastReport Core的例子。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
今天的文章中,我們來看一個教你如何在Linux上使用FastReport Core的例子。
首先對于Linux,我們將需要額外的庫,默認情況下可能并沒有安裝:
在服務(wù)器端Linux操作系統(tǒng)上通常沒有安裝XServer,而這是處理FastReport Core圖形所必需的。所以我們必須安裝它。你可以選擇Xvfb、VcXsrv或任何其他服務(wù)。
我們待會兒再來講XServer的問題,現(xiàn)在,我們先創(chuàng)建一個ASP.Net Core應(yīng)用程序:
選擇Web應(yīng)用程序:
通過NuGet將FastReport Core添加到項目中。
我們添加一個本地倉庫作為NuGet選項中的包的來源:
設(shè)置對本地存儲庫或文件夾Service FastReport.2017.4.0-release.nupkg的引用:
從下拉列表中選擇軟件包的本地源并安裝FastReport軟件包。
從“Controllers”文件夾中打開“HomeController.cs”文件。我們添加幾個額外的庫到使用部分:
using FastReport; using FastReport.Export.Html; using System.IO; using System.Text;
我們來編輯Index方法:
public IActionResult Index() { Report report = new Report(); string report_path = ”Reports”; System.Data.DataSet dataSet = new System.Data.DataSet(); dataSet.ReadXml(report_path + "nwind.xml"); report.Report.RegisterData(dataSet, "NorthWind"); report.Report.Load(Path.Combine(report_path, "Simple List.frx")); report.Prepare(); HTMLExport export = new HTMLExport(); export.Layers = true; using (MemoryStream ms = new MemoryStream()) { export.EmbedPictures = true; export.Export(report, ms); ms.Flush(); ViewData["Report"] = Encoding.UTF8.GetString(ms.ToArray()); ViewData["ReportName"] = "Simple List.frx"; } return View();
由于WebReport對象在FastReport Core中暫時不可用,因此我們使用常規(guī)報表對象。創(chuàng)建一個數(shù)據(jù)源并將其注冊到報表對象中。加載報表模板。使用 Prepare ()
方法準(zhǔn)備報表。接下來,我們在HTML中創(chuàng)建已完成的報表的導(dǎo)出。我們導(dǎo)出為MemoryStream流(或文件)。然后,我們使用ViewData或ViewBag將報表傳遞給視圖。
現(xiàn)在繼續(xù)編輯“Views - > Index.chtml”視圖。
這非常簡單 - 我們以HTML格式顯示報表:
@{ ViewData["Title"] = "Home Page"; } @if (ViewData.ContainsKey("Report")) { @Html.Raw(ViewData["Report"]) }
如前所述,F(xiàn)RCore需要XServer,而服務(wù)器端Linux并不提供。
我們來看一個使用debian服務(wù)器配置Linux示例:
1.打開控制臺;
2.更新apt-get并安裝軟件包:
3.更改環(huán)境變量,DISPLAY =: 99
。
我們在Program類中添加了兩個方法。LinuxStart方法檢查是否正在運行,如果是XServer,如果是的話則關(guān)閉它并創(chuàng)建一個新的。
StopLinux方法僅停止虛擬服務(wù)器。
public class Program { static Process xvfb; const string xvfb_pid = "pid.xvfb.fr"; public static void Main(string[] args) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) LinuxStart(); BuildWebHost(args).Run(); if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) LinuxStop(); } private static void LinuxStop() { xvfb.Kill(); if (File.Exists(xvfb_pid)) File.Delete(xvfb_pid); } public static void LinuxStart() { if (File.Exists(xvfb_pid)) { string pid = File.ReadAllText(xvfb_pid); try { xvfb = Process.GetProcessById(int.Parse(pid)); xvfb.Kill(); xvfb = null; } catch { } File.Delete(xvfb_pid); } string display = Environment.GetEnvironmentVariable("DISPLAY"); if (String.IsNullOrEmpty(display)) { Environment.SetEnvironmentVariable("DISPLAY", ":99"); display = ":99"; } ProcessStartInfo info = new ProcessStartInfo(); info.FileName = "/usr/bin/Xvfb"; info.Arguments = display + " -ac -screen 0 1024x768x16 +extension RANDR -dpi 96"; info.CreateNoWindow = true; xvfb = new Process(); xvfb.StartInfo = info; xvfb.Start(); File.WriteAllText(xvfb_pid, xvfb.Id.ToString()); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseUrls("//[::]:5000") .Build(); }
應(yīng)用程序已準(zhǔn)備就緒。運行查看效果:
總結(jié)一下,我們可以得出結(jié)論,和Windows一樣,在Linux上運行FastReport Core非常簡單。只不過需要一些操作系統(tǒng)的高級設(shè)置才能使用.Net Core。
產(chǎn)品介紹 | 下載試用 | 優(yōu)惠活動 | | 聯(lián)系Elyn
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn