zoukankan      html  css  js  c++  java
  • C#使用FastReport之调用网络位置的模板文件打印

    处理思路:
    FastReport加载模板文件时提供了两种方法:
    public void Load(Stream stream);//加载流
    public void Load(string fileName);//加载指定路径的模板文件
    处理本地frx文件:可以直接将本地模板文件路径字符串传到Load方法加载模板
    处理服务器上的frx文件:可以先用HttpWebRequest请求web资源,并将请求内容放入Stream再传给Load方法加载模板
    1.创建tmpInfo.cs的model,用于返回模板信息

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace PrintTool
    {
    /// <summary>
    /// 模板信息
    /// </summary>
    public class tmpInfo
    {
    /// <summary>
    /// 模板类型
    /// Local本地
    /// NetResource网络资源
    /// </summary>
    public string tmpType { get; set; }//Local/NetResource
    
    /// <summary>
    /// 模板名称-本地化的模板
    /// </summary>
    public string tmpFileName { get;set;}
    
    /// <summary>
    /// 模板流-服务端的模板流
    /// </summary>
    public Stream tmpStream { get; set; }
    
    /// <summary>
    /// 模板处理是否成功
    /// </summary>
    public bool tempCovSuccess { get; set; }
    
    /// <summary>
    /// 说明
    /// </summary>
    public string tempCovRemarks { get; set; }
    }
    }
    

     2.创建获取模板的方法

    /// <summary>
    /// 获取模板
    /// </summary>
    /// <param name="tempUrl"></param>
    /// <returns></returns>
    public static tmpInfo GetTmp(string tempUrl)
    {
    tmpInfo tmpInfo = new tmpInfo();
    tmpInfo.tempCovSuccess = false;
    try
    {
    if (tempUrl.Contains("http://"))//网络模板
    {
    HttpWebRequest httpwebr = (HttpWebRequest)HttpWebRequest.Create(tempUrl);
    httpwebr.Method = "GET";
    Stream s = httpwebr.GetResponse().GetResponseStream();
    byte[] buffer = new byte[1024];
    int actual = 0;
    //先保存到内存流中MemoryStream
    MemoryStream ms = new MemoryStream();
    while ((actual = s.Read(buffer, 0, 1024)) > 0)
    {
    ms.Write(buffer, 0, actual);
    }
    ms.Position = 0;
    tmpInfo.tmpType = "NetResource";
    tmpInfo.tmpStream = ms;
    tmpInfo.tempCovSuccess = true;
    return tmpInfo;
    }
    else//本地模板
    {
    string tmpPath = @".PrintTmpl" + tempUrl.Trim();
    if (System.IO.Directory.Exists(tmpPath))
    {
    tmpInfo.tmpType = "Local";
    tmpInfo.tempCovSuccess = false;
    tmpInfo.tempCovRemarks = "找不到打印模板" + tmpPath;
    return tmpInfo;
    }
    tmpInfo.tmpType = "Local";
    tmpInfo.tmpFileName = tmpPath;
    tmpInfo.tempCovSuccess = true;
    return tmpInfo;
    }
    }
    catch (Exception ex)
    {
    tmpInfo.tempCovRemarks = "模板处理异常" + ex.Message;
    }
    return tmpInfo;
    }
    

      

    3.业务调用

    Report report = new Report();
    tmpInfo tmpinfo = GetTmp(TmplFile.Trim());
    if (!tmpinfo.tempCovSuccess)
    {
    WriteLog.WriteMessage(pr.Rd);
    return pr;
    }
    else
    {
    if (tmpinfo.tmpType == "Local")
    {
    report.Load(tmpinfo.tmpFileName);//加载本地的模板文件
    }
    else
    {
    report.Load(tmpinfo.tmpStream);//加载web的模板文件
    }
    }
    //
    //....
    //
    //组织report的数据
    report.Print();//调用打印
    

      

  • 相关阅读:
    webpack devServer配置项的坑
    app混合开发 fastlick.js 在ios上 input标签点击 不灵敏 处理
    vue 学习八 自定义指令
    Verilog数值大小比较
    Verilog实现Matlab的fliplr函数
    基本不等式
    如何读取ila数据
    Xilinx FPGA时钟IP核注意事项
    FPGA Turbo译码器注意事项
    EbN0转SNR
  • 原文地址:https://www.cnblogs.com/soulsjie/p/14307071.html
Copyright © 2011-2022 走看看