private void button2_Click(object sender, EventArgs e) { DataTable dataTable = MysqlControl.Selectvalue("155", "222", "1").Tables[0]; int rowNumber = dataTable.Rows.Count; int columnNumber = dataTable.Columns.Count; if (rowNumber == 0) { MessageBox.Show("没有任何数据可以导入到Excel文件!"); } //建立Excel对象 Excel.Application excel = new Excel.Application(); excel.Application.Workbooks.Add(true); excel.Visible = true;//是否打开该Excel文件 //填充数据 for (int c = 0; c < rowNumber; c++) { for (int j = 0; j < columnNumber; j++) { excel.Cells[c + 1, j + 1] = dataTable.Rows[c].ItemArray[j]; } } excel.GetSaveAsFilename(); }
private void button1_Click(object sender, EventArgs e) { dataGridView1.DataSource = null; dataGridView1.DataSource = MysqlControl.Selectvalue("123", "2", "1").Tables[0]; }