原創|使用教程|編輯:我只采一朵|2018-02-05 14:22:35.000|閱讀 1142 次
概述:今天的文章中,我們來看一個教你如何在Linux上使用FastReport Core的例子。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
今天的文章中,我們來看一個教你如何在Linux上使用FastReport Core的例子。
首先對于Linux,我們將需要額外的庫,默認情況下可能并沒有安裝:
在服務器端Linux操作系統上通常沒有安裝XServer,而這是處理FastReport Core圖形所必需的。所以我們必須安裝它。你可以選擇Xvfb、VcXsrv或任何其他服務。
我們待會兒再來講XServer的問題,現在,我們先創建一個ASP.Net Core應用程序:
選擇Web應用程序:
通過NuGet將FastReport Core添加到項目中。
我們添加一個本地倉庫作為NuGet選項中的包的來源:
設置對本地存儲庫或文件夾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中暫時不可用,因此我們使用常規報表對象。創建一個數據源并將其注冊到報表對象中。加載報表模板。使用 Prepare ()
方法準備報表。接下來,我們在HTML中創建已完成的報表的導出。我們導出為MemoryStream流(或文件)。然后,我們使用ViewData或ViewBag將報表傳遞給視圖。
現在繼續編輯“Views - > Index.chtml”視圖。
這非常簡單 - 我們以HTML格式顯示報表:
@{ ViewData["Title"] = "Home Page"; } @if (ViewData.ContainsKey("Report")) { @Html.Raw(ViewData["Report"]) }
如前所述,FRCore需要XServer,而服務器端Linux并不提供。
我們來看一個使用debian服務器配置Linux示例:
1.打開控制臺;
2.更新apt-get并安裝軟件包:
3.更改環境變量,DISPLAY =: 99
。
我們在Program類中添加了兩個方法。LinuxStart方法檢查是否正在運行,如果是XServer,如果是的話則關閉它并創建一個新的。
StopLinux方法僅停止虛擬服務器。
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(); }
應用程序已準備就緒。運行查看效果:
總結一下,我們可以得出結論,和Windows一樣,在Linux上運行FastReport Core非常簡單。只不過需要一些操作系統的高級設置才能使用.Net Core。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn