Open Excel Document on Button Click

Discussion in 'C#' started by seanr, Sep 24, 2011.

  1. seanr

    seanr New Member

    Joined:
    Sep 24, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have read many different tutorials on the internet, but none of them have been exactly what I am looking for. Some have read an excel file and displayed the contents of different cells inside a messagebox. Others have just simply opened a filediaglog box for the user to select the file.

    What I want is for the Excel program to open and display a particular workbook so that the user can print it out. Please note that I will distribute this program to friends and family and the excel file will be put into the same folder as the program. The reason I say this is that there Windows Account Username will be different from mine, and so the file path couldn't simply be C:\Users\Sean Ramuchak\Desktop...etc.

    If I was not clear in explaining my issue in anyway then please reply below or send me an email at stramuchak@gmail.com
     
  2. masterwarner

    masterwarner New Member

    Joined:
    Nov 7, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    What you can do is start the process of Excel problematically. I am not sure but you dont need to open excel to print a document. Using API, this can achieved.
     
  3. nisha134

    nisha134 Banned

    Joined:
    Sep 7, 2011
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Open the window application form and then open the project and choose the open add references ,select microsoft 12.0 object library in your project and then code in code behind file .........
    Code:
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel; 
    
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Excel.Application xlApp ;
                Excel.Workbook xlWorkBook ;
                Excel.Worksheet xlWorkSheet ;
                object misValue = System.Reflection.Missing.Value;
    
                xlApp = new Excel.ApplicationClass();
                xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 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);
    
                MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString());
    
                xlWorkBook.Close(true, misValue, misValue);
                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();
                }
            } 
        }
    }
     
    Last edited by a moderator: Nov 9, 2011

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice