ani
ani

Reputation: 121

How to write a text inside a rectangle in excel using Microsoft.Office.Interop.Excel in c#

Here is the sample code..

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;


//Rectangle shape
xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 47, 280, 140, 90);

//using this below code i can write the text but the text is showing with special effects.
xlWorkSheet.Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, "simple text", "Arial", 14, MsoTriState.msoTrue, MsoTriState.msoFalse, 67, 320);

I need plain text with bold effect and size(16) ... i am using Visual studio 2008

Upvotes: 2

Views: 3490

Answers (2)

chenz101
chenz101

Reputation: 85

i just use textbox.

Excel.Shape textbox = xlShapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 20, 20, 150, 20);
            textbox.TextFrame.Characters(missing, missing).Text = "Hey";

Upvotes: 2

Doug Glancy
Doug Glancy

Reputation: 27478

I'm not a C# coder, but this compiles and meets your needs, I believe. I tested it in C# 2010 and Excel 2010:

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Shape shp;

shp = xlWorkSheet.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeRectangle, 47, 280, 140, 90);
shp.Fill.Visible = Office.MsoTriState.msoFalse;
shp.TextFrame2.TextRange.Font.Bold = Office.MsoTriState.msoTrue;
shp.TextFrame2.TextRange.Font.Name = "Arial";
shp.TextFrame2.TextRange.Font.Size = 16;
shp.TextFrame2.TextRange.Font.Fill.Visible = Office.MsoTriState.msoTrue;
shp.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = (int)Excel.XlRgbColor.rgbBlack;
shp.TextFrame2.TextRange.Characters.Text = "Test";

Upvotes: 3

Related Questions