コード例 #1
0
        private void toolStripLabel3_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"))
                            {
                                this.backgroundWorker1.RunWorkerAsync();                 // 运行 backgroundWorker 组件
                                JingDu form = new JingDu(this.backgroundWorker1, "读取中"); // 显示进度条窗体
                                form.ShowDialog(this);
                                form.Close();
                                List <PeiSe> list = cal.ReaderPeiSe(path);
                                DataTable    dt   = new DataTable();
                                if (list[0].Date != null)
                                {
                                    string strDate = DateTime.FromOADate(Convert.ToInt32(list[0].Date)).ToString("d");
                                    strDate = DateTime.Parse(strDate).ToString("dd-MMM-yyyy");
                                    dateTimePicker1.Value = Convert.ToDateTime(strDate);
                                }

                                cb_MianLiao.DropDownStyle = ComboBoxStyle.DropDown;
                                cb_MianLiao.Text          = list[0].Fabrics;
                                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                                {
                                    dt.Columns.Add(dataGridView1.Columns[i].HeaderCell.Value.ToString(), typeof(String));
                                }
                                foreach (PeiSe s in list)
                                {
                                    if (s.PingMing != null)
                                    {
                                        dt.Rows.Add(s.PingMing, s.HuoHao, s.GuiGe, s.C61601C1, s.C61602C1, s.C61603C1, s.C61605C1, s.C61606C1, s.C61607C1, s.C61609C1, s.C61611C1, s.C61618C1, s.C61624C1, s.C61627C1, s.C61631C1, s.C61632C1, s.C61633C1, s.C61634C1, s.MianLiaoYanSe, s.Id, s.Fabrics, s.Date);
                                    }
                                }
                                dataGridView1.DataSource = dt;
                                MessageBox.Show("读取完成!");
                            }
                            else
                            {
                                MessageBox.Show("读取失败!原因:读取文件后缀非'xlsx'");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw ex;
                MessageBox.Show(ex.Message);
            }
        }