如何在C#windows窗体中打开excel文件 [英] How to open an excel file in C# windows form

查看:345
本文介绍了如何在C#windows窗体中打开excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喂!

我正在尝试使用以下代码打开excel文件,该代码写在我在项目中创建的类库中。



 string mySheet = @C:\ Users \Tal \Desktop\DataBase.xlsx; 

var excelApp = new Excel.Application();

excelApp.Visible = true;


工作簿wb = excelApp.Workbooks.Open(mySheet);
工作表ws =(工作表)wb.Worksheets [1];
Excel.Range xlRange = ws.UsedRange;





当我尝试运行程序时,它会在尝试执行时停止以下命令行:

工作簿wb = excelApp.Workbooks.Open(mySheet);



它说无法找到excel文件,并要求我检查我插入的文件的位置是否正确写入。我确定文件位置是正确的,因为当我试图在控制台应用程序上运行相同的程序时,一切正常。



如果有人知道如何使这个程序在windows窗体上工作,如果他能帮助我这样做,我会很高兴。



谢谢,Tal。





再次,

我有一些与我的问题相关的更新,我想填写你的关于。


显然我告诉你的问题甚至比我想象的要大。我发现该程序无法正常工作,只能在我的计算机上运行。现在,它甚至没有像以前一样在控制台应用程序上工作。在两个平台(控制台应用程序和Windows窗体)中,程序停止,并显示相同的错误消息。

但是,当我尝试在另一台计算机上运行程序时,它的工作正常,并显示Excel文件。我应该说,两台计算机之间有三个基本的区别,我认为可能会解决这个问题。

Firt,在我的计算机上(代码无法编译的那台)windows 8用作操作系统,在其他计算机上安装Windows 7.

两台计算机之间的第二个区别是我的计算机上有Excel 2010,另一台是2007版本。

为了使用Excel,我需要添加Microsoft Excel Object Libary引用。在我的电脑上它是14.0版本,而在另一台计算机上它是12.0版本。

如果您认为问题与我上面提到的差异有关,请告诉我,以及如果没有,我会很高兴听到你可能有的任何其他建议,这可以帮助我解决这个问题。



再次感谢Tal。

解决方案

看看这里: Workbooks.Open方法 [ ^ ]。



有几个输入参数是强制性的。您可以通过遗失字段 [ ^ ]实际值的实例;)


  object  objOpt = Type.Missing; 
.excelApp = new Excel.Application();
.wbclass = excelApp.Workbooks.Open(
filePath,
objOpt,
true
objOpt,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt,
objOpt);


this.openFileDialog1.FileName =* .xls;



DialogResult dr = this.openFileDialog1.ShowDialog();

if(dr == System.Windows.Forms.DialogResult.OK)

{

// string mySheet = @C:\ Users \Tal \Desktop\DataBase.xlsx;

string mySheet = openFileDialog1.FileName;



var excelApp = new Microsoft.Office.Interop.Excel.Application();



excelApp.Visible = true;





工作簿wb = excelApp.Workbooks.Open(mySheet);

工作表ws =(工作表)wb.Worksheets [1];

Microsoft.Office.Interop.Excel.Range xlRange = ws.UsedRange;

}

Hallo!
I am trying to open an excel file using the following code which is written in a class library that I created in my project.

string mySheet = @"‪C:\Users\Tal\Desktop\DataBase.xlsx";

           var excelApp = new Excel.Application();
       
           excelApp.Visible = true;

          
           Workbook wb = excelApp.Workbooks.Open(mySheet);
           Worksheet ws = (Worksheet)wb.Worksheets[1];
           Excel.Range xlRange = ws.UsedRange;



When I try to run the program it stops when it trys to execute the following command line:
" Workbook wb = excelApp.Workbooks.Open(mySheet);"

It says it can not find the excel file and asks me to check if the location of the file that I inserted is write properly. I know for sure that the file location is right because when I'm trying to run the same program on the console application it all works just fine.

If anyone knows how to make this program to work on the windows form too, I would be very happy if he could help me to do so.

Thanks, Tal.


Hi again,
I have some updates related to my problem that I wanted to fill you in about.

Apparently the problem I told you about is even bigger than I thought. I found out that the program doesn't work properly but only on my computer. Now, it's not even working on the console application as it was before. In both platforms(console application and windows form) the program stops, and the same error message appears.
However, when I try to run the program on a different computer it work's exactly as it should and the Excel file is displayed. I should of say though, that there are three basic differences between the two computers which I think might couse this problem.
Firt, on my computer (the one where the code doesn't compile) windows 8 is used as the operating system and on the other computer windows 7 is installed.
The second difference between the two computers is that on my computer I have the Excel 2010 and on the other one the 2007 version.
In order to work with Excel, I need to add the Microsoft Excel Object Libary refrence. On my computer it is the 14.0 version, while on the other computer it is the 12.0 version.
Please let me know if you think that the problem is related to one of the differences I mentioned above, and if not, I would be very happy to hear for any other suggestions you might have, that could help me solve this problem.

Thanks again, Tal.

解决方案

Have a look here: Workbooks.Open method[^].

There are several input parameters which are obligatory. You can pass Missing Field[^] instaed of real values ;)


object objOpt = Type.Missing;
this.excelApp = new Excel.Application();
this.wbclass = excelApp.Workbooks.Open(
                                                filePath,
                                                objOpt,
                                                true,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt,
                                                objOpt);


this.openFileDialog1.FileName = "*.xls";

DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
//string mySheet = @"‪C:\Users\Tal\Desktop\DataBase.xlsx";
string mySheet = openFileDialog1.FileName;

var excelApp = new Microsoft.Office.Interop.Excel.Application();

excelApp.Visible = true;


Workbook wb = excelApp.Workbooks.Open(mySheet);
Worksheet ws = (Worksheet)wb.Worksheets[1];
Microsoft.Office.Interop.Excel.Range xlRange = ws.UsedRange;
}


这篇关于如何在C#windows窗体中打开excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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