美文网首页
[C#] NPOI 将Excel 转成 DataTable (不

[C#] NPOI 将Excel 转成 DataTable (不

作者: 贪吃鱼 | 来源:发表于2017-03-29 18:55 被阅读759次

    NPOI 官网 http://npoi.codeplex.com/ 百度云: http://pan.baidu.com/s/1skV0LdV下载 NPOI组件 解压后 得到 !

    psb.jpg

    第一步在项目中引用 (我的项目是4.0 ,所以引用 Net40文件夹下面的dll)


    Paste_Image.png

    这5个dll都需要引用, 缺少某一个都会报错( 缺少依赖项 )第二步上代码拉 asp.net MVC项目的方法

    /// <summary>
            /// 导入excel
            /// </summary>
            /// <param name="file"></param>
            /// <returns></returns>
            public ActionResult ImportCompanyModel(HttpPostedFileBase file)
            {
                dynamic dy;
                if (file == null || file.ContentLength <= 0)
                {
                    dy = new { code = -1, msg = "没有要导入的文件!", imcount = 0, errorcount = 0};
                    return Json(dy);
                }
                try
                {
                    string[] fileex = {".xls",".xlsx"};
                    string extension = Path.GetExtension(file.FileName);
                    if (!fileex.Contains(extension))
                    {
                        return Json(new { code = -2, msg = "只能导入excel文件!", imcount = 0, errorcount = 0});
                    }
                    DataTable dt = ExcelHelper.GetExcelList(file.InputStream);
                    int imcount; 
                    int errorcount;  
                    AgentCompanyBll.ImportExcel(dt, LoginInfo.Loginid.ToInt(), out imcount, out errorcount);
                    dy = new { code = 0, msg = "导入完成!", imcount, errorcount };
                }
                catch (Exception ex)
                {
                   
                    WebExceptionHelper.AsyncProcessWebException(ExceptionModuleType.FuLuKeTrialWebsite, ex);
                    dy = new { code = -2, msg = "导入数据异常!", imcount = 0, errorcount = 0 };
                }
                return Json(dy);
                
            }
    
     
    ExcelHelper.cs 
    /// <summary>
    /// 将Excel转成table
    /// </summary>
    /// <param name="stream">文件流</param>
    /// <returns></returns>
    public static DataTable GetExcelList(Stream stream)
    {
        DataTable table = new DataTable();
        //导入excel 自动区分 xls 和 xlsx
        IWorkbook workbook = WorkbookFactory.Create(stream);
        ISheet sheet = workbook.GetSheetAt(0);//得到里面第一个sheet
        //获取Excel的最大行数
        int rowsCount = sheet.PhysicalNumberOfRows;
        //为保证Table布局与Excel一样,这里应该取所有行中的最大列数(需要遍历整个Sheet)。
        //为少一交全Excel遍历,提高性能,我们可以人为把第0行的列数调整至所有行中的最大列数。
        int colsCount = sheet.GetRow(0).PhysicalNumberOfCells;
        for (int i = 0; i < colsCount; i++)
        {
            //将第一列设置成表头
            table.Columns.Add(sheet.GetRow(0).GetCell(i).ToString());
        }
        for (int x = 0; x < rowsCount; x++)
        {
            if (x == 0) continue; //去掉第一列
            DataRow dr = table.NewRow();
            for (int y = 0; y < colsCount; y++)
            {
                dr[y] = sheet.GetRow(x).GetCell(y).ToString();
            }
            table.Rows.Add(dr);
        }
        return table;
    }
    

    完成啦 剩下的数据自己去处理吧

    相关文章

      网友评论

          本文标题:[C#] NPOI 将Excel 转成 DataTable (不

          本文链接:https://www.haomeiwen.com/subject/oldfottx.html