Example #1
0
        private void OnSave(object sender, RoutedEventArgs e)
        {
            try
            {
                string startStr = startTime.Text;
                string endStr   = endTime.Text;
                if (startStr == null)
                {
                    throw new Exception("请输入起始时间!");
                }
                if (endStr == null)
                {
                    throw new Exception("请输入结束时间!");
                }
                string strSelect      = "select * from tb_lhf_data where cytime between '" + startStr + "' and '" + endStr + "'" + " order by cytime asc";
                string strSelectCorro = "select * from tb_lhf_corro where cytime between '" + startStr + "' and '" + endStr + "'" + " and T=" + TControl.Text + " order by cytime asc";
                if (con.State == ConnectionState.Open)
                {
                    DataTable temp_dt      = GetTable(strSelect, con);
                    DataTable temp_dtCorro = GetTable(strSelectCorro, con);

                    //创建一个保存文件式的对话框
                    SaveFileDialog sfd = new SaveFileDialog();
                    //设置这个对话框的起始保存路径
                    sfd.InitialDirectory = System.Environment.CurrentDirectory + @"\";
                    //设置保存的文件的类型,注意过滤器的语法
                    sfd.Filter = "Excel文件|*.xlsx";
                    //调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
                    if (sfd.ShowDialog() == true)
                    {
                        OperateExcel operateExcel = new OperateExcel(sfd.FileName);
                        operateExcel.AddToSheet(temp_dt);
                        operateExcel.AddToSheet(temp_dtCorro);
                        System.Windows.MessageBox.Show("保存成功");
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("取消保存");
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
            // OperateExcel operateExcel = new OperateExcel();
        }