如何在excel中使用Microsoft.Office.Interop.Excel在c#中编写矩形内的文本

本文关键字:文本 Interop excel Microsoft Office Excel | 更新日期: 2023-09-27 18:19:25

示例代码如下:

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);

我需要纯文本粗体效果和大小(16)…我使用的是Visual studio 2008

如何在excel中使用Microsoft.Office.Interop.Excel在c#中编写矩形内的文本

我不是c#程序员,但我相信这可以编译并满足您的需求。我在c# 2010和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";

我只是使用文本框。

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