如何从 C# 制作报表到 FastReport Cloud

众所周知,我们的世界在不断发展,新技术几乎每天都会出现。如今,不再需要在办公室内建立整个基础设施、雇用人员来监控设备、处理该设备出现的问题和其他困难。

如今,越来越多的服务提供业务云解决方案,例如FastReport Cloud。我们的服务使开发团队免去不必要的工作;您不再需要考虑如何部署项目、在哪里租用或购买服务器最好、最有利可图,以及使用什么技术进行部署。这一切我们都已经解决了,你只需利用它即可。

    FastReport .net下载(qun:585577353)icon-default.png?t=N7T8https://www.evget.com/product/1861/download

如何使用FastReport云?

在本文中,我们将了解如何使用 SDK 在 FastReport Cloud 中创建报表并将其导出为任何方便的格式。

首先,我们创建一个项目并向其中添加 FastReport.Cloud.SDK.Web nuget 包。有了这个包,我们就可以方便地与 FastReport Cloud 进行通信,而无需 API。

我们还需要一个报告模板。此示例将使用我们演示中的 Box.frx:

模板框.frx

创建项目并向其添加所有必要的依赖项后,您可以继续分析示例。

首先,您需要在FastReport Cloud工作区中创建一个API密钥;为此,请点击链接https://fastreport.cloud

单击包含 API 密钥的选项卡并创建一个新密钥。如果该密钥已存在,则可以通过右键单击它并从下拉列表中选择一个操作来复制它。

创建和复制 API 密钥

收到 API 密钥后,我们返回到我们的应用程序。我们将密钥写入一个单独的变量,如下例所示:

private const string ApiKey = "your API key";

接下来,我们需要创建将在程序中使用的主要对象:

var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://fastreport.cloud");
httpClient.DefaultRequestHeaders.Authorization = new FastReportCloudApiKeyHeader(ApiKey);
var subscriptions = new SubscriptionsClient(httpClient);
var rpClientTemplates = new TemplatesClient(httpClient);
var rpClientExports = new ExportsClient(httpClient);
var downloadClient = new DownloadClient(httpClient);
var subscription = (await subscriptions.GetSubscriptionsAsync(0, 10)).Subscriptions.First();
var templateFolder = subscription.TemplatesFolder.FolderId;
var exportFolder = subscription.ExportsFolder.FolderId;

之后,我们进入为云创建报告的阶段。你可以这样做:

TemplateCreateVM templateCreateVM = new TemplateCreateVM()
{
Name = "box.frx",
Content = Convert.FromBase64String(TestData.BoxReport)
//we send the frx file in byte format
};

在上面的示例中,我们已经有一个字节格式的报告。如果您有 frx 格式的文件,则可以使用以下示例:

TemplateCreateVM templateCreateVM = new TemplateCreateVM()
{
Name = "box.frx",
Content = File.ReadAllBytes("path to report")
//we send the frx file in byte format to the path
};

我们将 TemplateCreateVM 对象与报表一起上传到 FastReport.Cloud 工作区:

TemplateVM uploadedFile = await rpClientTemplates.UploadFileAsync(templateFolder, templateCreateVM);

现在我们将报告导出为我们需要的格式。首先,您需要决定未来文件的格式和名称。

ExportTemplateVM export = new ExportTemplateVM()
{
FileName = "box",
Format = ExportFormat.Pdf
//format to be exported
};

我们导出为 PDF 格式:

ExportVM exportedFile = await rpClientTemplates.ExportAsync(uploadedFile.Id, export) as ExportVM;
string fileId = exportedFile.Id;
int attempts = 3;
exportedFile = rpClientExports.GetFile(fileId);
while (exportedFile.Status != FileStatus.Success && attempts >= 0)
{
await Task.Delay(1000);
exportedFile = rpClientExports.GetFile(fileId);
attempts--;
}

我们通过报告完成了主要工作。我们收到了报告中的 pdf 文件:

结果导出

如果您想手动下载文件,请转到您的工作区并下载它,如下例所示:

下载导出的文件

您还可以使用以下示例通过 SDK 下载文件:

using (var file = await downloadClient.GetExportAsync(fileId))
{
using (var pdf = File.Open("report.pdf", FileMode.Create))
{
file.Stream.CopyTo(pdf);
}
}

现在您知道如何使用 SDK 在 FastReport Cloud 中创建、导出和下载文件。您可以通过以下链接找到本文中的示例:GitHub - FastReports/FastReport-Cloud

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值