C# 导入Excel

  #region 导入
        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImport_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = new DataSet();
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "Excel文件(*.xls,*.xlsx)|*.xls;*.xlsx";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filename = openFileDialog.FileName;
                    string connection = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + filename + ";Extended Properties=Excel 8.0";
                    OleDbConnection thisconnection = new OleDbConnection(connection);
                    thisconnection.Open();
                    DataTable tb = thisconnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    string sheetName = tb.Rows[0]["TABLE_NAME"].ToString();
                    string command = "select * from [" + sheetName + "]";
                    OleDbDataAdapter adapter = new OleDbDataAdapter(command, thisconnection);
                    adapter.Fill(ds, "[" + sheetName + "]");
                    thisconnection.Close();
                    DataTable _DataRows = ds.Tables[0];
                    if (_DataRows.Rows.Count > 0)
                    {
                        //将从Excel读取的数据显示到gd上
                        ShowItems(_DataRows);
                    }
                    else
                        Commons.ShowMessageEx("所选择文件中没有可导入数据行!", MessageKind.Exclamation);
                }
            }
            catch (Exception ex)
            {
                if (ex.Message == "外部表不是预期的格式。")
                {
                    Commons.ShowMessageEx("请将数据文件进行另存后,重试导入!", MessageKind.Error);
                }
                else
                    MessageBoxEx.ShowError(ex);
            }
        }
        void ShowItems(DataTable ds)
        {
            this.dg_WelfaresDetails.Rows.Clear();
            int err = 0;
            string Msgs = string.Empty;
            decimal mJe = 0; //总金额

            int dgRows = 0;
            for (int i = 0; i < ds.Rows.Count; i++)
            {
                //检测账户是否存在 
                DataTable dt = CommonBLL.GetList("*", "V_Finance_EmployeeAccs", "ccode='" + ds.Rows[i]["账户编号"].ToString().Trim() + "' and CNAME='" + ds.Rows[i]["账户名称"].ToString().Trim() + "' and cbank='" + ds.Rows[i]["身份证号"].ToString().Trim() + "'");
                if (dt.Rows.Count > 0)
                {
                    this.dg_WelfaresDetails.Rows.Add(1);
                    this.dg_WelfaresDetails.Rows[dgRows].Cells["账户ID"].Value = dt.Rows[0]["AccID"].ToString();
                    this.dg_WelfaresDetails.Rows[dgRows].Cells["账户编号"].Value = ds.Rows[i]["账户编号"].ToString();
                    this.dg_WelfaresDetails.Rows[dgRows].Cells["账户名称"].Value = dt.Rows[0]["cname"].ToString();
                    this.dg_WelfaresDetails.Rows[dgRows].Cells["身份证号"].Value = dt.Rows[0]["cbank"].ToString();
                    this.dg_WelfaresDetails.Rows[dgRows].Cells["福利金额"].Value = ds.Rows[i]["福利金额"].ToString();
                    this.dg_WelfaresDetails.Rows[dgRows].Cells["福利备注"].Value = ds.Rows[i]["福利备注"].ToString();
                    this.dg_WelfaresDetails.Rows[dgRows].Cells["DetailsID"].Value = "";
                    dgRows++;

                    mJe += Commons.ParseDecimalValue(ds.Rows[i]["福利金额"].ToString());
                }
                else
                {
                    err++;
                    Msgs += "Excel中第【" + (i + 2).ToString() + "】行," + ds.Rows[i]["账户名称"].ToString().Trim() + "【账户信息有误】,请核查!\r\n";
                }
            }
            labMsgInfo.Text = "总行数:" + this.dg_WelfaresDetails.Rows.Count.ToString() + "         " + "总金额:" + mJe.ToString() + " [元]";
            if (err == 0)
            {
                //允许保存
                MessageBoxEx.ShowMessage("浏览Excle中数据成功!", MessageKind.Information);
                this.btnDeleteRow.Enabled = true;
                this.btnSave.Enabled = true;
            }
            else
            {
                //不允许保存
                MessageBoxEx.ShowMessage(Msgs, MessageKind.Exclamation);
                this.btnSave.Enabled = false;
            }
        }
        #endregion

 

posted on 2015-09-29 10:33  选择大于努力  阅读(422)  评论(0编辑  收藏  举报

导航