通过.NET办公室PIA坐落在Excel.Application当前目录 [英] Set Current Directory in Excel.Application via .NET Office PIA

查看:204
本文介绍了通过.NET办公室PIA坐落在Excel.Application当前目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从.NET应用程序创建一个Excel US preadsheet,然后设置一个Excel.Application实例当前目录自定义文件夹。我想这样做,这样,当用户点击在Excel中,另存为对话框已经位于正确的目录保存按钮。

I want to create an Excel spreadsheet from a .NET Application and then set the Excel.Application instance current directory to a custom folder. I want to do this so when the user clicks the save button in Excel, the Save as dialog is already located in the correct directory.

我知道这是可能改变当前目录中的Excel实例与VBA.FileSystem.ChDir和VBA code /宏这样一个Excel.Application实例中这样做:

I know it's possible to change the current directory in Excel Instance with VBA.FileSystem.ChDir and doing it within the Excel.Application instance with VBA Code/Macro like this:

  1. 从创建C#.NET中的Excel US preadsheet

  1. Create an Excel Spreadsheet from C# .NET

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Workbooks.Add();
excel.Visible = true;  

  • 在Excel的小号preadsheet是开放的,按下Alt + F11。然后创建并运行下面的宏

  • When Excel spreadsheet is open, hit ALT + F11. Then create and run following macro

    Sub SetChdir()
       Call FileSystem.ChDir("C:\To\My\Custom\Directory\")
    End Sub
    

  • 当点击保存Excel中的当前目录设置路径设置CHDIR

  • When clicking Save in Excel the Current Directory is set path set with ChDir

    我想不通,甚至如果可能的话,如何创建一个从.NET这种行为。我累了瞎搞与Excel.DefaultFilePath该Excel使用启动时设置当前目录。但是它有两个问题:

    I can't figure out, or even if it's possible, how to create this behaviour from .NET. I tired messing around with Excel.DefaultFilePath which Excel uses on startup to set the Current Directory. But it has two problems:

    • 在当前DefaultFilePath设置为不同的值,则需要重新启动Excel,使之在新的或开放源代码preadsheets影响
    • 我真的不希望改变用户当前DefaultFilePath因为它是Excel中的一个全局值,而不是这种的各个S preadsheet。

    推荐答案

    还有另一种方式。

    excel.ExecuteExcel4Macro("DIRECTORY(\"C:\\To\\My\\Custom\\Directory\")");
    

    这篇关于通过.NET办公室PIA坐落在Excel.Application当前目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆