我想用:(saveFileDialog.Title =this.tyuef()+"月水费";)saveFileDialog.Title这个变量也作为文件,当打开导出对话框的时候,文件名也自动为saveFileDialog.Title ,如何可以实现呢,谢谢!!!
private void SaveAs() //另存新档按钮   导出成Excel
        {            SaveFileDialog saveFileDialog = new SaveFileDialog();            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";            saveFileDialog.FilterIndex = 0;            saveFileDialog.RestoreDirectory = true;            saveFileDialog.CreatePrompt = true;            saveFileDialog.Title =this.tyuef()+"月水费";
            saveFileDialog.ShowDialog();
            if (saveFileDialog.ShowDialog() == DialogResult.Cancel)//如果选择提醒导出
                return;            Stream myStream;            myStream = saveFileDialog.OpenFile();            //StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));            string str = "";            try
            {                //写标题                for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {                    if (i > 0)
                    {                        str += "\t";                    }                    str += dataGridView1.Columns[i].HeaderText;                }
                sw.WriteLine(str);                //写内容                for (int j = 0; j < dataGridView1.Rows.Count; j++)
                {                    string tempStr = "";                    for (int k = 0; k < dataGridView1.Columns.Count; k++)
                    {                        if (k > 0)
                        {                            tempStr += "\t";                        }                        tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();                    }                    sw.WriteLine(tempStr);                }                sw.Close();                myStream.Close();            }            catch (Exception e)
            {                MessageBox.Show(e.ToString());            }            finally
            {                sw.Close();                myStream.Close();            }        }

解决方案 »

  1.   

     
       saveFileDialog.Title =this.tyuef()+"月水费"; 
    saveFileDialog.FileName=this.tyuef()+"月水费";             saveFileDialog.ShowDialog(); //多余
                if (saveFileDialog.ShowDialog() == DialogResult.Cancel)//如果选择提醒导出 
                    return;