从 richtextbox 中获取每一行并使用 c# 导出到 excel 单元格

Get each line from a richtextbox and export to excel cells using c#

我正在尝试遍历 richtextBox 中的每一行内容,并将其传递给同一列中每一行的单元格。我不知道如何让这两个循环一起工作。以下是我想出的:

                int length = richTextBox1.Lines.Length;
                for (int index = 1; index < length; index++)
                {
                    string content = string.Empty;
                    foreach (string str in richTextBox1.Lines)
                    {
                        workSheet.Cells[rowCount + index, 1] = str;
                    }
                 }

这会遍历所有行,但我无法将每一行提取到不同的单元格中。这最终在 sheet 的每个单元格中给出了富文本框的最后一行。任何想法表示赞赏!谢谢

您可以使用 index 访问 Lines 属性 中的每个 string 对象。

int length = richTextBox1.Lines.Length;

for (int index = 1; index < length; index++)
{
     workSheet.Cells[rowCount + index, 1] = richTextBox1.Lines[index];
}

参考https://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.lines%28v=vs.110%29.aspx