Home > MS Office > Excel Application Handling through C#

Excel Application Handling through C#

September 5th, 2009 admin Leave a comment Go to comments

// Required Libraries
using Microsoft.Office.Interop;
using Excel = Microsoft.Office.Interop.Excel;

//New application is created and is shown to the user
Excel.Application excelApp = new Excel.ApplicationClass();
excelApp.Visible = true;

// Blank workbook object is created
Excel.Workbook excelWorkbook = excelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);

// Existing Workbook object is opened for editing. To be used instead of the upper snippet of code
string workbookPath = "c:\\SomeWorkBook.xls";
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,0, false, 5, "", "",
false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);

// Definition of current sheet from the workbook's collection of worksheets
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
String currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
//        Alternatively Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelWorkbook.ActiveSheet;

// Definition of the Range of cells to be edited
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1");

// Paste procedure from windows clipboard to current worksheet
excelWorksheet.Paste(excelCell, false);

// Safe quiting procedure
excelApp.Quit();

//        Copy value from one Range to another
((Excel.Range)excelWorksheetDestination.get_Range("A1", "DC1000")).Value2 = ((Excel.Range)excelWorksheetSource.get_Range("A1", "DC1000")).Value2;
Categories: MS Office Tags: ,
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.