Home Functions Commands

ESharper ESharper

Command examples and sample code

1. Set values for the current cell and cells below.

Cells below

using ExcelDna.Integration;

public class C : ESharperExt.ICommand
{
	public void Run()
	{
		var current = XlCall.Excel(XlCall.xlfActiveCell) as ExcelReference;
		for (int i = 1; i <= 5; ++i)
		{
			current.SetValue("Value " + i);
			current = current.Offset(1, 0);
		}
	}
}

public static class Utilities
{
	public static ExcelReference Offset(this ExcelReference range, int rows, int cols)
	{
		return new ExcelReference(range.RowFirst + rows, range.RowLast + rows, 
			range.ColumnFirst + cols, range.ColumnLast + cols, range.SheetId);
	}
}

2. Set values for cells in the selection.

Cells selection

using ExcelDna.Integration;

public class C : ESharperExt.ICommand
{
	public void Run()
	{
		var selection = XlCall.Excel(XlCall.xlfSelection) as ExcelReference;
		for (int row = selection.RowFirst; row <= selection.RowLast; ++row)
			for (int column = selection.ColumnFirst; column <= selection.ColumnLast; ++column)
			{
				var cell = new ExcelReference(row, column);
				cell.SetValue("Value " + (row + 1) + "," + (column + 1));
			}
	}
}

3. Using Microsoft.Office.Interop.Excel.

Interop

public class C : ESharperExt.ICommand
{
	public void Run()
	{
        Microsoft.Office.Interop.Excel.Application app = ExcelDna.Integration.ExcelDnaUtil.Application as
            Microsoft.Office.Interop.Excel.Application;
        System.Windows.MessageBox.Show(app.Name + " " + app.Version);
	}
}