Example #1
0
        /// <summary>
        /// 非通用导出
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="mergeIndex">例:[[1, 1, 2, 1], [10, 1, 12, 1]],第1行第1列到第2行第1列合并,第10行第1列到第12行第1列合并</param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public string CreateWorkbook <T>(List <T> list, List <int[]> mergeIndex, string fileName) where T : new()
        {
            var filePath = GetCustomExcelFilePath("Export", fileName, out string urlAddress);

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            using (var ms = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                Workbook workbook = new Workbook();

                if (list != null)
                {
                    var titleAttribute = AttributesHelper.GetClassAttributeDescrition <T, TitleAttribute>();
                    var sheetName      = titleAttribute == null ? "sheet1" : string.IsNullOrEmpty(titleAttribute.Title) ? "sheet1" : titleAttribute.Title;
                    CreateSheet(list, workbook, 0, mergeIndex, sheetName);
                }
                workbook.SaveToStream(ms, FileFormat.Version2007);
            }

            return(urlAddress);
        }