gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:52 25 4
gpt4 key购买 nike

您好,我尝试在 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 但它说范围属性不能更改......也许它与办公室互操作 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();

最佳答案

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

创建 Shape 后,您还可以重新调整其大小和位置。你能把它改编成你的代码吗??

关于c# - 如何在Excel工作表的特定位置添加文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36983847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com