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);
                    }

    -

  • 相关阅读:
    mysql 查询优化 ~ select count 知多少
    mongodb 案例 ~ 经典故障案例
    printk 驱动调试
    21天学通C++学习笔记(七):函数
    OPC UA
    MQTT
    分库分表
    水平、垂直权限问题(横向越权与纵向越权)
    数据库中的行转列和列转行
    面试知识点
  • 原文地址:https://www.cnblogs.com/runliuv/p/6028031.html
Copyright © 2011-2022 走看看