本文介绍了使用Process.Start在C#中打开Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过单击按钮打开Excel文件.由于某种原因,它无法正常工作.我已经尝试了几件事.有什么想法为什么他们不起作用?

I'm trying to open an excel file using a button click. And for some reason it's not working. I've tried several things. Any ideas why they are not working?

我尝试过的方法1.这将打开文件管理器,但不会打开正确的文件.绝对是使用了正确的文件路径,并且文件确实存在

Method 1 I have tried. This opens the file manager but does not open the proper file. It is definitely using the proper path to the file and the file does exist

private string fileCopy;

public RepairResultsControl()
{
    InitializeComponent();
}

public void Show(PSRepair.AnalysisResults analysis, string pathNameCopy)
{
    fileCopy = pathNameCopy;
    Show();
}

private void btnGoToFile_Click(object sender, EventArgs e)
{
    Process.Start("explorer.exe", "/select,"+ fileCopy);
}

方法2.这只是没有打开任何内容,不确定为什么

Method 2. This just didn't open anything not sure why

System.Diagnostics.Process.Start(@"C:\Users\username\Documents\newTest.xlsx");

推荐答案

通常, Process.Start(@"C:\ Users \ username \ Documents \ newTest.xlsx"); 将打开您的Excel中的文档.

Normally, Process.Start(@"C:\Users\username\Documents\newTest.xlsx"); would open your document in Excel.

但是,您在注释中表示您正在通过后台运行的Excel加载项执行此操作.解决方案需要考虑到这一点(代码示例假定您具有VSTO加载项,否则需要进行相应调整):

However, you say in a comment that you are doing this from an Excel add-in which runs in the background. The solution needs to take this into account (the code sample assumes that you have a VSTO add-in, otherwise you need to adjust accordingly):

// make the running Excel instance visible
Globals.ThisAddIn.Application.Visible = true;

// open the workbook using Excel interop
Globals.ThisAddIn.Application.Workbooks.Open(fileName);

这篇关于使用Process.Start在C#中打开Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:16