导入 导出 Excel

 public class IntOutController : Controller
    {
        // GET: IntOut
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        public FileResult ExportStu2()
        {
            //创建Excel文件的对象
            HSSFWorkbook book = new HSSFWorkbook();
            //创建第一个工作簿
            ISheet sheet = book.CreateSheet("sheet1");
            //获取list数据
            List<StudentInfo> listStudentInfo = GetStudent();
            //添加标头
            IRow row = sheet.CreateRow(0);//以create开头
            row.CreateCell(0).SetCellValue("ID");
            row.CreateCell(1).SetCellValue("Name");
            row.CreateCell(2).SetCellValue("Sex");
            row.CreateCell(3).SetCellValue("Score");
            //将数据逐步写入sheet个各行
            for (int i = 0; i < listStudentInfo.Count; i++)
            {
                IRow rowtemp = sheet.CreateRow(i + 1);
                rowtemp.CreateCell(0).SetCellValue(listStudentInfo[i].ID.ToString());
                rowtemp.CreateCell(1).SetCellValue(listStudentInfo[i].Name.ToString());
                rowtemp.CreateCell(2).SetCellValue(listStudentInfo[i].Sex.ToString());
                rowtemp.CreateCell(3).SetCellValue(listStudentInfo[i].Score.ToString());
            }
            MemoryStream ms = new MemoryStream();
            book.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);
            return File(ms, "application/vnd.ms-excel", "学生基本信息.xls");
        }
        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="filebs"></param>
        /// <returns></returns>
        public ActionResult ImportExcelFile(HttpPostedFileBase filebs)
        {
            HttpPostedFileBase file = Request.Files["filebs"];
            Stream a = file.InputStream;
            HSSFWorkbook book = new HSSFWorkbook(a);
            ISheet sheet = book.GetSheetAt(0);
            DataTable dt = new DataTable();
            IRow row = sheet.GetRow(0);
            int cellcount = row.LastCellNum;
            int rowcount = sheet.LastRowNum;
            for (int i = 0; i < cellcount; i++)
            {
                DataColumn column = new DataColumn(row.GetCell(i).StringCellValue);
                dt.Columns.Add(column);
            }
            for (int i = 1; i < rowcount; i++)
            {
                IRow roww = sheet.GetRow(i);
                DataRow datarow = dt.NewRow();
                //if(roww!=null)
                //{
                    for (int j = 0; j < cellcount; j++)
                    {
                        //if (roww.GetCell(j) != null)
                        //{
                            datarow[j] = roww.GetCell(j);
                       // }
                    }
                //}
                dt.Rows.Add(datarow);
            }
            return View();
        }
        public List<StudentInfo> GetStudent()
        {
            return new List<StudentInfo>() {
                 new StudentInfo(){ID=1,Name="张三",Sex="男",Score=100},
                 new StudentInfo(){ID=1,Name="李四",Sex="男",Score=100},
                 new StudentInfo(){ID=1,Name="王五",Sex="男",Score=100},
                 new StudentInfo(){ID=1,Name="赵六",Sex="男",Score=100},
                 new StudentInfo(){ID=1,Name="小宁",Sex="男",Score=100},
                 new StudentInfo(){ID=1,Name="小明",Sex="男",Score=100},
                 new StudentInfo(){ID=1,Name="王丽",Sex="男",Score=100}
            };

        }
    }

导入 导出 Excel