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
  • 相关阅读:
    iBase4J部署总结
    就像我爱你,不仅仅是今天
    10年千亿美元,紫光集团目标跻身全球前五的存储器企业
    ddd
    微信的API都是通过https调用实现的,分为post方法调用和get方法调用。不需要上传数据的采用get方法(使用IntraWeb开发)
    管道通信实例(A程序作为服务器,不断从B程序接收数据,并发送到C程序中)
    HTTP协议中的短轮询、长轮询、长连接和短连接
    细说gulp
    Linux IO 调度器
    SPARK如何使用AKKA实现进程、节点通信
  • 原文地址:https://www.cnblogs.com/rwh871212/p/4845679.html
Copyright © 2011-2022 走看看