C# 里saveFileDialog.ShowDialog()怎么取消
我现在想实现用户在界面操作结束后点保存的时候,不跳出来那个保存对话框.也就是想save一样,直接存到默认文件里,而不是像saveas那样跳出对话框让用户选择或输入文件名。...
我现在想实现用户在界面操作结束后点保存的时候,不跳出来那个保存对话框.也就是想save一样,直接存到默认文件里 ,而不是像save as那样跳出对话框让用户选择或输入文件名。
保存出来是个 Excel文件
点击之后直接默认保存到一个指定的地址里不出现下面这个,填写文件名和地址
那位代码高手知道怎么写 好几天了 都解决不了,一下是我自己写的代码 要怎么该才能实现我想要的呢?
SaveFileDialog savefileDialog = new SaveFileDialog();
savefileDialog.FileName = "abc";
savefileDialog.InitialDirectory = "c:\\";
savefileDialog.DefaultExt = "xls";
savefileDialog.FilterIndex = 0;
savefileDialog.RestoreDirectory = true;
savefileDialog.CreatePrompt = true;
savefileDialog.Title = "选择";
savefileDialog.ShowDialog();
Stream myStream;
myStream = savefileDialog.OpenFile();
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 ee)
{
MessageBox.Show(ee.ToString());
}
finally
{
sw.Close();
myStream.Close();
} 展开
保存出来是个 Excel文件
点击之后直接默认保存到一个指定的地址里不出现下面这个,填写文件名和地址
那位代码高手知道怎么写 好几天了 都解决不了,一下是我自己写的代码 要怎么该才能实现我想要的呢?
SaveFileDialog savefileDialog = new SaveFileDialog();
savefileDialog.FileName = "abc";
savefileDialog.InitialDirectory = "c:\\";
savefileDialog.DefaultExt = "xls";
savefileDialog.FilterIndex = 0;
savefileDialog.RestoreDirectory = true;
savefileDialog.CreatePrompt = true;
savefileDialog.Title = "选择";
savefileDialog.ShowDialog();
Stream myStream;
myStream = savefileDialog.OpenFile();
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 ee)
{
MessageBox.Show(ee.ToString());
}
finally
{
sw.Close();
myStream.Close();
} 展开
1个回答
推荐于2016-07-24
展开全部
FileStream fsFile = new FileStream(@"C:\abc.xls",FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fsFile);
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();
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
finally
{
sw.Close();
}
追问
感谢大哥~但是为什么导出来之后是乱码呢? 求帮助
追答
xls不是文本文件,你用文本的方式去写xls,所以乱码
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询