internal static void SetParameter(CrystalHelper crystalHelper, ISearchExpression se) { if (se == null) return; LogicalExpression le = se as LogicalExpression; if (le != null) { SetParameter(crystalHelper, le.LeftHandSide); SetParameter(crystalHelper, le.RightHandSide); } else { SimpleExpression cse = se as SimpleExpression; string simpleParamName = "@" + cse.FullPropertyName; string complexParamName = "@" + cse.FullPropertyName + cse.Operator.ToString(); switch (cse.Operator) { case SimpleOperator.Any: case SimpleOperator.EqProperty: case SimpleOperator.IsNotNull: case SimpleOperator.IsNull: case SimpleOperator.NotEq: case SimpleOperator.NotEqProperty: case SimpleOperator.Sql: throw new ArgumentException(cse.Operator + " is not supported in procedure!"); case SimpleOperator.Ge: case SimpleOperator.Gt: case SimpleOperator.Le: case SimpleOperator.Lt: crystalHelper.SetParameter(complexParamName, cse.Values); break; case SimpleOperator.Eq: case SimpleOperator.GInG: case SimpleOperator.InG: case SimpleOperator.Like: crystalHelper.SetParameter(simpleParamName, cse.Values); break; } } }
/// <summary> /// 生成报表 /// </summary> /// <param name="reportInfoName"></param> /// <param name="dateStart"></param> /// <param name="dateEnd"></param> /// <returns></returns> public static byte[] GenerateReport(string reportInfoName, DateTime dateStart, DateTime dateEnd) { CrystalHelper crystalHelper = new CrystalHelper(); ReportInfo reportInfo = ADInfoBll.Instance.GetReportInfo(reportInfoName); if (reportInfo == null) { throw new ArgumentException("不存在名为" + reportInfoName + "的ReportInfo!"); } ReportDocument reportDocument = ReportHelper.CreateReportDocument(reportInfo.ReportDocument); crystalHelper.ReportSource = reportDocument; System.Data.DataSet templateDataSet = ReportHelper.CreateDataset(reportInfo.DatasetName); IList<ISearchManager> sms = new List<ISearchManager>(); IList<ReportDataInfo> reportDataInfos = ADInfoBll.Instance.GetReportDataInfo(reportInfo.Name); foreach (ReportDataInfo reportDataInfo in reportDataInfos) { if (string.IsNullOrEmpty(reportDataInfo.SearchManagerClassName)) { throw new ArgumentException("ReportDataInfo of " + reportDataInfo.Name + " 's SearchManagerClassName must not be null!"); } ISearchManager sm = ServiceProvider.GetService<IManagerFactory>().GenerateSearchManager(reportDataInfo.SearchManagerClassName, reportDataInfo.SearchManagerClassParams); sm.EnablePage = false; sms.Add(sm); } ISearchExpression se = SearchExpression.And(SearchExpression.Ge("日期", dateStart), SearchExpression.Le("日期", dateEnd)); for (int i = 0; i < reportDataInfos.Count; ++i) { System.Collections.IEnumerable dataList = sms[i].GetData(se, null); string s = reportDataInfos[i].DatasetTableName; if (!templateDataSet.Tables.Contains(s)) { throw new ArgumentException("报表DataSet中未包含名为" + s + "的DataTable!"); } System.Data.DataTable dt = templateDataSet.Tables[s]; dt.Rows.Clear(); GenerateReportData.Generate(dt, dataList, reportDataInfos[i].GridName); } // Set Parameter SetParameter(crystalHelper, se); crystalHelper.DataSource = templateDataSet; string fileName = System.IO.Path.GetTempFileName(); crystalHelper.Export(fileName, CrystalExportFormat.PortableDocFormat); System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] fileData = new byte[fs.Length]; using (System.IO.BinaryReader sr = new System.IO.BinaryReader(fs)) { sr.Read(fileData, 0, fileData.Length); } fs.Close(); System.IO.File.Delete(fileName); return fileData; }
protected override void Dispose(bool disposeManaged) { if (disposeManaged) { _crystalHelper.Close(); _crystalHelper.Dispose(); _crystalHelper = null; m_ds.Dispose(); m_ds = null; } base.Dispose(disposeManaged); }