Files
HJGL_DS/HJGL_DS/BLL/Common/FastReportService.cs
T

135 lines
4.6 KiB
C#

using FastReport;
using NPOI.POIFS.Crypt;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Security;
namespace BLL.Common
{
public static class FastReportService
{
// 缓存过期时间:30分钟
private static readonly int CacheExpirationMinutes = 5;
/// <summary>
/// 重置数据 - 清理缓存和Session
/// </summary>
public static void ResetData()
{
if (HttpContext.Current?.Session == null)
{
return;
}
// 清理Session中的缓存键引用
var cacheKeys = HttpContext.Current.Session["ReportDataCacheKeys"] as List<string>;
if (cacheKeys != null)
{
foreach (var key in cacheKeys)
{
try
{
CacheHelper.Remove(key);
}
catch
{
// 忽略缓存删除异常
}
}
}
HttpContext.Current.Session["ReportDataCacheKeys"] = null;
HttpContext.Current.Session["ReportParameterCacheKey"] = null;
}
/// <summary>
/// 添加报表数据表到缓存
/// </summary>
public static void AddFastreportTable(DataTable dataTable)
{
// 生成唯一缓存键
string cacheKey = $"ReportData_{Guid.NewGuid()}_{DateTime.Now.Ticks}";
// 将数据表存储到缓存中,设置5分钟过期
CacheHelper.Add(cacheKey, dataTable, DateTimeOffset.Now.AddMinutes(CacheExpirationMinutes));
List<string> cacheKeys = HttpContext.Current.Session["ReportDataCacheKeys"] as List<string>;
if (cacheKeys==null)
{
cacheKeys = new List<string>();
}
cacheKeys.Add(cacheKey);
HttpContext.Current.Session["ReportDataCacheKeys"] = cacheKeys;
}
/// <summary>
/// 添加报表参数到缓存
/// </summary>
public static void AddFastreportParameter(Dictionary<string, string> Dicparames)
{
// 生成唯一缓存键
string cacheKey = $"ReportParam_{Guid.NewGuid()}_{DateTime.Now.Ticks}";
// 将参数存储到缓存中,设置5分钟过期
CacheHelper.Add(cacheKey, Dicparames, DateTimeOffset.Now.AddMinutes(CacheExpirationMinutes));
// Session只存储缓存键的引用(轻量级)
HttpContext.Current.Session["ReportParameterCacheKey"] = cacheKey;
}
/// <summary>
/// 合并报表后打印
/// </summary>
/// <param name="fastReportItems">报表实体集</param>
/// <param name="path">导出的路径</param>
public static void ExportMergeReport(List<Model.FastReportItem> fastReportItems, string path, string printType)
{
Report report = new Report();
report.PrintSettings.ShowDialog = false; //隐藏弹窗
FastReport.EnvironmentSettings settings = new EnvironmentSettings();
settings.ReportSettings.ShowProgress = false; //隐藏进度条
for (int i = 0; i < fastReportItems.Count; i++)
{
report.Load(Funs.RootPath + fastReportItems[i].ReportPath); //加载报表
List<DataTable> dataTables = fastReportItems[i].DataTables;
Dictionary<string, string> ParameterValues = fastReportItems[i].ParameterValues;
if (dataTables != null && dataTables.Count > 0)
{
for (int j = 0; j < dataTables.Count; j++)
{
report.RegisterData(dataTables[j], dataTables[j].TableName);//绑定数据源
}
}
if (ParameterValues.Count > 0)
{
foreach (KeyValuePair<string, string> kvp in ParameterValues)
{
report.SetParameterValue(kvp.Key, kvp.Value);//绑定参数
}
}
if (i == 0)
{
report.Prepare();
}
else
{
report.Prepare(true);
}
}
FastReport.Export.Pdf.PDFExport exp = new FastReport.Export.Pdf.PDFExport();
report.Export(exp, path);
}
}
}