处理思路:
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();//调用打印