Answered by:
How to execute a "Refresh All" for Excel programmatically in C#?

Question
-
I've written an application to read in some data from an Excel spreadsheet using an oledbconnection. Some of my data is from an external datasource which is why I would need to refresh the data before reading it.
My external data source is not a database, but an asp page and I'm not sure how to create an oledbconnection to an asp page, but I do know how to create an external connection from Excel to the datasource.
It is possible to run an excel "refresh all" from my c# application?Friday, November 6, 2009 9:04 PM
Answers
-
Yes it's possible.
Code Snippet- private void _DataRefresh(String S_Directory)
- {
- if (S_Directory.Substring(S_Directory.Length - 1, 1) != @"\")
- {
- S_Directory = S_Directory + @"\";
- }
- DirectoryInfo finfo = new DirectoryInfo(S_Directory);
- foreach (FileInfo fileName in finfo.GetFiles("*.xls*"))
- {
- if (isOpen(S_Directory + fileName) && fileName.ToString().Substring(1, 2) != "~$")
- {
- try
- {
- xls.Application ExcelObj = new xls.Application();
- ExcelObj.DisplayAlerts = true;
- ExcelObj.Visible = true;
- ExcelObj.DefaultFilePath = S_Directory;
- xls.Workbook eBook = ExcelObj.Workbooks.Open(fileName.ToString(), false, false,
- Type.Missing, "", "", true, xls.XlPlatform.xlWindows, "",
- false, false, 0, false, true, 0);
- foreach (xls.WorkbookConnection wc in eBook.Connections)
- {
- if (wc.Type.ToString() == "xlConnectionTypeODBC")
- {
- wc.ODBCConnection.BackgroundQuery = false;
- }
- else
- {
- wc.OLEDBConnection.BackgroundQuery = false;
- }
- }
- eBook.RefreshAll();
- eBook.Save();
- NAR(eBook);
- ExcelObj.Workbooks.Close();
- ExcelObj.Quit();
- NAR(ExcelObj);
- }
- catch (COMException e)
- {
- ErroredFiles.Add(fileName.ToString());
- WriteErrorMessage(S_Directory, fileName.ToString(), e.Message.ToString());
- }
- }
- else { }
- }
- KillpIDs("Excel");
- if (ErroredFiles.Count > 0)
- {
- GenerateEmail("Data Refresh Failures");
- }
- }
- Marked as answer by NoodlesCoder Monday, November 9, 2009 2:06 PM
Friday, November 6, 2009 9:13 PM
All replies
-
Yes it's possible.
Code Snippet- private void _DataRefresh(String S_Directory)
- {
- if (S_Directory.Substring(S_Directory.Length - 1, 1) != @"\")
- {
- S_Directory = S_Directory + @"\";
- }
- DirectoryInfo finfo = new DirectoryInfo(S_Directory);
- foreach (FileInfo fileName in finfo.GetFiles("*.xls*"))
- {
- if (isOpen(S_Directory + fileName) && fileName.ToString().Substring(1, 2) != "~$")
- {
- try
- {
- xls.Application ExcelObj = new xls.Application();
- ExcelObj.DisplayAlerts = true;
- ExcelObj.Visible = true;
- ExcelObj.DefaultFilePath = S_Directory;
- xls.Workbook eBook = ExcelObj.Workbooks.Open(fileName.ToString(), false, false,
- Type.Missing, "", "", true, xls.XlPlatform.xlWindows, "",
- false, false, 0, false, true, 0);
- foreach (xls.WorkbookConnection wc in eBook.Connections)
- {
- if (wc.Type.ToString() == "xlConnectionTypeODBC")
- {
- wc.ODBCConnection.BackgroundQuery = false;
- }
- else
- {
- wc.OLEDBConnection.BackgroundQuery = false;
- }
- }
- eBook.RefreshAll();
- eBook.Save();
- NAR(eBook);
- ExcelObj.Workbooks.Close();
- ExcelObj.Quit();
- NAR(ExcelObj);
- }
- catch (COMException e)
- {
- ErroredFiles.Add(fileName.ToString());
- WriteErrorMessage(S_Directory, fileName.ToString(), e.Message.ToString());
- }
- }
- else { }
- }
- KillpIDs("Excel");
- if (ErroredFiles.Count > 0)
- {
- GenerateEmail("Data Refresh Failures");
- }
- }
- Marked as answer by NoodlesCoder Monday, November 9, 2009 2:06 PM
Friday, November 6, 2009 9:13 PM -
That was quick.
Is there a way to reuse the following code I've written to connect to my Excel file, or can Refresh all not be used with these data types?
// Create a connection to excel spreadsheet
string connectionString = null;
OleDbConnection cnn;
connectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Test.xls;Extended Properties=Excel 8.0;";
cnn =
new OleDbConnection(connectionString);
Friday, November 6, 2009 9:24 PM -
The Refresh All is part of the client code, so I believe you'd need to use the Excel PIA to accomplish that.Friday, November 6, 2009 10:32 PM
-
Thanks for the clarification.
Monday, November 9, 2009 2:06 PM