C# Winform怎么弹出保存对话框?
2个回答
展开全部
下面是保存按纽里的代码:
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);
}
}
}
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);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#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();
}
}
}
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();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询