Excel 多表头导入导出(借助Aspose)

需求中Excell多表头,完成导入导出。


Aspose 代码实现多表头方式借助代码比较繁琐, 借助模板方式。 简化逻辑。 注意,aspose从0开始索引。
导入部分代码: 实现选择导入Excel,导入Excel后,转换成DataTable, 同时将列明替换成数据库映射字段。(注意,严谨情况下需要比对导入Excel和模板Excel列是否相同,此处逻辑省略。)

//读取Excel模板
 var wb = new Workbook(Request.Files[0].InputStream);
                if (wb.Worksheets.Count <= 0)
                {
                    return Ext.Net.JSON.Serialize(new { success = false, msg = "不是有效的Excel文件" });
                }
                var ws = wb.Worksheets[0];
                var sheet = ws;

//读取Excel内容并将DataTable表头转换为数据库字段。
//页面Grid集合
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                serializer.MaxJsonLength = int.MaxValue;

                var gridHeaders= new List().DeserializeModel(GridHeaders);
                var GridModelsList = new { name = "", type = "" }.NMList().DeserializeModel(GridModels); 
                int firstStartDataRow  = 3;
                var ExcelDt = ws.Cells.ExportDataTableAsString(firstStartDataRow, 0, ws.Cells.MaxDataRow - firstStartDataRow + 1, gridHeaders.Count, false);
                var ExcelRows = ExcelDt.Rows.Cast();
                Func ExcelColumnName2DTColumnName = (int colIndex) =>
                {
                    return gridHeaders.Where(a => a.MapExcelColOrder == colIndex + 1).Select(a => a.DataIndex).FirstOrDefault();
                };
                for (var i = 0; i < ExcelDt.Columns.Count; i++)
                {
                    var findCN = ExcelColumnName2DTColumnName(i);
                    if (!string.IsNullOrWhiteSpace(findCN))
                    {
                        ExcelDt.Columns[i].ColumnName = findCN;
                    }
                }

//前台传送表头(用于显示,映射数据库字段,MapExcelColOrder )
        public class NameDataExeclOrderMap
        {
            public int MapExcelColOrder {get;set;}
            public string DataIndex { get; set; }

            public string Text { get; set; }
        }

需要前台传送的 表头格式:
Excel 多表头导入导出(借助Aspose)_第1张图片

导出模式(部分代码): 

var ExcelTemplateFileName = Server.MapPath(@"\App_Data\ExcelTemplate\导入导出模板.xls");
            Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(ExcelTemplateFileName);            
            var sheet = wb.Worksheets[0];
            

            var cellRowIndex = 3; //Excel开始写的行
            foreach (var row in dt.AsEnumerable())
            {
                foreach (var col in nameKeyMapOrderLst)
                {
                    if (dt.Columns.Contains(col.DataIndex))
                    {
                        sheet.Cells[cellRowIndex, col.MapExcelColOrder - 1].PutValue(row.GetRowValue(col.DataIndex));
                    }
                    else
                    {
                        sheet.Cells[cellRowIndex, col.MapExcelColOrder - 1].PutValue("");
                    }
                    sheet.Cells[cellRowIndex, col.MapExcelColOrder - 1].SetStyle(_tdStyle);
                }
                cellRowIndex++;
            }


            string fileName = _reportName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
            wb.Save(System.Web.HttpContext.Current.Server.MapPath("~/ExportFile/") + fileName);
            var Request = System.Web.HttpContext.Current.Request;
            return "http://" + Request.Url.Host + ":" + Request.Url.Port + "/ExportFile/" + fileName;

你可能感兴趣的:(Excel 多表头导入导出(借助Aspose))