如何在Excel工作表中的特定位置添加文本框?

3

你好,我想在 Excel 表格的特定位置添加文本框,例如从 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,但它说range属性不能被更改......可能与office interop 15有关。
所以任何帮助或建议都将是很好的,感谢您的时间。
编辑:工作的C#代码,以便在任何位置获取excelsheet中的文本框:
    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();

谢谢更新! (+1) - Gary's Student
1个回答

1
使用 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

你可以在创建形状后调整其大小和位置。你能否将此适应于你的代码?

@AndreasA。如果您已经在**c#中使其工作,请务必使用c#**代码更新您的原始问题.............这可能会帮助许多其他人。 - Gary's Student

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接