zoukankan      html  css  js  c++  java
  • 利用ExcelDataReader封装类 导入表格数据

    nuget 添加Install-Package ExcelDataReader

    //FilePath为上传的文件路径

    public void Leading_in(string FilePath)
            {
                var file = new FileStream(FilePath, FileMode.Open, FileAccess.Read);//FilePath为文件的路径带名字,FileMode.Open为打开文件,FileAccess.Read为以只读的方式打开
                var excel = Excel.ExcelReaderFactory.CreateOpenXmlReader(file);
                var dataset = excel.AsDataSet();//将表格导出为dataset格式
                //循环插入数据到数据库
                Model.production model = new Model.production();
                BLL.production bll = new BLL.production();
                int i = 0;
                var lujing = HttpContext.Current.Session["lujing"].ToString();

                foreach (DataRow dr in dataset.Tables[0].Rows)
                {

                    if (i != 0 && lujing!="")
                    {
                        DataTable dt = new DataTable();
                        dt = new BLL.production().GetList_pro(" production_id=" + dr["Column1"]).Tables[0];
                        int n = int.Parse(dt.Rows.Count.ToString());
                        if (n == 0)
                        {
                            model.channel_id = 20;
                            model.category_id = 83;
                            model.production_id = int.Parse(dr["Column1"].ToString());
                            model.pmzc = dr["Column2"].ToString();
                            model.title1 = dr["Column3"].ToString();
                            model.author = dr["Column4"].ToString();
                            model.img_url1 = lujing + dr["Column5"].ToString();
                            model.size = dr["Column6"].ToString();
                            model.creation_year = dr["Column7"].ToString();
                            model.judge = dr["Column8"].ToString();
                            if (!string.IsNullOrEmpty(dr["Column10"].ToString()))
                            {
                                model.explain_1 = dr["Column10"].ToString();
                            }
                            model.explain_2 = dr["Column11"].ToString();
                            model.sort_id = 99;
                            if (bll.Add(model) < 1)
                            {

                            }
                        }
                        else {
                            foreach (DataRow item in dt.Rows)
                            {

                                Model.production model_1 = bll.GetModel(int.Parse(item["id"].ToString()));
                                model_1.price_1 = dr["Column9"].ToString();
                                if (bll.Update(model_1))
                                {

                                }
                            }
                           
                           
                        }
                       
                    }
                    i++;
                }
                
            }

    红色部分为引入包后的初始化

  • 相关阅读:
    Set.prototype (Set) – JavaScript 中文开发手册
    wordpress插件 : 利用Max Mega Menu实现二级菜单的伸缩
    HTML textarea placeholder 属性
    Number.parseInt (Number) – JavaScript 中文开发手册
    Java 实例 – 获取 URL响应头的日期信息
    PHP date_default_timezone_set() 函数
    SIMD.fromInt32x4Bits (SIMD) – JavaScript 中文开发手册
    使用 mysql_random_data_load 生成随机数据
    python基本数据类型
    day02代码
  • 原文地址:https://www.cnblogs.com/WZH75171992/p/4140113.html
Copyright © 2011-2022 走看看