linux能使用网站控件嘛,如何在Linux上使用FastReport Core

今天的文章中,我们来看一个教你如何在Linux上使用FastReport Core的例子。

首先对于Linux,我们将需要额外的库,默认情况下可能并没有安装:

Libgdiplus;

libx11-dev。

在服务器端Linux操作系统上通常没有安装XServer,而这是处理FastReport Core图形所必需的。所以我们必须安装它。你可以选择Xvfb、VcXsrv或任何其他服务。

我们待会儿再来讲XServer的问题,现在,我们先创建一个ASP.Net Core应用程序:

ef2678e2f7d045da39be0a973a31e2f3.png

选择Web应用程序:

6ea3c9693e0e49a8d3b76f297319d778.png

通过NuGet将FastReport Core添加到项目中。

42492f6aae4e3a1a70e79e7517721703.png

我们添加一个本地仓库作为NuGet选项中的包的来源:

26471f0c5c3a601e2a41082960e463c8.png

设置对本地存储库或文件夹Service FastReport.2017.4.0-release.nupkg的引用:

b124d690e76cab595197f646142ed581.png

从下拉列表中选择软件包的本地源并安装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并安装软件包:

sudo apt-get update;

sudo apt-get install -y xvfb x11vnc x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps libgdiplus libx11-dev;

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()

.UseUrls("http://[::]:5000")

.Build();

}

应用程序已准备就绪。运行查看效果:

f188f8b56123cb9a4accd486ca07c80d.png

总结一下,我们可以得出结论,和Windows一样,在Linux上运行FastReport Core非常简单。只不过需要一些操作系统的高级设置才能使用.Net Core。

推荐阅读

e39a3b6277c23e496a4314aa2d4c5d54.png

标签:报表专家报表解决方案报表.NETUnix/Linux报表控件报表设计报表引擎

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至hey@evget.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值