private void button9_Click(object sender, EventArgs e) { string tbname = comboBox2.Text.ToString(); string colname = comboBox5.Text.ToString(); string csql = " select distinct " + "[" + colname + "]" + " from " + "[" + tbname + "]"; string sql = " select * from " + "[" + tbname + "]" + " where " + "[" + colname + "]" + " = "; IList <string> clums = new List <string>(); DataTable dt = UiServices.GetDataTableFromSQL(csql); int num = dt.Rows.Count; if (dt.Rows.Count > 0) { foreach (DataRow _DataRowItem in dt.Rows) { clums.Add(_DataRowItem[colname].ToString()); } } string filename = UiServices.ShowFileDialog(tbname); Stopwatch watch = Stopwatch.StartNew(); watch.Start(); foreach (var a in clums) { string savepath = filename.Remove(filename.LastIndexOf('.')) + '_' + a + ".xlsx"; DataTable dt1 = UiServices.GetDataTableFromSQL(sql + "'" + a + "'"); UiServices.SaveExcel(savepath, dt1, "sheet1"); } watch.Stop(); toolStripStatusLabel1.Text = string.Format("导出的时间为:{0}秒", watch.ElapsedMilliseconds / 1000); toolStripStatusLabel1.ForeColor = Color.Red; MessageBox.Show("导出成功"); GC.Collect(); }
private void button10_Click(object sender, EventArgs e) { string tbname = comboBox2.Text.ToString(); string colname = comboBox5.Text.ToString(); string csql = " select distinct " + "[" + colname + "]" + " from " + "[" + tbname + "]"; string sql = " select * from " + "[" + tbname + "]" + " where " + "[" + colname + "]" + " = "; IList <string> clums = new List <string>(); DataTable dt = UiServices.GetDataTableFromSQL(csql); int num = dt.Rows.Count; if (dt.Rows.Count > 0) { foreach (DataRow _DataRowItem in dt.Rows) { clums.Add(_DataRowItem[colname].ToString()); } } string filename = ""; System.Windows.Forms.SaveFileDialog saveFileDialog1 = new System.Windows.Forms.SaveFileDialog(); saveFileDialog1.Filter = "CSV|*.csv"; saveFileDialog1.FileName = tbname; saveFileDialog1.DefaultExt = ".csv"; if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { filename = saveFileDialog1.FileName.ToString(); } else { return; } //string filename = UiServices.ShowFileDialog(tbname); Stopwatch watch = Stopwatch.StartNew(); watch.Start(); foreach (var a in clums) { string savepath = filename.Remove(filename.LastIndexOf('.')) + '_' + a + ".csv"; DataTable dt1 = UiServices.GetDataTableFromSQL(sql + "'" + a + "'"); UiServices.SaveCsv(dt1, savepath); } watch.Stop(); toolStripStatusLabel1.Text = string.Format("导出的时间为:{0}秒", watch.ElapsedMilliseconds / 1000); toolStripStatusLabel1.ForeColor = Color.Red; MessageBox.Show("导出成功"); GC.Collect(); }
public static int WriteCsvFromsql(string sql, string filename) { try { DataTable dt = UiServices.GetDataTableFromSQL(sql); int i = SaveCsv(dt, filename); return(i); } catch (Exception ex) { MessageBox.Show(ex.Message); } return(-1); }