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<NameDataExeclOrderMap>().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<DataRow>();
                Func<int, string> 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; }
        }

須要前臺傳送的 表頭格式:


導出模式(部分代碼): code

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;
相關文章
相關標籤/搜索