其他分享
首页 > 其他分享> > NPOI操作excel

NPOI操作excel

作者:互联网

导入excel表格,数据转datetable,存入数据库

1.引入dll文件
在这里插入图片描述
工具—nuget包管理器—搜索
在程序包管理器控制台输入Install-Package NPOI -Version 2.0.6
2.转datetable

  public int uploadFile(HttpFileCollection files, string path)
        {
            string code = "";
            string name = "";
            string prepath = "/excelfile/";//存文件的文件夹
            ////添加评估文件
            string filename;
            string savepath;

            foreach (string key in files.AllKeys)
            {
                IWorkbook hssfworkbook = null;
                // HSSFWorkbook hssfworkbook;
                HttpPostedFile file = files[key];
                //判断文件是否为空
                if (file.ContentLength > 0)
                {
                    filename = Guid.NewGuid().ToString() + "$" + file.FileName;//给文件重命名
                    string ext = Path.GetExtension(file.FileName);//获取文件后缀名
                    savepath = path + filename + ext;
                    file.SaveAs(savepath);//存入
                    using (FileStream filestream = new FileStream(savepath, FileMode.Open, FileAccess.Read))
                    {
                        if (ext.ToLower() == ".xlsx")
                        {
                            hssfworkbook = new XSSFWorkbook(filestream);
                        }
                        else if (ext.ToLower() == ".xls")
                        {
                            hssfworkbook = new HSSFWorkbook(filestream);
                        }

                    }
                    //ISheet  sheet = hssfworkbook.GetSheet(filename);
                    ISheet sheet = hssfworkbook.GetSheetAt(0);
                    DataTable dt = ExcelHelper.RenderDataTableFromExcel(hssfworkbook, sheet.SheetName, 0);
                    ////////
                    //string prefix = "insert into a (HeadImg,IDNumber) values";
                    StringBuilder sb = new StringBuilder();
                    int count = dt.Rows.Count;//总数
                    int row = (count / 100) + 1;//分组数
                    for (int i = 0; i < row; i++)
                    {
                        int temp = 100;
                        if (i == row - 1)
                        {
                            temp = count % 100;
                        }
                        for (int j = 0; j < temp; j++)
                        {
                            code = dt.Rows[(i * 100) + j]["HeadImg"].ToString();
                            name = dt.Rows[(i * 100) + j]["IDNumber"].ToString();
                            sb.Append("('" + code + "','" + name + "'),");
                        }
                        //String sql = prefix + sb.Remove(sb.Length - 1, 1);
                        int hang = InsetData(code, name);
                        return hang;
                        //SqlHelper.Curtsql(sql);
                        //sb = new StringBuilder();
                    }
                }
                else
                {

                }
            }

            return 1;

        }

3.存库(用于ef)

 public int InsetData(string code, string name)
        {
            var context = new  ...Entities();
            SqlConnection conn = context.Database.Connection as SqlConnection;
            conn.Open();
            var cmd = (conn).CreateCommand();
            cmd.CommandText = "sql语句'";
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            return cmd.ExecuteNonQuery();
        }

4.遇到的问题
我这里会报程序集错误,因为dll版本问题,移除重新引入新版本

标签:code,string,int,hssfworkbook,excel,NPOI,new,操作,dt
来源: https://blog.csdn.net/weixin_42411608/article/details/88868425