码迷,mamicode.com
首页 > 其他好文 > 详细

fileupload NPOI导入EXECL数据

时间:2016-08-23 16:51:45      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

fileupload JS

技术分享
@section scripts{
    <script src="~/Content/js/fileupload/vendor/jquery.ui.widget.js"></script>
    <script src="~/Content/js/fileupload/jquery.fileupload.js"></script>
    <script type="text/javascript">
      
        $("#fu_UploadFile").fileupload({
            url: _webBaseUrl + "/Owner/ImportExcel",
            acceptFileTypes: /(\.|\/)(xlsx|xls)$/i,
            add: function (e, data) {
                var file = data.files[0];
                var suffix = /\.[^\.]+/.exec(file.name.toUpperCase());
                if (suffix != .XLSX && suffix != .XLS) {
                    alert(建议上传.XLSX &nbsp 文件哦!);
                    return false;
                }
                //$("#Up").on("click", function () {
                data.submit();//开始上传
                //});
            },
            process: function (e, data) {

            },
            progressall: function (e, data) {

            },
            done: function (e, data) {
                var result = eval("(" + data.result + ")");
                if (result.IsSuccess == true) {
                    alert(result.Message);
                    PageJump(pageIndex);
                }
                else {
                    alert(result.Message);
                }
            },
            fail: function (e, data) {
                alert("上传失败,请联系管理员。");
            }
        });
    </script>
}
View Code


controller

技术分享
        public ActionResult ImportExcel()
        {
            string messages = string.Empty;
            bool isSuccess = false;
            try
            {
                HttpPostedFileBase file = Request.Files[0];//接收客户端传递过来的数据.
                if (file == null)
                {
                    messages = "请上传Excel文件";
                    return Content("{\"IsSuccess\":\"" + isSuccess + "\",\"Message\":\"" + messages + "\"}", "text/plain");
                }
                else
                {
                    //对文件的格式判断,此处省略
                    List<InOwnerVO> ownerList = new List<InOwnerVO>();
                    Stream inputStream = file.InputStream;
                    //HSSFWorkbook hssfworkbook = new HSSFWorkbook(inputStream);
                    XSSFWorkbook hssfworkbook = new XSSFWorkbook(inputStream);
                    NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
                    // IRow headerRow = sheet.GetRow(0);//第一行为标题行
                    // int cellCount = headerRow.LastCellNum;//LastCellNum = PhysicalNumberOfCells
                    int rowCount = sheet.LastRowNum;//LastRowNum = PhysicalNumberOfRows - 1

                    for (int i = (sheet.FirstRowNum + 1); i <= rowCount; i++)
                    {
                        IRow row = sheet.GetRow(i);
                        InOwnerVO owner = new InOwnerVO();
                        if (row != null)
                        {
                             if (row.GetCell(0) != null)
                            {
                                owner.Name = GetCellValue(row.GetCell(0));
                            }
                            if (row.GetCell(1) != null)
                            {
                                owner.Tel = GetCellValue(row.GetCell(1));
                            }
                             if (row.GetCell(2) != null)
                            {
                                owner.StoreNo = GetCellValue(row.GetCell(2));
                            }
                             if (row.GetCell(3) != null)
                            {
                                owner.HouseNo = GetCellValue(row.GetCell(3));
                            }

                        }
                        ownerList.Add(owner);
                    }

                    OwnerManager manager = new OwnerManager();
                    isSuccess = manager.ImportOwner(ownerList);
                    if (isSuccess)
                    {
                        messages = "导入成功!";
                    }
                    return Content("{\"IsSuccess\":\"" + isSuccess + "\",\"Message\":\"" + messages + "\"}", "text/plain");
                    //return Content("导入成功");
                }

            }
            catch (Exception e)
            {
                messages = "导入失败!";
                return Content("{\"IsSuccess\":\"" + isSuccess + "\",\"Message\":\"" + messages + "\"}", "text/plain");
                //return Content("导入失败");
            }
        }

        /// <summary>
        /// 根据Excel列类型获取列的值
        /// </summary>
        /// <param name="cell">Excel列</param>
        /// <returns></returns>
        private static string GetCellValue(ICell cell)
        {
            if (cell == null)
                return string.Empty;
            switch (cell.CellType)
            {
                case CellType.Blank:
                    return string.Empty;
                case CellType.Boolean:
                    return cell.BooleanCellValue.ToString();
                case CellType.Error:
                    return cell.ErrorCellValue.ToString();
                case CellType.Numeric:
                case CellType.Unknown:
                default:
                    return cell.ToString();//This is a trick to get the correct value of the cell. NumericCellValue will return a numeric value no matter the cell value is a date or a number
                case CellType.String:
                    return cell.StringCellValue;
                case CellType.Formula:
                    try
                    {
                        HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(cell.Sheet.Workbook);
                        e.EvaluateInCell(cell);
                        return cell.ToString();
                    }
                    catch
                    {
                        return cell.NumericCellValue.ToString();
                    }
            }
        }
View Code

 

fileupload NPOI导入EXECL数据

标签:

原文地址:http://www.cnblogs.com/love201314/p/5799667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!