Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);
Posted
Updated 4-Apr-19 17:40pm

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[^]
 
Share this answer
 
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.
 
Share this answer
 
v4
Comments
Kiran_Desai 2-Apr-19 8:08am    
I'm using C# WinForms and I want to display the content of excel file in one panel.
[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.
 
Share this answer
 
Comments
Member 13334277 30-Sep-21 6:28am    
This solution is working , excel opened in panel but menu ribbon is not visible what can i do for this please suggest me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900