zoukankan      html  css  js  c++  java
  • EXCEL datatable 根据列名自动写入到相应属性、字段或列中

    string path = openFileDialog1.FileName;
                    try
                    {
                        DataTable dt = ExcelHelper.ExcelInput(path);
    
                        int appidIndex = -1;
                        int shopidIndex = -1;
                        int storeidIndex = -1;
    
    
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            string colNmaeL = dt.Columns[i].ColumnName.ToLower();
                            if (colNmaeL.Contains("app") && colNmaeL.Contains("id"))
                                appidIndex = i;
    
                            if (colNmaeL.Contains("shop") && colNmaeL.Contains("id"))
                                shopidIndex = i;
    
                            if (colNmaeL.Contains("store") && colNmaeL.Contains("id"))
                                storeidIndex = i;
                        }
    
                        List<SHOPID> lstShopids = new List<SHOPID>();
    
                        string firstAppId = string.Empty;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            DataRow dr = dt.Rows[i];
                            SHOPID si = new SHOPID();
                            if(dr[appidIndex]!=null&&!string.IsNullOrEmpty(dr[appidIndex].ToString()))
                                firstAppId = dr[appidIndex].ToString();
                            si.appid = firstAppId;
    
                            si.storeid = dr[storeidIndex].ToString();
                            si.shopid = dr[shopidIndex].ToString();
    
                            lstShopids.Add(si);
                        }
    
                        dataGridView1.DataSource = lstShopids;
    
                        MessageBox.Show("abc");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

    -

  • 相关阅读:
    设计模式学习心得5
    HTTP协议 (六) 状态码详解
    HTTP协议 (五) 代理
    HTTP协议 (四) 缓存
    HTTP协议 (三) 压缩
    HTTP协议 (二) 基本认证
    HTTP协议 (一) HTTP协议详解
    java中abstract和interface的區別(轉)
    基于TCP的字符串传输程序
    文件比较
  • 原文地址:https://www.cnblogs.com/runliuv/p/6028031.html
Copyright © 2011-2022 走看看