Open and print Excel document programmatically using late binding technique


This article is useful for Open and print Excel document programmatically using late binding technique

Opening and using an assembly during runtime is called late binding. This is very help full to play with multiple versions of office application. For example if want to print a excel document from the windows application, normally we add a reference of the excel dlls or PIA to our application, but the same thing will fail if the target machine has the older versions of the dlls. To avoid this version problem we can use the late binding logic, because here we are not referring any dlls during the development time, we are referring only at the runtime with the program id.

After creating the instance of the excel application, we can use the reflection concept to invoke its methods.

I have done a sample late binding code below. Here I am opening, printing and closing the excel document.


The following class is used to open,print and close the Excel document


public class MSExcel
{
Type ExcelType;
object ExcelApplication;
public object oBook;

public MSExcel()
{
//Gets the type of the Excel application using prorame id
ExcelType = Type.GetTypeFromProgID("Excel.Application");

//Creating Excel application instance from the type
//Check the running processes using alt+ctrl+del
ExcelApplication = Activator.CreateInstance(ExcelType);
}
public void Open(string strFileName)
{
object fileName = strFileName;
object readOnly = true;
object missing = System.Reflection.Missing.Value;
object[] oParams = new object[1];

//Getting the WoorkBook collection [work Sheet collection]
object oDocs = ExcelApplication.GetType().InvokeMember("Workbooks",
System.Reflection.BindingFlags.GetProperty,
null,
ExcelApplication,
null, CultureInfo.InvariantCulture);
oParams = new object[3];
oParams[0] = fileName;
oParams[1] = missing;
oParams[2] = readOnly;

//Open the first work sheet
oBook = oDocs.GetType().InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod,
null,
oDocs,
oParams, CultureInfo.InvariantCulture);

}
public void Close()
{
//Closing the work sheet
oBook.GetType().InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
null,
oBook,
null, CultureInfo.InvariantCulture);
}
public void Print()
{
//Printing the sheet
oBook.GetType().InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod,
null,
oBook,
null, CultureInfo.InvariantCulture);
}
public void Quit()
{
//Close the Excel application block
//Check the running processes using alt+ctrl+del
ExcelApplication.GetType().InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,
null,
ExcelApplication,
null, CultureInfo.InvariantCulture);
}
}


Testing the MSExcel class


Public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}



private void Form1_Load(object sender, EventArgs e)
{
MSExcel excel = new MSExcel();

//Note: Give the vaild path
excel.Open("C:\\TestBook.xls");
excel.Print();
excel.Close();
excel.Quit();
}
}


Comments

Guest Author: Prabakaran04 Jul 2013

I used your code to Print Excel Sheets ,it's work Well but it print the all excel sheets from Open Excel workbooks, I need to Print Particular Sheets Only How can i do that Please help me..



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: