C# Winform怎么弹出保存对话框?

我在导出Execl时想弹出个保存对话框我知道用SaveFileDialog但不知道怎么用... 我在导出Execl时 想弹出个保存对话框
我知道用SaveFileDialog 但不知道怎么用
展开
 我来答
wangshuaisupin
2011-05-20 · TA获得超过1884个赞
知道小有建树答主
回答量:805
采纳率:100%
帮助的人:1236万
展开全部
下面是保存按纽里的代码:
private void button1_Click(object sender, EventArgs e)
{
//设置文件路径选择对话框属性
saveFileDialog1.AddExtension = true;
saveFileDialog1.DefaultExt = "xls";

saveFileDialog1.FileName = "新建文件.xls";
saveFileDialog1.Filter = "Excel files (*.xls)|*.xls|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.Title = "请选择保存路径!";

selectPath = false;
saveFileDialog1.ShowDialog();

if (saveFileDialog1.FileNames.Length > 0)
{
try
{
//创建Application对象
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbooks wbs = app.Workbooks;
Microsoft.Office.Interop.Excel.Workbook wb = wbs.Add(true);

Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//下面向excel写入内容就省略了
try
{
//保存文件
wb.SaveAs(saveFileDialog1.FileNames[0], Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wb.Close(false, Type.Missing, false);
wbs.Close();
app.Quit();
wb = null;
wbs = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;
System.GC.Collect();
models.Clear();
}
catch (System.Exception ex)
{
//stW.Close();
MessageBox.Show(ex.Message, "错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
}
catch (System.Exception exx)
{
//stW.Close();
MessageBox.Show(exx.Message, "错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
tzj112186
推荐于2017-11-28 · 超过17用户采纳过TA的回答
知道答主
回答量:104
采纳率:0%
帮助的人:108万
展开全部
#region 导出Excel
SaveFileDialog saveFileDialog;
private void button2_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files(*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录
//saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出Excel文件到";
DateTime now = DateTime.Now;
saveFileDialog.FileName = "报警记录报表"+now.Year.ToString().PadLeft(2) + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') + "-" + now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') + now.Second.ToString().PadLeft(2, '0');

//点了保存按钮进入
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog.FileName.Trim() == "")
{
MessageBox.Show("请输入要保存的文件名", "提示");
return;
}
progressBar1.Visible = true; //进度条
this.panel1.Enabled = false;

Thread threadReceive = new Thread(new ThreadStart(ExportTOExcel));
threadReceive.Start();
}
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式