private void toolStripLabel4_Click(object sender, EventArgs e) { try { DialogResult queren = MessageBox.Show("读取的'EXCEL文件'后缀必须为.Xlsx,否则读取失败!", "系统提示!", MessageBoxButtons.YesNo); if (queren == DialogResult.Yes) { if (this.openFileDialog1.ShowDialog() == DialogResult.OK) { string path = openFileDialog1.FileName; if (!path.Equals(string.Empty)) { if (!File.Exists(path)) { MessageBox.Show("文件不存在!"); return; } IntPtr vHandle = _lopen(path, OF_READWRITE | OF_SHARE_DENY_NONE); if (vHandle == HFILE_ERROR) { MessageBox.Show("文件被占用!"); return; } CloseHandle(vHandle); if (path.Trim().Contains("xlsx")) { list = cal.readerSehaoExcel(path); this.backgroundWorker1.RunWorkerAsync(); // 运行 backgroundWorker 组件 JingDu form = new JingDu(this.backgroundWorker1, "读取中"); // 显示进度条窗体 form.ShowDialog(this); form.Close(); DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(int)); dt.Columns.Add("Name", typeof(String)); dt.Columns.Add("SeHao1", typeof(String)); foreach (Sehao s in list) { dt.Rows.Add(s.Id, s.Name, s.SeHao1); } dataGridView1.DataSource = dt; MessageBox.Show("读取成功!"); } else { MessageBox.Show("读取失败!原因:读取文件后缀非'xlsx"); } } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }