如何在ExcelSheet的特定位置添加文本框?

您好,我尝试在Excelsheet中的特定位置添加一个文本框。 从A34到J39一样。 但我不知道该怎么做。

这是我试过的:

excelSheet.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 100, 100, 200, 50); 

工作,但文本框出现在工作表上随机的地方。

 Microsoft.Office.Tools.Excel.Controls.TextBox textBox1 = this.Controls.AddTextBox( this.Range["A1", "B2"], "textBox1"); textBox1.Text = "Sample text"; 

这是从MSDN网站。 但不会工作,因为你需要一个新的方法,我不应该添加一个新的方法……

 Range rng = UsedArea.Cells[rownum, cellnum]; txtbox = sheet.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, rng.Left, rng.Top, txt.Width / 2, rng.Height); 

这是来自SO,但它说范围属性不能改变…… Mabey它与办公室interop 15东西有关。

所以任何帮助或build议将是伟大的,thx为您的时间。

编辑:工作C#代码来获取任何位置的Excel表格中的文本框:

  Excel.Range rng = excelSheet.get_Range("A34:J39"); float left = (float)rng.Left; float top = (float)rng.Top; float width = (float)rng.Width; float height = (float)rng.Height; excelSheet.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, left, top, width, height).Select(); 

使用VBA

 Sub CoverRange() Dim r As Range Dim L As Long, T As Long, W As Long, H As Long Set r = Range("A34:J39") L = r.Left T = r.Top W = r.Width H = r.Height With ActiveSheet.Shapes .AddTextbox(msoTextOrientationHorizontal, L, T, W, H).Select End With End Sub 

创build后,您还可以重新resize并重新定位Shape 。 你可以适应这个你的代码?