使用 c# + vue 制作 DevExpress 报表

一、下载

DevExpress 下载地址: docs.devexpress.com/XtraReports...

二、创建报表

选择你要放置的文件夹,依次选择 "Add", "New Item..."

第一次显示时可能没有详情页面,点击右下角 "Show All Template"

选择 "Blank" 模板

三、添加工具栏

若工具栏没有显示,可以按下 "ctrl + alt + x" 呼出

还有一些其他工具栏可从上方 XtraReports 中选择

四、 绑定 Json 格式数据源

点击右上方小正方形添加数据源

将你自己的 json 数据粘贴到下方,它会自动解析为对象 添加完毕后拖动数据制作报表

点击下方按钮即可预览

下列代码是将查询的数据转化为 json 作为数据源制作报表,并以byte数组的形式返回前端

csharp 复制代码
 public byte[] GetOrderConfirmationReport(string buid, string orderInfos)
 {
     // 执行数据查询 
     OrderInfo orderInfo = GetOrderById(buid, orderInfos);
     // 转化为 json 字符串
     string queryResult = JsonConvert.SerializeObject(orderInfo);
     // 绑定数据源
     XtraReport1 report = new XtraReport1()
     {
         DataSource = CreateDataSourceFromText(queryResult),
     };
     // 指定pdf格式
     PdfExportOptions pdfOptions = report.ExportOptions.Pdf;

     // Specify the quality of exported images.
     pdfOptions.ConvertImagesToJpeg = false;
     pdfOptions.ImageQuality = PdfJpegImageQuality.Medium;

     // Specify the PDF/A-compatibility.
     pdfOptions.PdfACompatibility = PdfACompatibility.PdfA3b;
     
     // 指定pdf标题,这个参数影响前端的标题显示!!!
     pdfOptions.DocumentOptions.Title = "Order Confirmation Report";

     string fileName = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
     string pdfPath = $".\Downloads\{fileName}.pdf";
     // 将报表导出到指定目录下
     report.ExportToPdf(pdfPath);
     // 读取报表内容,转化为byte[]
     byte[] res = GetSignaturePDFByte(pdfPath);
     // 删除报表
     File.Delete(pdfPath);

     return res;
 }

 private JsonDataSource CreateDataSourceFromText(string json)
 {
     var jsonDataSource = new JsonDataSource();

     // Specify the object that retrieves JSON data.
     jsonDataSource.JsonSource = new CustomJsonSource(json);
     // Populate the data source with data.
     jsonDataSource.Fill();
     return jsonDataSource;
 }

 private static byte[] GetSignaturePDFByte(string srcPdfFile)
 {
     using (FileStream fsRead = new FileStream(srcPdfFile, FileMode.Open, FileAccess.Read, FileShare.Read))
     {
         int fsLen = (int)fsRead.Length;
         byte[] hebyte = new byte[fsLen];
         fsRead.Read(hebyte, 0, hebyte.Length);
         return hebyte;
     }
 }
}

导出参数可参考:docs.devexpress.com/XtraReports...

五、测试

Swagger 返回结果如下:

六、前端处理

下列代码的作用是,新建一页并将接收数据转化为pdf显示

ini 复制代码
const exportConfirmation = ()=>{
  getOrderConfirmationReport(store.currentBU).then(res=>{
    var newWindow = window.open("");
    newWindow.document.body.style.margin = 0
    newWindow.document.body.innerHTML = 
    '<iframe frameborder="0" style="width:100%;height:100%" src=data:application/pdf;base64,' + res + '>';
  })
}

pdf 内容如下:

相关推荐
桦说编程7 小时前
TransmittableThreadLocal 解读:从 CRR 机制到上下文所有权
java·后端·源码
喵个咪7 小时前
GoWind 拆解:全链 ID 类型体系——int64 定版与前端字符串化契约
前端·后端·go
喵个咪7 小时前
GoWind CMS 拆解:Flutter 设计系统内核——双态 token、四档断点与自适应壳层
前端·flutter·交互设计
喵个咪8 小时前
GoWind CMS 拆解:发布审批——多级推进、逐级角色与全程留痕
前端·后端·go
喵个咪8 小时前
GoWind Flow 拆解:一个 Go 工作流引擎与它的双模式设计器
前端·后端·go
喵个咪8 小时前
GoWind CMS 拆解:搜索三板斧——高亮、联想、同义词
后端·搜索引擎·go
喵个咪8 小时前
GoWind 拆解:AI Agent 编排引擎——波次并行、子图递归与人工审批
人工智能·后端·go
喵个咪8 小时前
GoWind Admin 拆解:打印模板设计器——从画布到服务端 PDF 的完整打印链路
前端·后端·go
总要学点什么8 小时前
第96篇:三维数据加载-城市建筑白模样式+着色器——让色带按体量上色,最白的那档却涂不上去
前端·gis·webgl·cesium·着色器·空间分析·白模
喵个咪9 小时前
GoWind Admin 拆解:BI 大屏设计器——schema 即 JSON、resolver 注入与三张快照表的口径谱系
大数据·后端·go