从richtextbox到excel的C#regex.split

时间:2012-06-10 07:08:54

标签: c# .net excel

我有52个项目存储在RichTextBox中,每行一个项目。所以我有52行。

我需要将RichTextBox中每行的值写入Excel范围C17:C42& H17:H42

我在精确的单元格中使用了一个代码来写入数组:

//for the first 26 data
range = objSheet.get_Range("C17:C42", Missing.Value);
object[,] numbers1 = new object[26, 1];
for (int i = 0; i <= 25; i++)
{
   numbers1[i, 0] = //here i want a read each line in richtextbox; 
}
range.Value2 = numbers1;

以下是我如何阅读RichTextBox的每一行:

    string s = richTextBoxReceive.Text;
    var k = Regex.Split(s, "\\n", RegexOptions.Multiline);

    foreach (string str in k)
    {
        //do what you want
        MessageBox.Show(str);
    }

我如何组合这些?

2 个答案:

答案 0 :(得分:0)

我认为它会起作用......

void insert( List<string> value)
{
    //for the first 26 data
    range = objSheet.get_Range("C17:C42", Missing.Value);
    object[,] numbers1 = new object[26, 1];
    for (int i = 0; i <= 25; i++)
    {
        //value contains the data
        numbers1[i, 0] = value[i];
    }
     range.Value2 = numbers1;
}


//Get value from richtextbox
string s = richTextBoxReceive.Text;
var k = Regex.Split(s, "\\n", RegexOptions.Multiline);
List<string> allvalue=new List<string>();

foreach (string str in k)
{
    allvalue.Add(str);
}
//call insert method to insert data into excell
insert(allvalue);

答案 1 :(得分:0)

好吧,我已经解决了这个问题。我使用了一个数组并且每行读取了richtextbox。

range = objSheet.get_Range("C17:C42", Missing.Value);
object[,] numbers3 = new object[26,1];
for (int i = 0; i<= richTextBoxReceive.Lines.Length; i++)
{
    for (int j = 0; j <= 25; j++)
    {
        numbers3[j, 0] = richTextBoxReceive.Lines[j + 1];
    }
}