如何使用C#在Excel文本框中设置多行文本

时间:2012-11-29 03:02:03

标签: c# winforms excel

这是我的代码:

using Excel = Microsoft.Office.Interop.Excel;

private void button1_Click(object sender, EventArgs e)
{
    Excel.Application xlApp = new Excel.Application();
    Excel.Workbook wb = xlApp.Workbooks.Add(System.Type.Missing);
    Excel.Worksheet ws = (Excel.Worksheet)wb.Sheets[1];
    xlApp.Visible = true;

    Excel.Shape textBox = ws.Shapes.AddTextbox(
        Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
        10 + 10 + 10, 36, 600, 100);
    textBox.TextFrame.Characters(System.Type.Missing, System.Type.Missing).Text
        = "testing";

    xlApp.ActiveWindow.Activate();
    xlApp.UserControl = true;
    ws = null;
    wb = null;
    xlApp = null;
}

但是使用这段代码我只能添加一行文本“testing”:

---------------------------------
|Testing                        |
|                               |
|                               |
---------------------------------

现在我想添加3行文字,如下所示:

---------------------------------
|Test1                          |
|Test2                          |
|Test3                          |
---------------------------------

有人帮助我吗?

1 个答案:

答案 0 :(得分:2)

System.Environment.NewLine可能是你想要分离3个字符串的常数。