문제

실행하는 데 약 10 초가 걸리는 데이터 수집 루틴이있는 다음 CSV 파일에 데이터를 저장합니다.

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은 시작하는 데 몇 분이 걸립니다 (5-10).

데이터 수집 직전에 Excel을 호출하여 데이터를 수집 할 때까지 응용 프로그램이 실행될 수 있도록이 기술을 수정하려고합니다. 그런 다음 데이터와 함께 파일을 표시하도록합니다.

이것을 염두에두고, 여기에 코드를 수정 한 내용이 있지만, 파일이 항상 없음을 알려줍니다.

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"
  }
}

이견있는 사람? 파일을 활성 프로세스로 올바르게 보내는 방법은 무엇입니까?

도움이 되었습니까?

해결책

프로세스가 시작된 후에는 STARTINFO를 지정할 수 없습니다. StartInfo는 프로세스를 시작하기위한 것입니다.

BYB를 사용하여 Excel을 사용하여 원하는대로 할 수 있습니다. Process.Start(), 그리고 ~ 후에 데이터 수집, 사용 Excel 자동화 Excel에게 특정 파일을 열도록 지시합니다.

// 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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top