我有一个使用Excel 2013 PowerPivot创建的文档...它连接到我托管的WCF数据服务...数据连接工作正常,所有的数据都被成功地带回并填充到我的PowerPivot图表/表中。
数据托管在一个名为Data Feed URL的网址上,看起来像这样:
http://mydomain/Reporting/WcfDataService1.svc/
我有数十个客户,每个客户都需要接收相同的文档...但是需要编辑DataFeedURL以指向他们托管的数据服务...每个客户都会拥有不同的URL...例如:
http://Client1/Reporting/WcfDataService1.svc/
http://Client2/Reporting/WcfDataService1.svc/
长话短说,我希望客户使用我的C# Windows表单应用程序,该应用程序将引用模板PowePivot报告...当用户完成有关其公司的各种信息设置后,我需要以编程方式保存包含与其新URL的更新连接的PowerPivot报告版本,该URL是由我生成的。 我尝试按以下方式执行此操作:
string workbookPath = String.Format("{0}\\{1}", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PowerPivotSource.xlsx");
string workbookPath2 = String.Format("{0}\\{1}", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PowerPivotTestOut.xlsx");
foreach (Excel.WorkbookConnection connection in excelWorkbook.Connections)
{
if (connection.Type == Excel.XlConnectionType.xlConnectionTypeDATAFEED)
{
Excel.DataFeedConnection dataFeedCur = connection.DataFeedConnection;
dataFeedCur.Connection = dataFeedCur.Connection.Replace("mydomain", "Client1");
break;
}
}
excelWorkbook.Close(true);
我能读取dataFeedCur.Connection的内容,但是试图像这样编辑它是行不通的。
dataFeedCur.Connection = dataFeedCur.Connection.Replace("localhost", "Client1");
抛出以下异常:
Exception Message: "Exception from HRESULT: 0x800A03EC"
Exception Stack Trace: "" at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)\r\n at Microsoft.Office.Interop.Excel.DataFeedConnection.set_Connection(Object value)\r\n
有些迹象表明WorkBookConnection.DataFeedConnection对象是只读的...但我听说其他论坛上的人谈论过能够编辑这个连接字符串,但没有看到任何例子...有人知道如何编辑DataFeedConnection.Connection对象吗?