Simple C# Excel Add-In Ribbon Button to Color Cells
First, a big thank you to anyone that read this question and can help.
I'm sing VS2010 and I've created a Excel 2007 Add-In Project using the wizard, then I added a new folder to my project called Ribbon, and inside it created a New Item which is a Ribbon (Visual Designer).
From here I've added a new group to the ribbon and a new button. I double clicked on the new button and am presented with this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Xml.Linq;
namespace UploadFCStats.Ribbon
{
public partial class FCRibbon
{
private void FCRibbon_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
}
}
}
Now all I want to do is color some cells, or store some values in variables, and 开发者_开发知识库then create a connection to a SQL server and use an insert statement to upload some data.
However I can't seem to access any of the typical Excel properties. Now I am new at C# and Excel Add-In's but have a great understanding of C++, Java, and Excel. Nothing I've googled has helped me in this instance.
My best guess is that I'm missing a library, but after some fiddling that didn't seem to solve my problem. Using this button, how do I grab values from the worksheet? How do I color cells? How do I access formula functions inside of excel?
Thank you!
Read some basics about creating an Excel 2007 Add-In and start with Globals.ThisAddIn.Application
You have to access Range object (i.e. Cells, Rows, Columns) and then change background or anything you want to change.
var range = ((Range)Globals.ThisAddin.Application.Cells[1,1]).Resize[1,5];
range.Interior.Color = (int)XlRgbColor.rgbGreen;
Casting to Range is unnecessary but Cells[1,1] is type of dynamic. When I cast it to Range intelisence tells me what properties Range has.
Application.Cells[1,1] selects Cell A1, Resize[1,5] resizes range to all cells between A1 and E1 including these.
精彩评论