![](/img/trans.png)
[英]How to use ColorDialog to place text in Textbox in C# / Excel Interop
[英]How to add text to an existing shape(textbox) in 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
的類型是否為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";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.