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
  • 相关阅读:
    接口测试的维度
    python每个文件都需要顶部注释,那今天介绍一个方法,只需要设置一次,下次新建python文件后,注释自动出现在顶部的方法
    反射的4个方法
    python3 导入包总提示no moudle named xxx
    http.client.ResponseNotReady: Request-sent
    jmeter Linux环境执行总报错 cannot allocate memory
    xpath提取包含标签的所有文本内容
    使用python批量插入wordpress-从理清表结构开始
    Cloudways托管迁移中遇到的问题
    wordpress后台数据库表分析
  • 原文地址:https://www.cnblogs.com/rwh871212/p/4845679.html
Copyright © 2011-2022 走看看