Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I currently have an excel pivot report that is refreshed daily for my users. I have created 2 exact reports. Each report points to a different SQL Server in the case that my 1st server fails.

I would like to have 1 report where I can change the data source pro-grammatically instead of trying to maintain changes in 2 reports.


I have C# routine that opens the report currently, refreshes it and then saves it out to the users folders.

Is there a way that I can set the data source in the pivot report before refreshing it so I don't have to maintain 2 reports? Either in VBA in the Spreadsheet or through the C# app?

I use a Microsoft.Office.Interop.Excel object in the c#. If I do it in VBA, how do I pass the parameter to tell the report which server?

Thanks!
Posted
Updated 16-Dec-10 17:08pm
v2
Comments
Maciej Los 17-Dec-10 16:01pm    
Show me your code...

1 solution

void RefreshWorkbookTwo(string SourcePath, string SourceFilename, string ReportName, Guid ExecutionID )
{
try
{
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

excelApp.DisplayAlerts = false;

// if Visible is set to false we get an exception for some reason
excelApp.Visible = false;

Workbook excelWorkbook = excelApp.Workbooks.Open(
SourcePath + SourceFilename,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value);

// pivot tables are normally refreshed in a background thread
// setting this to false means that we can save synchronously

Console.WriteLine("Iterating through the Query Tables...");


foreach (Worksheet ws in excelWorkbook.Worksheets)
{
foreach (QueryTable qt in ws.QueryTables )
{
qt.BackgroundQuery = false;
}
}


foreach (PivotCache cache in excelWorkbook.PivotCaches())
{
cache.BackgroundQuery = false;
}


Console.WriteLine("Refreshing...");

excelWorkbook.RefreshAll();

var refreshedWorkBookPath = Path.Combine(Path.GetDirectoryName(SourcePath + SourceFilename), Path.ChangeExtension(Path.GetFileName(Path.GetTempFileName()), "xls"));

Console.WriteLine("Saving...");

excelWorkbook.Save();

// ensure all excel objects are closed and references are released
// if this is not done an instance of excel will stay in the running processes
excelWorkbook.Close(false, SourcePath + SourceFilename, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook);
excelWorkbook = null;

excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
excelApp = null;

}
catch (Exception e)
{
///Sending message to me and Marie Coleman on failure
String failureMessage = "The " + ReportName + " failed " + DateTime.Now.ToString() + " with the following error:\n\nSource: " + e.Source.ToString() + "\n\nMessage: " + e.Message.ToString();
SendMessage(ReportName + " Application Failure", failureMessage);
///Writing to error file
ProcessLog(ExecutionID, "ReportRunner application error", "ERROR", DateTime.Now, "ReportRunner", "ReportRunner Application", "Application Error", null, null, failureMessage);

///Killing the Excel process if it's active
KillProcesses("EXCEL");

}

finally
{
KillProcesses("EXCEL");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900