你好,我想在 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();