Home > Backend Development > C#.Net Tutorial > Detailed explanation of the method summary of opening Excel documents in C# winform

Detailed explanation of the method summary of opening Excel documents in C# winform

黄舟
Release: 2017-03-25 13:22:29
Original
2170 people have browsed it

下面小编就为大家带来一篇C# winform打开Excel文档的方法总结(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

C#打开Excel文档方法一:调用Excel的COM组件

在项目中打开Add Reference对话框,选择COM栏,之后在COM列表中找到"Microsoft Excel 11.0 Object Library"(Office 2003),然后将其加入到项目的References中即可。Visual C#.NET会自动产生相应的.NET组件文件,以后即可正常使用。

按钮的点击事件如下:

privatevoid button1_Click(object sender, EventArgs e) 
{ 
 Excel.Application excel = 
 new Excel.Application(); //引用Excel对象 
 Excel.Workbook book = 
 excel.Application.Workbooks.Add("c:\\test.xls"); 
 //引用Excel工作簿 
 excel.Visible = true; //使Excel可视 
}
Copy after login

C#打开Excel文档方法二:利用命令行

我们在开始→运行里输入c:\\test.xls,后如果电脑安装有Excel的话他也会自动的打开这个文件,代码如下:

privatevoid button1_Click(object sender, EventArgs e) 
{ 
 System.Diagnostics.Process.Start("c:\\test.xls"); 
}
Copy after login

C#打开Excel文档的具体方法的介绍就向你介绍到这里,希望对你了解和学习C#打开Excel文档有所帮助。

我的一点看法:

private void button1_Click(object sender, EventArgs e)    

{      

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();      

Workbook wkb = app.Workbooks.Add(@"E:\Work\DemoBase_091111\CSharpOpenExcel\cshapropenexcel.xlsx"); 

//打开已经存在的excel,      

//而通过gdal打开已经存在的shp,方法为      

// OSGeo.GDAL.Gdal.AllRegister();       

// OSGeo.GDAL.Dataset dataSet = OSGeo.GDAL.Gdal.Open(@"E:\Work\DemoBase_091111\GDALTEST\testshapefile\point_out.shp", Access.GA_ReadOnly);       

//二者是差不多的,一个用add方法,一个用open方法,都得到了可以进行下一步操作的文件,技术具有相同性     

  app.Visible = true;    

}
Copy after login

The above is the detailed content of Detailed explanation of the method summary of opening Excel documents in C# winform. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template