private void button1_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); //openFileDialog1.InitialDirectory = "c:\\"; openFileDialog1.Filter = "Excel files |*.xls;*.xlsx"; //openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; openFileDialog1.Multiselect = false; if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { //chọn sheet ChonSheet cs = new ChonSheet(); cs.SetComboBox(DatatableFroExcelFile.GetListSheetNameFromFileExcel(openFileDialog1.FileName)); if (cs.ShowDialog() == System.Windows.Forms.DialogResult.OK) { dataGridView1.DataSource = kq = DatatableFroExcelFile.exceldata(openFileDialog1.FileName, cs.strSheet); filename = openFileDialog1.SafeFileName; button2.Enabled = true; } } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } }
private void btn_browser_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); //openFileDialog1.InitialDirectory = "c:\\"; openFileDialog1.Filter = @"Excel files |*.xls;*.xlsx"; //openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; openFileDialog1.Multiselect = false; if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { //chọn sheet ChonSheet cs = new ChonSheet(); cs.SetComboBox(DatatableFroExcelFile.GetListSheetNameFromFileExcel(openFileDialog1.FileName)); if (cs.ShowDialog() == DialogResult.OK) { dataGridView1.DataSource = _kq = DatatableFroExcelFile.exceldata(openFileDialog1.FileName, cs.strSheet); Filename = openFileDialog1.SafeFileName; btn_convert.Enabled = true; txt_batch.Text = ""; this.Text = Global.NameProgram + " " + openFileDialog1.FileName; lb_partfile.Text = cs.strSheet; for (int i = 0; i < dataGridView1.Rows.Count; i++) { dataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString(); } } } catch (Exception ex) { MessageBox.Show(@"Error: Could not read file from disk. Original error: " + ex.Message); } } }