/// <summary> /// 由SheetFormatterContainer集合导出基于多工作薄的EXCEL模板的文件 /// </summary> /// <param name="templatePath">模板路径</param> /// <param name="formatterContainers">模板数据格式化容器字典(Key:Sheet名称,Value:模板数据格式化容器对象)</param> /// <param name="filePath">导出路径,可选</param> /// <returns></returns> public static string ToExcelWithTemplate(string templatePath, IDictionary <string, SheetFormatterContainer> formatterContainers, string filePath = null) { ExcelCommon.CheckTemplateAndExportPath(templatePath, ref filePath); if (string.IsNullOrEmpty(filePath)) { return(null); } string templateConfigFilePath = ExcelCommon.GetTemplateConfigFilePath(templatePath, false); var workbookParameterContainer = new WorkbookParameterContainer(); workbookParameterContainer.Load(templateConfigFilePath); List <SheetFormatter> sheetFormatterList = new List <SheetFormatter>(); foreach (var item in formatterContainers) { SheetParameterContainer sheetParameterContainer = workbookParameterContainer[item.Key]; sheetFormatterList.Add(new SheetFormatter(item.Key, item.Value.GetFormatters(sheetParameterContainer))); } ExportHelper.ExportToLocal(templatePath, filePath, sheetFormatterList.ToArray()); return(filePath); }
/// <summary> ///由SheetFormatterContainer导出基于EXCEL模板的文件 /// </summary> /// <param name="templatePath">模板路径</param> /// <param name="sheetName">模板中使用的工作薄名称</param> /// <param name="formatterContainer">模板数据格式化容器</param> /// <param name="filePath">导出路径,可选</param> /// <returns></returns> public static string ToExcelWithTemplate(string templatePath, string sheetName, SheetFormatterContainer formatterContainer, string filePath = null) { ExcelCommon.CheckTemplateAndExportPath(templatePath, ref filePath); if (string.IsNullOrEmpty(filePath)) { return(null); } string templateConfigFilePath = ExcelCommon.GetTemplateConfigFilePath(templatePath, false); var workbookParameterContainer = new WorkbookParameterContainer(); workbookParameterContainer.Load(templateConfigFilePath); SheetParameterContainer sheetParameterContainer = workbookParameterContainer[sheetName]; ExportHelper.ExportToLocal(templatePath, filePath, new SheetFormatter(sheetName, formatterContainer.GetFormatters(sheetParameterContainer))); return(filePath); }
/// <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); }
/// <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); }