/// <summary> /// Returns the Excel file as byte array. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="lastWorksheet"></param> /// <returns></returns> public static byte[] ToXlsx <T>(this WorksheetWrapper <T> lastWorksheet) { lastWorksheet.AppendWorksheet(); using (var stream = new MemoryStream()) using (ExcelPackage package = lastWorksheet.Package) { package.SaveAs(stream); return(stream.ToArray()); } }
/// <summary> /// Starts a new worksheet on the same Excel package /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="K"></typeparam> /// <param name="previousSheet"></param> /// <param name="rows"></param> /// <param name="name"></param> /// <returns></returns> public static WorksheetWrapper <T> NextWorksheet <T, K>(this WorksheetWrapper <K> previousSheet, IEnumerable <T> rows, string name) { previousSheet.AppendWorksheet(); var worksheet = new WorksheetWrapper <T> { Name = name, Package = previousSheet.Package, Rows = rows, Columns = new List <WorksheetColumn <T> >() }; return(worksheet); }
/// <summary> /// Starts new worksheet on same Excel package /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="K"></typeparam> /// <param name="previousSheet"></param> /// <param name="rows"></param> /// <param name="name"></param> /// <param name="configureColumn"></param> /// <param name="configureHeader"></param> /// <param name="configureHeaderRow"></param> /// <param name="configureCell"></param> /// <returns></returns> public static WorksheetWrapper <T> NextWorksheet <T, K>(this WorksheetWrapper <K> previousSheet, IEnumerable <T> rows, string name, Action <ExcelColumn> configureColumn = null, Action <ExcelRange> configureHeader = null, Action <ExcelRange> configureHeaderRow = null, Action <ExcelRange, T> configureCell = null) { previousSheet.AppendWorksheet(); var worksheet = new WorksheetWrapper <T>() { Name = name, Package = previousSheet.Package, Rows = rows, Columns = new List <WorksheetColumn <T> >(), ConfigureHeader = configureHeader ?? previousSheet.ConfigureHeader, ConfigureColumn = configureColumn ?? previousSheet.ConfigureColumn, ConfigureHeaderRow = configureHeaderRow ?? previousSheet.ConfigureHeaderRow, ConfigureCell = configureCell }; return(worksheet); }
/// <summary> /// Starts new worksheet on same Excel package /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="K"></typeparam> /// <param name="previousSheet"></param> /// <param name="rows"></param> /// <param name="name"></param> /// <param name="configurationAction"></param> /// <returns></returns> public static WorksheetWrapper <T> NextWorksheet <T, K>(this WorksheetWrapper <K> previousSheet, IEnumerable <T> rows, string name, Action <IExcelConfiguration <T> > configurationAction = null) { previousSheet.AppendWorksheet(); IExcelConfiguration <T> configuration = DefaultExcelConfiguration <T> .Instance; configurationAction?.Invoke(configuration); var worksheet = new WorksheetWrapper <T> { Name = name, Package = previousSheet.Package, Rows = rows, Columns = new List <WorksheetColumn <T> >(), ConfigureHeader = configuration.ConfigureHeader ?? previousSheet.ConfigureHeader, ConfigureColumn = configuration.ConfigureColumn ?? previousSheet.ConfigureColumn, ConfigureHeaderRow = configuration.ConfigureHeaderRow ?? previousSheet.ConfigureHeaderRow, ConfigureCell = configuration.ConfigureCell }; return(worksheet); }
/// <summary> /// Returns wrapped worksheets as ExcelPackage /// </summary> /// <typeparam name="T"></typeparam> /// <param name="lastWorksheet"></param> /// <returns>ExcelPackage</returns> public static ExcelPackage ToExcelPackage <T>(this WorksheetWrapper <T> lastWorksheet) { lastWorksheet.AppendWorksheet(); return(lastWorksheet.Package); }