using System.IO
public void SaveFile()
{
//实例化一个保存文件对话框
SaveFileDialog sf = new SaveFileDialog();
//设置文件保存类型
sf.Filter = "txt文件|*.txt|xml";
//如果用户没有输入扩展名,自动追加后缀
sf.AddExtension = true;
//设置标题
sf.Title = "写文件";
//如果用户点击了保存按钮
if (sf.ShowDialog() == DialogResult.OK)
{
//实例化一个文件流--->与写入文件相关联
FileStream fs = new FileStream(sf.FileName, FileMode.Create);
//实例化一个StreamWriter-->与fs相关联
StreamWriter sw = new StreamWriter(fs);
//开始写入
if (this.dataGridView1.Rows.Count < 1)
{
MessageBox.Show("没有数据!导出失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i )
{
sw.WriteLine(this.dataGridView1.Rows[i].Cells[0].Value.ToString());
}
//sw.Write(this.textBox1.Text);
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}