如何在不使用Web浏览器的情况下将Excel集成到C#windows应用程序中? [英] How to integrate excel in C# windows application without using web browser?

查看:80
本文介绍了如何在不使用Web浏览器的情况下将Excel集成到C#windows应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必需的方案是

1. Excel应该在应用程序内打开。
2. Excel有一些公式/微观,应该按预期工作。
3.行实用地添加并从excel中删除。





我尝试过:



 [DllImport(user32.dll)] 
private static extern IntPtr SetParent(IntPtr hWndChild,IntPtr hWndNewParent);





 excelApp = new Microsoft.Office.Interop.Excel.Application(); 
流程p = Process.Start(excel.exe);
Thread.Sleep(1000); //允许进程打开它的窗口
SetParent(p.MainWindowHandle,this.panel1.Handle);

解决方案

有几个库可以帮助你。



一个是Microsoft.Office.Interop库。这个库的缺点是它需要在它正在执行的同一台机器上安装Excel。



另一种选择是OpenXML。它更难使用,但可以在任何地方使用。



我将建议OpenXML的一个分支:ClosedXML。



NuGet Gallery | ClosedXML 0.94.2 [ ^ ]

如果您想在应用程序中使用电子表格,可以使用几个库,但如果您希望保证与Excel公式和宏的兼容性,则必须花钱。 Telerik,Infragistics,DevExpress和SpreadsheetGear都是可行的选择,但它们都需要花钱。



还有其他商业项目,所以我建议使用google来发现所有你的选择。



你也没有指定你的平台(WinForms或WPF),但我强烈重新推荐WPF,因为数据绑定要简单得多。

 [DllImport(user32.dll)] 
static extern IntPtr SetParent(IntPtr hWndChild,IntPtr hWndNewParent);



Microsoft.Office.Interop.Excel.Application excelApp;
工作簿excelWorkBook;
工作表excelWorkSheet;

//在按钮点击事件的面板控件内打开Excel文件。

private void btnLoadExcelParent_Click(object sender,EventArgs e)
{
excelApp = new Microsoft.Office.Interop.Excel.Application();

excelApp.Visible = true;
excelApp.ScreenUpdating = true;
excelApp.EnableAutoComplete = false;
object misValue = System.Reflection.Missing.Value;
excelWorkBook = excelApp.Workbooks.Add(misValue);
IntPtr excelHwnd = new IntPtr(excelApp.Application.Hwnd);
SetParent(excelHwnd,panel1.Handle);

}







我实现了这个功能上面的代码。


Required scenarios are

1. Excel should be open inside application.
2. Excel has some formulas/micro, that should work as expected.
3. Rows are pragmatically added and removed from excel.



What I have tried:

[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);



excelApp = new Microsoft.Office.Interop.Excel.Application();
Process p = Process.Start("excel.exe");
Thread.Sleep(1000); // Allow the process to open it's window
SetParent(p.MainWindowHandle, this.panel1.Handle);

解决方案

There are a couple of libraries that can help you.

One is the Microsoft.Office.Interop library. The weakness of this library is that it requires Excel installed on the same machine it's executing on.

Another choice is OpenXML. It's harder to use, but will work anywhere.

I'm going to suggest an offshoot of OpenXML: ClosedXML.

NuGet Gallery | ClosedXML 0.94.2[^]


If you want a spreadsheet in your app, there are a couple of libraries you can use, but if you want guaranteed compatibility with Excel formulas and macros, you'll have to spend money. Telerik, Infragistics, DevExpress, and SpreadsheetGear, are all viable choices, but they all cost money.

There are other conmmercial items available, so I recommend using google to discover all of your alternatives.

You also didn't specify your plaform (WinForms or WPF), but I strongly recoommend WPF because data binding is much simpler.


[DllImport("user32.dll")]
   static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);



   Microsoft.Office.Interop.Excel.Application excelApp;
   Workbook excelWorkBook;
   Worksheet excelWorkSheet;

  // Excel file opened inside panel control on button click event.

   private void btnLoadExcelParent_Click(object sender, EventArgs e)
   {
       excelApp = new Microsoft.Office.Interop.Excel.Application();

       excelApp.Visible = true;
       excelApp.ScreenUpdating = true;
       excelApp.EnableAutoComplete = false;
       object misValue = System.Reflection.Missing.Value;
       excelWorkBook = excelApp.Workbooks.Add(misValue);
       IntPtr excelHwnd = new IntPtr(excelApp.Application.Hwnd);
       SetParent(excelHwnd, panel1.Handle);

   }




I achieve this functionality with the above code.


这篇关于如何在不使用Web浏览器的情况下将Excel集成到C#windows应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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