I have created a shape object in excel using C# and I want the text in a cell to appear in the shape. When I recorded the macro in excel, I got the below formula
ActiveSheet.Shapes.Range(Array("Rectangle1")).Select
Selection.Formula = "=$D$21"
Can anyone help me on how to reproduce the above code in C#? Below is the C# code that controls excel
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
namespace WindowsFormsApplication2
{
public partial class ExcelTest : Form
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
public ExcelTest()
{
Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Open("F:\\YourExcelSheet.xlsx");
Microsoft.Office.Interop.Excel.Worksheet ws = excel.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
excel.Visible = true;
Range MyCells = ws.Cells;
MyCells.Item[1, "A"] = "Test";
Microsoft.Office.Interop.Excel.Shape shp = ws.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 30, 100, 300, 100);
shp.Name = "Shape1";
shp.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = System.Drawing.Color.Gray.ToArgb();
shp.Fill.ForeColor.RGB = System.Drawing.Color.White.ToArgb();
shp.TextEffect.Text = Convert.ToString(MyCells.Item[1, "A"].Value);
//InitializeComponent();
}
}
}