void tsmi_ExportExcel_Click(object sender, EventArgs e) { object src = ((sender as ToolStripMenuItem).Owner as ContextMenuStrip).SourceControl; if (src == null) { return; } //ListView lv = ((sender as ToolStripMenuItem).Owner as ContextMenuStrip).SourceControl as ListView; //if (lv == null) return; CExcel ce = new CExcel(); if (src is DataGridView) { ce.ExportExcel(src as DataGridView); } else if (src is ListView) { ce.ExportExcel(src as ListView); } else { return; } }
private void btn_export_Click(object sender, EventArgs e) { ////OpenFileDialog ofd = new OpenFileDialog(); ////ofd.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx|所有文件|*.*"; ////ofd.ValidateNames = true; ////ofd.CheckPathExists = true; ////ofd.CheckFileExists = true; ////if (ofd.ShowDialog() != DialogResult.OK) ////{ //// return; //// //其他代码 ////} ////string strFileName = ofd.FileName; CExcel ce = new CExcel(); ce.ExportExcel(this.listView1); }