C#将excel导入到list,按照excel的表头字段,动态生成json数据

C#将excel导入到list,按照excel的表头字段,动态生成json数据,不用实体类接收,返回数据给前端,使用js进行序列化

        /// 
        /// 将excel导入到list
        /// 
        /// 
        /// 
        public static List ExcelToList(this Stream fs)
        {
            IWorkbook workbook = null;
            ISheet sheet = null;

            var x = new ExpandoObject() as IDictionary;

            List ts = new List();
            try
            {
                workbook = new HSSFWorkbook(fs);

                if (workbook != null)
                {
                    sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet

                    if (sheet != null)
                    {
                        int rowCount = sheet.LastRowNum;//总行数
                        if (rowCount > 0)
                        {
                            IRow firstRow = sheet.GetRow(0);//第一行
                            int cellCount = firstRow.LastCellNum;//列数

                            for (int i = 1; i <= rowCount; i++)
                            {
                                x = new ExpandoObject() as IDictionary;
                                IRow currRow = sheet.GetRow(i);//第i行
                                for (int k = 0; k < cellCount; k++)
                                {
                                    //如果值不为null
                                    if (currRow.GetCell(k) != null)
                                    {
                                        //设置单元格的值的类型
                                        firstRow.GetCell(0).SetCellType(CellType.String);
                                        currRow.GetCell(k).SetCellType(CellType.String);
                                        //获取表头的值
                                        var Name = firstRow.GetCell(k).StringCellValue;
                                        //获取当前单元格的值
                                        var Value = currRow.GetCell(k).StringCellValue;
                                        x.Add(Name, Value);
                                    }
                                    else
                                    {
                                        var Name = firstRow.GetCell(k).StringCellValue;
                                        var Value = string.Empty;
                                        x.Add(Name, Value);
                                    }
                                }
                                ts.Add(JsonConvert.SerializeObject(x as ExpandoObject));
                            }
                        }
                    }
                }

                return ts;
            }
            catch (Exception ex)
            {
                if (fs != null)
                {
                    fs.Close();
                }
                return null;
            }
        }
 
  

调用

   [Route("ReadExcel")]
    public ActionResult ReadExcel()
    {
        var list1 = new List();
        try
        {
            // 获取请求文件数据
            HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;

    

            list1 = files[0].InputStream.ExcelToList();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return Json(list1, JsonRequestBehavior.AllowGet);
    }
 
  

js代码

function Test() {
            $.post("/Home/ReadExcel", function (data) {
                if (data != "") {

                    for (var i = 0; i < data.length; i++) {
                        data[i] = JSON.parse(data[i]);
                        $("#List").append("

" + data[i].ID + "

"); } } }); } 返回的数据: "{"ID":"37c36139-7953-4d32-b533-3c1c94dd8b24","简介":"0测试","测试33":"0测试","测试33333":"0测试","昵称1":"0测试","昵称3":"0测试","昵称4":"0测试","昵称5":"0测试","限制人数":"2","模式":"3","收益率":"4","收益":"-22","头像":"","平台名称":"","用户昵称":"","你的事迹叫你":""}"

序列化之后
C#将excel导入到list,按照excel的表头字段,动态生成json数据_第1张图片

你可能感兴趣的:(excel,c#)