将excel文件保存在我之前打开过两个窗口的文件夹中

本文关键字:两个 窗口 文件夹 保存 文件 excel 存在 | 更新日期: 2024-09-19 14:17:04

我在窗口2:

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

现在,在下一个窗口中,我创建了一个excel文件:

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与上一个窗口上的finalpath的路径相同。目标是将excel文件保存在我在window2上打开的文件夹中。有人能帮帮我吗?

将excel文件保存在我之前打开过两个窗口的文件夹中

您需要将目录传递到第二个窗口,或者访问第二个窗中的UserPath对象以获取文本。

如果它们是两个不同的窗口,您可以将包含目录路径的字符串参数作为参数传递给第二个窗口,然后在oWB.SaveAs(filePath + @"/finalPath2.text"); 中使用该参数

如果第二个窗口可以访问UserPath对象,另一个选项是将其包含在另存为命令中,如oWB.SaveAs(UserPath.Text + @"/finalPath2.text");