Question

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?

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top