示例#1
0
        /// <summary>
        /// 由GetFormatterContainer Func委托导出基于EXCEL模板的多工作薄文件
        /// </summary>
        /// <typeparam name="T">数据源可枚举项类型</typeparam>
        /// <param name="templatePath">模板路径</param>
        /// <param name="sheetName">模板中使用的工作薄名称</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="getFormatterContainer">生成模板数据格式化容器(SheetFormatterContainer)委托,在委托方法中实现模板的格式化过程</param>
        /// <param name="sheetSize">每个工作薄显示的数据记录数</param>
        /// <param name="filePath">导出路径,可选</param>
        /// <returns></returns>
        public static string ToExcelWithTemplate <T>(string templatePath, string sheetName, IEnumerable <T> dataSource, Func <IEnumerable <T>, SheetFormatterContainer> getFormatterContainer, int sheetSize, string filePath = null)
        {
            ExcelCommon.CheckTemplateAndExportPath(templatePath, ref filePath);

            if (string.IsNullOrEmpty(filePath))
            {
                return(null);
            }

            int             sheetCount          = 0;
            var             formatterContainers = new Dictionary <string, SheetFormatterContainer>();
            IEnumerable <T> data = null;

            while ((data = dataSource.Take(sheetSize)).Count() > 0)
            {
                var sheetFormatterContainer = getFormatterContainer(data);
                sheetCount++;
                if (sheetCount == 1)
                {
                    formatterContainers.Add(sheetName, sheetFormatterContainer);
                }
                else
                {
                    formatterContainers.Add(sheetName + sheetCount.ToString(), sheetFormatterContainer);
                }
                dataSource = dataSource.Skip(sheetSize);
            }
            string temp_templatePath = null;

            try
            {
                temp_templatePath = ExcelCommon.CreateTempFileByTemplate(templatePath, sheetName, sheetCount);
                filePath          = ToExcelWithTemplate(temp_templatePath, formatterContainers, filePath);
            }
            finally
            {
                ExcelCommon.DeleteTempTemplateFile(temp_templatePath);
            }

            return(filePath);
        }
示例#2
0
        /// <summary>
        /// 由SheetFormatterContainer集合导出基于EXCEL模板的多工作薄文件
        /// </summary>
        /// <param name="templatePath">模板路径</param>
        /// <param name="sheetName">模板中使用的工作薄名称</param>
        /// <param name="formatterContainers">模板数据格式化容器列表</param>
        /// <param name="filePath">导出路径,可选</param>
        /// <returns></returns>
        public static string ToExcelWithTemplate(string templatePath, string sheetName, List <SheetFormatterContainer> formatterContainers, string filePath = null)
        {
            ExcelCommon.CheckTemplateAndExportPath(templatePath, ref filePath);

            if (string.IsNullOrEmpty(filePath))
            {
                return(null);
            }

            string temp_templatePath = null;

            try
            {
                int sheetCount            = 0;
                var formatterContainerDic = new Dictionary <string, SheetFormatterContainer>();
                for (int i = 0; i < formatterContainers.Count; i++)
                {
                    sheetCount++;
                    if (sheetCount == 1)
                    {
                        formatterContainerDic.Add(sheetName, formatterContainers[i]);
                    }
                    else
                    {
                        formatterContainerDic.Add(sheetName + sheetCount.ToString(), formatterContainers[i]);
                    }
                }
                temp_templatePath = ExcelCommon.CreateTempFileByTemplate(templatePath, sheetName, sheetCount);

                filePath = ToExcelWithTemplate(temp_templatePath, formatterContainerDic, filePath);
            }
            finally
            {
                ExcelCommon.DeleteTempTemplateFile(temp_templatePath);
            }

            return(filePath);
        }