Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Open Excel Document on Button Click (http://www.go4expert.com/forums/excel-document-button-click-t26784/)

seanr 24Sep2011 23:53

Open Excel Document on Button Click
 
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

masterwarner 8Nov2011 02:16

Re: Open Excel Document on Button Click
 
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.

nisha134 9Nov2011 13:30

Re: Open Excel Document on Button Click
 
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();
            }
        }
    }
}



All times are GMT +5.5. The time now is 11:27.