Create Excel Add-in - get cell value
I am creating a excel Add-in using visual studio 2010. I was able to get the cell address using this code.
label1.Label = Globals.MyAddIn.Applica开发者_如何学运维tion.ActiveCell.Address.ToString();
I want to get the cell value. Also if you can tell me how to set a value for a given cell.
Please help.
to get a cell value for the active cell the following should do it
var cellValue = Globals.MyAddIn.Application.ActiveCell.Value.ToString()
or for a specific cell
var cellValue = Globals.MyAddIn.Application.Cells("A1").Value.ToString()
To Set the value it is basically the reverse
Globals.MyAddIn.Application.Cells("A1").Value = "ABC123"
EDIT
Try this. I know that this works as I have something like it working in my own addin.
int row = 1;
int col = 1;
var sheet1 = (Excel.Worksheet)Application.ActiveWorkbook.Worksheets["Sheet1"];
sheet1.Cells[row, col] = "ABC123";
string cellValue = sheet1.Cells[row,col];
精彩评论