简体   繁体   中英

Save excel file in a folder that i opened 2 windows before

I opened a directory in window2:

private void CreateFolder()
{
    string path = @"C:\Users\ben\Desktop\מחקר";
    string param = string.Format("{0} {1}, {2}", UserFirstName.Text.ToString(), UserLastName.Text.ToString(), UserDate.Text.ToString());
    string finalPath = System.IO.Path.Combine(path, param);
    System.IO.Directory.CreateDirectory(finalPath);
    Userpath.Text = finalPath;
}

Now, on the next window I created an excel file:

private void button3_Click(object sender, RoutedEventArgs e)
{
    oXL = new Microsoft.Office.Interop.Excel.Application();
    oXL.Visible = false;
    oWB = (Microsoft.Office.Interop.Excel._Workbook)                                                                     (oXL.Workbooks.Add("")); 
    oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;
    oSheet.Range["A1"].Value = "Type";
    oSheet.Range["B1"].Value = "Time";
    oSheet.get_Range("A1", "B1").Font.Bold = true;
    oSheet.get_Range("A1", "B1").VerticalAlignment =
    Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
    oWB.SaveAs(@"finalpath2.Text");
 }

finalpath2 is the same path as the finalpath on the previous window. the goal is that the excel file will be saved inside the folder that I opened on window2. Can someone please help me?

You need to pass the directory to the second window, or access the UserPath object in the second window to get the text out.

If they are two different windows, you can pass as an argument to the second window a string parameter which contains the directory path and then use that in your oWB.SaveAs(filePath + @"/finalPath2.text");

The other option, if the second window can access the UserPath object is to include it in your save as command like so oWB.SaveAs(UserPath.Text + @"/finalPath2.text");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM