c# - XamarinForms(Visual Studio) 中的报告
问题描述
当我正在攻读学士学位时,要求之一是能够在应用程序中看到一些报告(如 SSRS 或 Crystal Reports)。正如标题所说,我正在 XamarinForms 跨平台中执行此操作(目前,如果我能够在 android 中查看它们并且 wpf 会很棒)。
我搜索了,我没有找到具体的东西,我不知道该怎么做。
我从 .NET Core 2.1 中的 RESTful API 获取我的数据,该 API 发布在 azure 云上,以及我的数据库。我试图在 API 中制作一个 pdf 并返回它,以便我可以在 XamarinForms 中查看它,但我对这种方法有一些疑问。
我必须指定 Azure Cloud 帐户不是我的,而且我不知道如何使用他们服务中列出的所有选项。
有没有办法让它干净又漂亮?如果是,你能解释一下工作流程以及如何做吗?
谢谢!
解决方案
创建 Pdf 是最好的选择,这在 xamarin 表单上完美运行,使用 PDFSharp 或使用 IO Stream Class
public string SaveFiles(PDFFormatModel pd)
{
PdfPTable table = new PdfPTable(2);
PdfPCell cell = new PdfPCell(new Phrase("Cell Name"));
cell.Colspan = 2;
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
table.AddCell(cell);
table.AddCell("Cell 1 ");
table.AddCell("Cell 1 Value");
table.AddCell("Cell 2 ");
table.AddCell("Cell 2 Value");
var pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
string root = null;
if (Android.OS.Environment.IsExternalStorageEmulated)
{
root = Android.OS.Environment.ExternalStorageDirectory.ToString();
}
else
root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Java.IO.File myDir = new Java.IO.File(root + "/MAMN");
myDir.Mkdir();
Java.IO.File file = new Java.IO.File(myDir, pd.FileName);
if (file.Exists()) file.Delete();
using (var memoryStream = new System.IO.MemoryStream())
{
FileOutputStream outs = new FileOutputStream(file);
var writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
pdfDoc.Add(table);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var filePath = Path.Combine(root, pd.FileName);
outs.Write(bytes);
outs.Flush();
outs.Close();
if (file.Exists())
{
Android.Net.Uri path = Android.Net.Uri.FromFile(file);
string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(path, mimeType);
Forms.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
}
return filePath;
}
}
此外,如果您需要在图表或图形中表示数据,请使用 MicroCharts 或 Syncfusion
推荐阅读
- spring - 对象映射器反序列化 json 后的 @autowire bean 和 @value 属性
- angular - 如何在 onChange 的 ngModel 中设置值?
- amazon-s3 - 使用 Airflow 将数据从 Redshift 卸载到 S3
- sql - 如何在sql中将字符串连接成固定长度
- c# - 用 sql 命令玩数学
- android - 自定义视图不会在 Fragment 内更新
- php - 更新小部件存档下拉标签文本
- node.js - SocketIO——连接问题和集群
- javascript - 如何在 React Native 中的循环中查找
- c# - PostAsync() 方法在 dot-net 核心窗口服务中不起作用