zoukankan      html  css  js  c++  java
  • 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() + "【账户信息有误】,请核查!
    ";
                    }
                }
                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
  • 相关阅读:
    HDU 5583 Kingdom of Black and White 水题
    HDU 5578 Friendship of Frog 水题
    Codeforces Round #190 (Div. 2) E. Ciel the Commander 点分治
    hdu 5594 ZYB's Prime 最大流
    hdu 5593 ZYB's Tree 树形dp
    hdu 5592 ZYB's Game 树状数组
    hdu 5591 ZYB's Game 博弈论
    HDU 5590 ZYB's Biology 水题
    cdoj 1256 昊昊爱运动 预处理/前缀和
    cdoj 1255 斓少摘苹果 贪心
  • 原文地址:https://www.cnblogs.com/rwh871212/p/4845679.html
Copyright © 2011-2022 走看看