private void button1_Click(object sender, RoutedEventArgs e) { if (dt.Rows.Count <= 1) { System.Windows.MessageBox.Show("没有数据,无法导出", "提示"); return; } SaveFileDialog sfd = new SaveFileDialog(); string filename; sfd.InitialDirectory = System.Environment.CurrentDirectory; filename = MyDateTimeUtil.Format(Convert.ToDateTime(DatePicker1.Text), "yyyyMMdd"); //if (DatePicker2.Text == "") { filename = DatePicker1.Text; } if (DatePicker2.Text != "") { filename = filename + "-" + MyDateTimeUtil.Format(Convert.ToDateTime(DatePicker2.Text), "yyyyMMdd"); } sfd.FileName = filename + "自助入住记录统计"; sfd.RestoreDirectory = true; sfd.Filter = "Excel文件(*.xlsx)|*.xlsx|Excel文件(*.xls)|*.xls |CSV文件(*.csv) | *.csv"; if (sfd.ShowDialog() == DialogResult.OK) { if (sfd.FileName.ToLower().EndsWith("csv")) { CsvUtil cu = new CsvUtil(); cu.ExportDataToCVS(sfd.FileName, dt); System.Windows.MessageBox.Show("成功导出CSV文件", "成功"); } else { Type officeType = Type.GetTypeFromProgID("Excel.Application"); if (officeType == null) { Console.WriteLine("没安装!"); System.Windows.MessageBox.Show("提示", "请先安装office2007以上版本办公软件"); return; } else { Console.WriteLine("已安装!"); ExcelUtil eu = new ExcelUtil(); eu.DataTabletoExcel(dt, sfd.FileName); System.Windows.MessageBox.Show("成功导出EXCEL文件", "成功"); } } } //var dialog = new FolderBrowserDialog(); //dialog.ShowDialog(); //folderpathTB.Text = dialog.SelectedPath; // excelUtil eu = new excelUtil(); //eu.DataTabletoExcel(dt, System.Environment.CurrentDirectory + "\\temp.xlsx"); //eu.DataTabletoExcel(dt, dialog.SelectedPath + "\\temp.xlsx"); }