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
  • 相关阅读:
    在Spring Boot中使用Swagger2
    初识设计模式之简单工厂模式、工厂方法模式、抽象工厂模式
    基于百度AI接口的微信小程序-数字识别
    微信小程序实现自定义遮罩
    微信小程序之视图容器movable-view实现拖拽功能
    微信小程序之map组件初体验
    基于百度AI接口的微信小程序-人脸搜索
    基于百度AI接口的微信小程序-图像搜索
    微信小程序实现上拉加载(分页加载)
    微信小程序实现下拉刷新
  • 原文地址:https://www.cnblogs.com/rwh871212/p/4845679.html
Copyright © 2011-2022 走看看