Click here to Skip to main content
15,313,682 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is it possible to open excel directly inside Window form C# and edit it the way we do in MS Office excel
.

I mean I need entire feature set of excel within a windows form?

I have tried below code:

What I have tried:

C#
public void loadDocument(string fileName)
        {

            del = new ConvertDocumentDelegate(ConvertExcel);
            // Call DocumentConversionComplete when the method has completed. 
            del.BeginInvoke(fileName, DocumentConversionComplete, null);
        }

  
        void ConvertExcel(string fileName)
        {        

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            excel.Visible = true;
            excel.EditDirectlyInCell = true;
            Microsoft.Office.Interop.Excel.Workbook xlWorkbook = excel.Workbooks.Open(fileName);
            Microsoft.Office.Interop.Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1];

            tempFileName = GetTempFile("html");
            object missing = System.Reflection.Missing.Value;
            object newFileName = (object)tempFileName;
            object fileType = (object)Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;

            xlWorkbook.SaveAs(fileName, fileType, missing, missing, missing, missing,
                 Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                 missing, missing, missing, missing, missing);
        }

        void DocumentConversionComplete(IAsyncResult result)
        {
            // navigate to our temp file. 
            wbPreview.Navigate(tempFileName);
        }

        void webBrowser1_DocumentCompleted(object sender,
            WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
                if (tempFileName != string.Empty)
                {
                    // delete the temp file we created. 
                    File.Delete(tempFileName);

                    // set the tempFileName to an empty string. 
                    tempFileName = string.Empty;
                }
            }
            catch (Exception ex)
            {
            }
        }

        string GetTempFile(string extension)
        {
            // Uses the Combine, GetTempPath, ChangeExtension, 
            // and GetRandomFile methods of Path to 
            // create a temp file of the extension we're looking for. 
            return Path.Combine(Path.GetTempPath(),
                Path.ChangeExtension(Path.GetRandomFileName(), extension));
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

            loadDocument(@"C:\Project\TestDescription\Myfile.xml");
        }
Posted
Updated 28-Jan-21 0:36am
v2

   
Comments
Maciej Los 12-Jan-21 4:12am
   
5ed!
Below is my solution


set BrowserFlags = 0 at registry location:
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.Sheet.12


restart the machine and write below code in WPF

WebBrowser Control in wpf

<WebBrowser  Name="webBrowser1" HorizontalAlignment="Left"  Margin="10,10,0,0" VerticalAlignment="Top" />


using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Webbrowser
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            string mySheet = @"C:\Book 2.xlsx";          
            webBrowser1.Navigate(mySheet);
            webBrowser1.LoadCompleted += WebBrowser1_LoadCompleted;
          
        }

        private void WebBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
        {
            Thread t = new Thread(new ThreadStart(RunThread));
            t.Start();
            

        }

        public void RunThread()
        {
            var pComWebBrowser = typeof(WebBrowser).GetProperty("AxIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
            var objComWebBrowser = pComWebBrowser.GetValue(webBrowser1);
            const int OLECMDID_HIDETOOLBARS = 24;
            const int OLECMDEXECOPT_DONTPROMPTUSER = 2;
            ((dynamic)objComWebBrowser).ExecWB(OLECMDID_HIDETOOLBARS, OLECMDEXECOPT_DONTPROMPTUSER, 16);
        }


    }
}
   
v3

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