我有一个显示所有水晶报告的页面,如下所示:
这些报告中有一个(遗憾的是,硬编码的)网址公式,指向例如https://myserver.bla.bla
我想以某种方式以编程方式查找url的实例,并将其更改为其他内容(这些报告有数百个,现在没有足够的时间进入并更改所有链接)。
我一直在寻找FieldObjects,但似乎无法弄清楚如何更改其格式化公式。当我查看reportdocument.fieldformulas时,url格式化公式不存在。
public partial class Report:System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
iLogger logger = LoggingFactory.CreateLogger();
ReportDocument rd = new ReportDocument();
string fileName = Request.QueryString["reportfile"];
if(!Regex.IsMatch(fileName,@"^[ 0-9a-zA-Z-_\\]+.rpt$"))
{
ArgumentException aex = new ArgumentException("Invalid file/path specified.");
logger.LogError(ActionTypes.Administration, HttpContext.Current.User.Identity.Name,
"Passed invalid file path to report viewer: " + fileName, aex);
throw aex;
}
if(Path.IsPathRooted(fileName))
{
ArgumentException aex = new ArgumentException("Absolute path passed to report viewer.");
logger.LogError(ActionTypes.Administration, HttpContext.Current.User.Identity.Name,
"Passed invalid file path to report viewer: " + fileName, aex);
throw aex;
}
string rootPath = Server.MapPath("~/Reports/");
string path = Path.Combine(rootPath, fileName);
if (File.Exists(path))
{
rd.Load(path);
}
//get all keys starting with Prompt
var prompts = Request.QueryString.AllKeys.Where(q => q.StartsWith("Prompt"));
foreach (string promptKey in prompts)
{
//try to convert the rest of the string to an int
//yes, this should probably not just be a replace here...
string withoutPrompt = promptKey.Replace("Prompt", "");
int promptVal;
if (int.TryParse(withoutPrompt, out promptVal))
{
rd.SetParameterValue(promptVal, Request.QueryString[promptKey]);
}
//rd.SetParameterValue(promptKey, Request.QueryString[promptKey]);
CrystalReportViewer1.ReportSource = rd;
}
}
答案 0 :(得分:1)
确定。我至少找到了如何获取URL,所以我想我会与每个人分享我得到的内容。
CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument cDoc = rd.ReportClientDocument;
ReportObjects reportObjects = rd.ReportDefinition.ReportObjects;
CrystalDecisions.ReportAppServer.ReportDefModel.ReportObjects objects =
cDoc.ReportDefController.ReportObjectController.GetReportObjectsByKind(
CrystalDecisions.ReportAppServer.ReportDefModel.CrReportObjectKindEnum.crReportObjectKindField);
foreach(ISCRReportObject obj in objects)
{
//CrystalDecisions.ReportAppServer.ReportDefModel.ObjectFormatConditionFormulas formulas =obj.Format.ConditionFormulas[];
ConditionFormula hyperFormulas = obj.Format.ConditionFormulas[CrObjectFormatConditionFormulaTypeEnum.crObjectFormatConditionFormulaTypeHyperlink];
if (hyperFormulas != null && hyperFormulas.Text != null)
{
hyperFormulas.Text = hyperFormulas.Text.Replace(@"https://my/old/url","https://my/new/url");
}
}
我实际上并不认为这会改变公式:(
我认为我会在值设定后修改它,遗憾的是。
活泉。知道了:))
我的修改例程如下:
foreach(ISCRReportObject obj in objects)
{
//CrystalDecisions.ReportAppServer.ReportDefModel.ObjectFormatConditionFormulas formulas =obj.Format.ConditionFormulas[];
ConditionFormula hyperFormulas = obj.Format.ConditionFormulas[CrObjectFormatConditionFormulaTypeEnum.crObjectFormatConditionFormulaTypeHyperlink];
if (hyperFormulas != null && hyperFormulas.Text != null)
{
hyperFormulas.Text = hyperFormulas.Text.Replace(@"{{old url}}",{{new url}});
cDoc.ReportDefController.ReportObjectController.Modify(
cDoc.ReportDefController.ReportDefinition.FindObjectByName(obj.Name), obj);
}
}
效果很好!