如何将文本添加到C#Excel Interop中的现有形状(文本框)
我有一个excel包含文本框/形状,我将填写具体的数据。 我使用下面的代码来识别每个形状:
//using Excel = Microsoft.Office.Interop.Excel; Excel.Worksheet xlWorkSheet foreach(Excel.Shape shp in xlworksheet.Shapes) { //code to add text to shape goes here.... }
我也试过使用:
shp.TextFrame2.TextRange.Characters.Text = "Test";
和
shp.TextFrame.Characters(Type.Missing, Type.Missing).Text = "Test";
但给出了一个错误状态The specified value is out of range
和Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))
Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))
,分别。
将文本添加到现有的文本框应该是什么正确的方法?
在设置文本之前,您必须检查Shape
的types是否是msoTextBox
。
Excel.Worksheet xlWorkSheet foreach (Excel.Shape shp in xlworksheet.Shapes) { if (shp.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox) { shp.TextFrame.Characters(Type.Missing, Type.Missing).Text = "Test"; } }