小弟在做一个winform程序,现在读入了一个txt文件,想把里面的内容抽取出来(就是用正则表达式匹配等方式),然后导出到一个excel文件。
    小弟写了如下的读取txt文件的代码,但不知如何往下走了。就是不知道如何将读出来的内容放到excel中去了。谁能给小弟些提示?小弟写的代码如下: private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "文本文件(*.txt) |*.txt";
if (ofd.ShowDialog() == DialogResult.OK)
{
filepathstr=ofd.FileName;
this.label1.Text="您导入的文件是:"+ofd.FileName; Thread tr=new Thread(new ThreadStart(ReadJob));   
tr.IsBackground=true;   
tr.Start(); 
}
}
protected string filepathstr="";   
public void ReadJob()   
{   
readout=read(filepathstr);   
Readcallback readcallback=new Readcallback(this.readoverwork); 
readcallback(this,EventArgs.Empty);
}   
public string read(string filepath)   
{   
//读文件代码省略   
string   tmp="";   
FileStream fs=new FileStream(filepath,FileMode.Open,FileAccess.Read);       
//使用StreamReader类来读取文件   
StreamReader  sr=new  StreamReader(fs,System.Text.Encoding.Default);       
//String  Line;   
            tmp=sr.ReadToEnd();
// while((Line=sr.ReadLine())!=null)   
// {   
// tmp+=Line+"\n"; 
// this.richTextBox1.Text+=Line+"\n";
// }   
return tmp;   
}   
//protected TextBox textBox1=new TextBox();//这行代码只是表示你的窗体上有个textBox1,具体代码不是这样的。   
protected string readout="";
protected delegate void Readcallback(object sender,EventArgs e);   
protected void readoverwork(object sender,EventArgs e)   
{   
if(richTextBox1.InvokeRequired)//在VS2003中允许跨线程访问UI元素,但在05中报错,所以要用到线程推送,如果你要显示结果在非UI控件中就不必如此。还可以参考MSDN的BACKGROUNDWORK类实现简单的跨线程掉用UI。   
{   
object[] chuan =new object[2];   
chuan[0]=sender;
chuan[1]=e;
richTextBox1.Invoke(new Readcallback(readoverwork),chuan);
}   
else
{   
richTextBox1.Text=readout;
}   
}祝进来看此帖子的各位大哥好运!

解决方案 »

  1.   

    Excel.Application myExcel = new Excel.Application();
                    myExcel.Application.Workbooks.Add(true);
                    myExcel.Visible = true;
                    for (int i = 0; i < neuronds.Tables["DataTable1"].Columns.Count; i++)
                    {
                        myExcel.Cells[2, i + 1] = neuronds.Tables["DataTable1"].Columns[i].Caption;
                    }
                    for (int i = 0; i < neuronds.Tables["DataTable1"].Rows.Count; i++)
                    {
                        for (int j = 0; j < neuronds.Tables["DataTable1"].Columns.Count; j++)
                        {
                            if (neuronds.Tables["DataTable1"].Rows[i][j].GetType() == typeof(System.String))
                            {
                                myExcel.Cells[i + 3, j + 1] = "'" + neuronds.Tables["DataTable1"].Rows[i][j].ToString();
                            }
                            else
                            {
                                myExcel.Cells[i + 3, j + 1] = neuronds.Tables["DataTable1"].Rows[i][j].ToString();
                            }
                        }
                    }
                    this.Cursor = Cursors.Default;
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
      

  2.   

    这个是将dataset里面的表写出来的,自己改改
      

  3.   

    谢谢hudingwen ,能加你MSN,请教下你吗