1

Say a new Excel file is opened and data is entered into it. Could an external C# application hook onto the Excel process and get the data from it? Also, the Excel file is never saved into a file.

Would this be difficult to do and also could you point me in the right direction on how to achieve this.

John
  • 941
  • 2
  • 12
  • 20
  • You want a C# app that reads what Excel has stored in memory for a particular file? Sounds like a pain, if it's even possible to do. – Broots Waymb Oct 04 '16 at 19:28
  • No,the application security models around most OSs wouldn't allow that! – Illuminati Oct 04 '16 at 19:30
  • http://stackoverflow.com/questions/3746409/how-to-read-some-data-from-a-windows-application-memory maybe helpful – Hozikimaru Oct 04 '16 at 19:30
  • If you are in control of the receiving app, I'd recommend creating an AddIn for Excel and post/push from Excel instead of pulling. It will make things way easier and you'll be able to use a supported API. – Filburt Oct 04 '16 at 19:46

1 Answers1

0

Just add this code in the code behind of a windows form application that has a button named "button1" added on it. This sample code will iterate through cells of the Excel file and display the content of each cell in a Message Box. Of course a project reference to "Microsoft.Office.Interop.Excel" is required.

And don't forget to actually create the excel file, in this example I used this path "C:\ExcelData\DataToImp.xlsx".

using Excel = Microsoft.Office.Interop.Excel;


    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        Excel.Range range;

        string str;
        int rCnt = 0;
        int cCnt = 0;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open("C:\\ExcelData\\DataToImp.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        range = xlWorkSheet.UsedRange;

        for (int rowIndex = 1; rowIndex <= range.Rows.Count; rowIndex++)
        {
            for (int columnIndex = 1; columnIndex <= range.Columns.Count; columnIndex++)
            {
                Excel.Range rangeTest = range.Cells[rowIndex, columnIndex] as Excel.Range;

                if (rangeTest.MergeCells)// && rangeTest.Value2 == null)
                {
                    //continue;

                    int columns = rangeTest.Columns.Count;
                    columnIndex += columns;
                }

                MessageBox.Show("m " + (string)rangeTest.Value2);

            }
        }

        xlWorkBook.Close(true, null, null);
        xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);
    }

    private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Unable to release the Object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }
Clock
  • 974
  • 3
  • 17
  • 35
  • 1
    This was my first though too. The caveat for OP is that no, you can't take an already open Excel and "hook in" to it this way, you have to run your program first which itself opens Excel and from there you will be able to read whatever people type in to it. – Quantic Oct 04 '16 at 20:38
  • 1
    Yeah, that is the way to do it, also the file has to be "saved" on disk after some data modifications are made in Excel. – Clock Oct 04 '16 at 20:47
  • 1
    Also there is no need for the application to "open" Excel, it just reads the file. There is no need to have the Excel open for this to work. – Clock Oct 04 '16 at 20:49
  • 1
    But how will you determine the file currently opened in Excel? What if multiple workbooks are opened? – Filburt Oct 05 '16 at 08:07