Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,951 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
How are you?
I'm developing with C#.
Create an Excel application of Microsoft.Office.Interop.Excel using Panel in C# Winform and connect it to Panel
C# I have configured a program to control Excel within the program.
During development, a specific Office version (Office 2019 Pro 1808 version) caused 0xC0000005 error when changing the cell value and confirmed that the Excel process was terminated.
I tried both the old version and the update, but in that version, the process ends.
Is there a way to solve that problem?
Below is a simple implementation of the program.
Thank you.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using System.Threading;
using System.Diagnostics;
using System.Security.Principal;
namespace ExcelPanelIntegration
{
public partial class Form1 : Form
{
private Excel.Application excelApp;
private Excel.Workbook workbook;
private Excel.Worksheet worksheet;
private Panel excelPanel;
[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
private const int SWP_NOZORDER = 0x4;
private const int SWP_NOACTIVATE = 0x10;
public Form1()
{
InitializeComponent();
excelPanel = new Panel();
excelPanel.Dock = DockStyle.Fill;
this.Controls.Add(excelPanel);
InitializeExcel();
}
private void InitializeExcel()
{
try
{
excelApp = new Excel.Application();
workbook = excelApp.Workbooks.Add();
worksheet = workbook.ActiveSheet;
// Set Excel window as a child of our panel
SetParent(new IntPtr(excelApp.Hwnd), excelPanel.Handle);
// Resize Excel window to fit the panel
SetWindowPos(new IntPtr(excelApp.Hwnd), IntPtr.Zero, 0, 0, excelPanel.Width, excelPanel.Height, SWP_NOZORDER | SWP_NOACTIVATE);
// Make Excel visible
excelApp.Visible = true;
// Disable Excel's standard error handling
excelApp.DisplayAlerts = false;
}
catch (COMException comEx)
{
MessageBox.Show($"COM Exception during Excel initialization: {comEx.Message}\nError Code: {comEx.ErrorCode:X}");
}
catch (Exception ex)
{
MessageBox.Show($"Error during Excel initialization: {ex.Message}");
}
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
CleanupExcel();
}
private void CleanupExcel()
{
try
{
if (worksheet != null)
{
Marshal.ReleaseComObject(worksheet);
worksheet = null;
}
if (workbook != null)
{
workbook.Close(false);
Marshal.ReleaseComObject(workbook);
workbook = null;
}
if (excelApp != null)
{
excelApp.Quit();
Marshal.ReleaseComObject(excelApp);
excelApp = null;
}
}
catch (Exception ex)
{
MessageBox.Show($"Error during Excel cleanup: {ex.Message}");
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
}