void ExportDataToExcel() { string fileName; this.openFileDialog1.Filter = "Excel数据文件|*.xls"; this.openFileDialog1.RestoreDirectory = true; if (this.openFileDialog1.ShowDialog() == DialogResult.OK) { fileName = this.openFileDialog1.FileName; string path = Path.GetDirectoryName(fileName); if (!path.EndsWith(@"\")) { path = path + @"\"; } string name = Path.GetFileName(fileName); SubscribeInfoManager.OutputDataToExcel(path, name, "TempInfo"); foreach (Process process in Process.GetProcessesByName("EXCEL")) { process.Kill(); } MessageBox.Show("数据已成功保存到 " + fileName); } }
void ExportData(DataTable tbl) { this.openFileDialog1.Filter = "Excel数据文件|*.xls"; this.openFileDialog1.RestoreDirectory = true; if (this.openFileDialog1.ShowDialog() == DialogResult.OK) { //先获取一个要导出的Excel文件名 fileName = this.openFileDialog1.FileName; string path = Path.GetDirectoryName(fileName); if (!path.EndsWith(@"\")) { path = path + @"\"; } string name1 = Path.GetFileName(fileName); //清空保存统计信息的表中的记录 SubscribeInfoManager.ClearStatInfoTable(); //将记录插入到StatInfoTable中 foreach (DataRow row in tbl.Rows) { AssignValues(row); SubscribeInfoManager.InsertIntoStatInfoTable(name, post, company, region, source, invoice, telephone, mobilephone, number, monthCount, totalMoney, averageMoney, monthMoney, startDate, endDate, giveDate); } //将StatInfoTable表中的记录导出到Excel文件中 SubscribeInfoManager.OutputDataToExcel(path, name1, "StatInfoTable"); foreach (Process process in Process.GetProcessesByName("EXCEL")) { process.Kill(); } MessageBox.Show("数据已成功保存到 " + fileName); } }