大哥:
   你好,现在我做一个windows 应用程序,有个导入的按钮,导入文件后(txt格式文件),读取文件的内容并做些转化,然后把转化的内容展示出来,并可以供用户手工编辑。完了后,用户可以将数据导出。
  在这个过程中:
  1、如何读入txt文件?小弟是菜鸟,如果把代码写出来就好了。小弟写的一些代码如下:
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
//ofd.Filter ="请选择"+cb_cp.Text+ "投注文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
this.label1.Text="您导入的文件是:"+ofd.FileName;;
}
                }
请问小弟想读取导入的文件,下一步如何做?   2、要把读入的内容展示出来,并可编辑,那怎样实现?新建个form吗?然后在datagrid中展现吗?貌似不能修改!
   
小弟在此谢谢你了!祝每一个给小弟出主意的人2008年好运!

解决方案 »

  1.   

        读取内容并显示出来。。
    很早以前写的,应该对你有用~~~   
    private void button1_Click(object sender, EventArgs e)
            {
                openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
                openFileDialog1.Title = "打开文件";
                openFileDialog1.InitialDirectory = "c:\\";
                openFileDialog1.RestoreDirectory = true;
                openFileDialog1.FileName = "";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)                                                 
                    richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                textBox1.Text = openFileDialog1.FileName;
                richTextBox1.Focus();
            }
      

  2.   

       //打开 rtf格式 和txt格式文件
             string str;
                openPicture.Filter = "文本文件(*.txt)|*.txt|rtf文件(*.rtf)|*.rtf";
                if (openPicture.ShowDialog() == DialogResult.OK && openPicture.FileName.Length > 0)
                {
                    str = openPicture.FileName;                FileStream fs = new FileStream(str, FileMode.Open, FileAccess.Read);
                    StreamReader m_streamReader = new StreamReader(fs);
                    //使用StreamReader类来读取文件
                      m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                    // 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
                      this.richTextBox1.Text = "";
                    string strLine = m_streamReader.ReadLine();
                    while (strLine != null)
                    {
                        this.richTextBox1.Text += strLine + "\n";
                        strLine = m_streamReader.ReadLine();
                    }
                    //关闭此StreamReader对象
                    m_streamReader.Close();
                }操作这样的数据 建议使用richtextbox 比较方便//导出 saveShow.Filter = "rtf文件(*.rtf)|*.rtf";
                saveShow.InitialDirectory = "e:\\";
                if (saveShow.ShowDialog() == DialogResult.OK && saveShow.FileName.Length > 0)
                {
                    richTextBox1.SaveFile(saveShow.FileName);
                }
    word 支持rtf文件读写
    而且rtf格式可以插入图片