在视图界面中添加导入按钮  要实现选中文件后就执行导入

<a class="btn green" id="btnExport" href="javascript:;"><i class="icon-plus icon-white"></i>导入</a>
                <input type="file" name="file1" id="file" style="display: none" title="" onchange="javascript:ExecImport()" value="浏览" />
                <button style="" id="sub" title="" value="浏览" />

上面的导入按钮 第一个<a>标签显示导入按钮

<input> 呈现出文件类型的按钮   当点击导入按钮时,触发此input

     $("#btnExport").click(function () {
            $("#file").click();
        })

在input中有一个触发事件, onchange="javascript:ExecImport()" 

这个触发事件 触发的是一个提交作用的按钮

   var ExecImport = function () {
            $("#sub").click();
        }

这个触发提交按钮 会提交到此按钮所在的form表单中

 @using (Html.BeginForm("ImportExcelFile", "Doctor", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <div>
                <a class="btn red" id="deleteitems" href="javascript:;"><i class="icon-trash icon-white"></i>删除</a>
                <a class="btn blue" title='添加医生' href="@Url.Action("Edit", new { doctid = 0 })"><i class="icon-plus icon-white"></i>新增</a>
                <a class="btn green" id="btnExport" href="javascript:;"><i class="icon-plus icon-white"></i>导入</a>
                <input type="file" name="file1" id="file" style="display: none" title="" onchange="javascript:ExecImport()" value="浏览" />
                <button style="" id="sub" title="" value="浏览" />
            </div>
        }

将会提交到Doctor控制器中的importExcelFile方法中进行执行

 

后台进行导入的代码:

我用的插件是npoi 要进行引用npoi的程序集

这个插件分导入.xls后缀的Excel文件时 用HSSFWorkbook类进行处理

导入xlms等其他excel文件时  用XSSFWorkbook类进行处理

      public ActionResult ImportExcelFile()
        {
            this.Session.Remove("drflag");
            //初始化信息
            #region
            HttpPostedFileBase file = Request.Files["file1"];
            if (file != null)
            {
                Stream streamfile = file.InputStream;
                DataTable dt = new DataTable();
                var houzhuiname = Path.GetExtension(file.FileName);
                try
                {
                    if (houzhuiname == ".xls")
                    {
                        HSSFWorkbook hssfworkbook = new HSSFWorkbook(streamfile);
                        dt = ImExport(dt, hssfworkbook);
                    }
                    else
                    {
                        XSSFWorkbook hssfworkbook = new XSSFWorkbook(streamfile);
                        dt = ImExport(dt, hssfworkbook);
                    }
                    if (this.HosService.ImportDoctorDB(dt) > 0)
                    {
                        this.Session.Add("drflag", "1");
                    }
                    else { this.Session.Add("drflag", "0"); }
                }

                catch (Exception ex)
                {
                    this.Session.Add("drflag", "5");
                    return RedirectToAction("HOSUserIndex");
                }
            #endregion

                if (dt.Rows.Count == 0)
                {
                    this.Session.Add("drflag", "2");
                    return RedirectToAction("Index");
                }
                return RedirectToAction("HOSUserIndex");
            }
            return null;
        }

下面贴出这两种导入的excel代码

Excel2007的版本,扩展名是.xlsx

 private static DataTable ImExport(DataTable dt, XSSFWorkbook hssfworkbook)
        {
            NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
            for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)
            {
                //dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());]  
                dt.Columns.Add(sheet.GetRow(0).Cells[j].ToString());
            }
            while (rows.MoveNext())
            {
                XSSFRow row = (XSSFRow)rows.Current;
                DataRow dr = dt.NewRow();
                for (int i = 0; i < row.LastCellNum; i++)
                {
                    NPOI.SS.UserModel.ICell cell = row.GetCell(i);
                    if (cell == null)
                    {
                        dr[i] = null;
                    }
                    else
                    {
                        dr[i] = cell.ToString();
                    }
                }
                dt.Rows.Add(dr);
            }
            dt.Rows.RemoveAt(0);
            return dt;
        }

Excel2003以前(包括2003)的版本

private static DataTable ImExport(DataTable dt, HSSFWorkbook hssfworkbook)
        {
            NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
            for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)
            {
                dt.Columns.Add(sheet.GetRow(0).Cells[j].ToString());
                //dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());
            }
            while (rows.MoveNext())
            {
                HSSFRow row = (HSSFRow)rows.Current;
                DataRow dr = dt.NewRow();
                for (int i = 0; i < row.LastCellNum; i++)
                {
                    NPOI.SS.UserModel.ICell cell = row.GetCell(i);
                    if (cell == null)
                    {
                        dr[i] = null;
                    }
                    else
                    {
                        dr[i] = cell.ToString();
                    }
                }
                dt.Rows.Add(dr);
            }
            dt.Rows.RemoveAt(0);
            return dt;
        }

 

然后弯沉后台具体插入表的操作就可以了