views:

127

answers:

1

I've got a data collection routine that takes about 10 seconds to run, then it saves data to a CSV file:

string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Book1.csv");
StreamWriter streamWriter1 = new StreamWriter(File.Open(file, FileMode.Create, FileAccess.Write));
DataTable table = GetMyData(); // takes about 10 seconds
foreach (DataRow row in table.Rows) {
  object[] item = row.ItemArray;
  for (int i = 0; i < item.Length; i++) {
    streamWriter1.Write(item.ToString() + ",");
  }
  streamWriter1.WriteLine();
}
streamWriter1.Close();
Process.Start("Excel", "book1.csv");

Excel takes a few moments to start as well (5 to 10).

I'd like to modify this technique so that I call Excel just before my data collection so the application will be running by the time I have collected the data, then just have it display the file with the data.

With this in mind, here's what I modified the code to, but it always tells me the file is not there (even though it is):

Process excel = Process.Start("Excel");
if (excel != null) {
  string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Book1.csv");
  StreamWriter streamWriter1 = new StreamWriter(File.Open(file, FileMode.Create, FileAccess.Write));
  DataTable table = GetMyData(); // takes about 10 seconds
  foreach (DataRow row in table.Rows) {
    object[] item = row.ItemArray;
    for (int i = 0; i < item.Length; i++) {
      streamWriter1.Write(item.ToString() + ",");
    }
    streamWriter1.WriteLine();
  }
  streamWriter1.Close();
  for (int i = 0; i < 100; i++) { // simulate the data collection routine
    Thread.Sleep(100);
  }
  excel.StartInfo.Arguments = file;
  excel.StartInfo.ErrorDialog = true;
  excel.StartInfo.UseShellExecute = false;
  excel.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  try {
    excel.Start();
  } catch (Exception err) {
    Console.WriteLine(err.Message); // <= Message is "The system cannot find the file specified"
  }
}

Any thoughts? How would I correctly send the file to the active process?

+2  A: 

You cannot specify the startInfo after the process has been started. StartInfo is for starting the process.

You may be able to do what you want byb starting Excel using Process.Start(), and then after data collection, using Excel automation to tell Excel to open a particular file.

// connect to, or start, Excel:
Excel.Application xl=new Excel.ApplicationClass(); 

Excel.Workbook wb = xl.Workbooks.Open(Environment.CurrentDirectory+"/SampleExcel.xls",
                                      0,
                                      false,
                                      5,
                                      System.Reflection.Missing.Value,
                                      System.Reflection.Missing.Value,
                                      false,
                                      System.Reflection.Missing.Value,
                                      System.Reflection.Missing.Value,
                                      true,
                                      false,
                                      System.Reflection.Missing.Value,
                                      false,
                                      false,
                                      false);
Cheeso
So that's how it is done. :) I've been told by some to avoid the Excel automation if I can since the MS Office versions change as quickly as the VS versions - and they are not guaranteed to be compatible.I just spent some time looking at your other posts, too. I particularly liked "circular buffer" and "Thread.Start -vs- Thread.Pool". I looked at "VS2010 vs VS2008" and felt humbled. I've yet to use the full potential of generics, introduced with VS2005. I wished I had a way to force myself to use the newest technologies.Just rambling...
jp2code
As a crude solution to my dilemma, I might look at calling two thread start routines: Start.Process("Excel"), do my data collection routines, then call Start.Process("Excel", file)....or maybe I'll use what you've got up there. Lots of parameters, though.(New on StackOverflow) How do I get formatting to work on my comments? Do I have to manually include some sort of carriage return?
jp2code
I don't think formatting works in comments. You can get italics and bold, but that's about it. No linebreaks. On the concerns you have with Excel automation, it depends. You can look into other questions on that topic. My understanding is that Excel is backwards compatible. The Excel version may change but the old Interop assembly will work with the new version.
Cheeso