コード例 #1
0
 /// <summary>
 ///     Adds a column mapping.  If no column mappings are specified all public properties will be used
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="worksheet"></param>
 /// <param name="map"></param>
 /// <param name="columnHeader"></param>
 /// <param name="configureColumn"></param>
 /// <returns></returns>
 public static WorksheetWrapper <T> WithColumn <T>(this WorksheetWrapper <T> worksheet, Func <T, object> map, string columnHeader, Action <ExcelColumn> configureColumn = null)
 {
     worksheet.Columns.Add(new WorksheetColumn <T>
     {
         Map             = map,
         Header          = columnHeader,
         ConfigureColumn = configureColumn
     });
     return(worksheet);
 }
コード例 #2
0
        /// <summary>
        ///     Converts given list of objects to ExcelPackage
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rows"></param>
        /// <param name="addHeaderRow"></param>
        /// <param name="worksheetName"></param>
        /// <returns></returns>
        public static ExcelPackage ToExcelPackage <T>(this IEnumerable <T> rows, bool addHeaderRow = true, string worksheetName = null)
        {
            WorksheetWrapper <T> worksheet = rows.ToWorksheet(string.IsNullOrEmpty(worksheetName) ? typeof(T).Name : worksheetName);

            if (!addHeaderRow)
            {
                worksheet.WithoutHeader();
            }

            return(worksheet.ToExcelPackage());
        }
コード例 #3
0
        /// <summary>
        ///     Converts list of items to Excel and returns the Excel file as bytearray.
        /// </summary>
        /// <typeparam name="T">Type of object</typeparam>
        /// <param name="rows">List of objects</param>
        /// <param name="addHeaderRow">Add header row to worksheet</param>
        /// <returns></returns>
        public static byte[] ToXlsx <T>(this IEnumerable <T> rows, bool addHeaderRow = true)
        {
            WorksheetWrapper <T> worksheet = rows.ToWorksheet(typeof(T).Name);

            if (!addHeaderRow)
            {
                worksheet.WithoutHeader();
            }

            return(worksheet.ToXlsx());
        }
コード例 #4
0
        /// <summary>
        ///     Converts given list of objects to ExcelPackage
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rows"></param>
        /// <param name="addHeaderRow"></param>
        /// <returns></returns>
        public static ExcelPackage ToExcelPackage <T>(this IEnumerable <T> rows, bool addHeaderRow = true)
        {
            WorksheetWrapper <T> worksheet = rows.ToWorksheet(typeof(T).Name);

            if (!addHeaderRow)
            {
                worksheet.WithoutHeader();
            }

            return(worksheet.ToExcelPackage());
        }
コード例 #5
0
        /// <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());
                }
        }
コード例 #6
0
        /// <summary>
        ///     Generates an Excel worksheet from given list
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rows"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static WorksheetWrapper <T> ToWorksheet <T>(this IEnumerable <T> rows, string name)
        {
            var worksheet = new WorksheetWrapper <T>
            {
                Name    = name,
                Package = new ExcelPackage(),
                Rows    = rows,
                Columns = new List <WorksheetColumn <T> >()
            };

            return(worksheet);
        }
コード例 #7
0
        /// <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);
        }
コード例 #8
0
        /// <summary>
        ///     Adds a title row to the top of the sheet
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="worksheet"></param>
        /// <param name="title"></param>
        /// <param name="configureTitle"></param>
        /// <returns></returns>
        public static WorksheetWrapper <T> WithTitle <T>(this WorksheetWrapper <T> worksheet, string title, Action <ExcelRange> configureTitle = null)
        {
            if (worksheet.Titles == null)
            {
                worksheet.Titles = new List <WorksheetTitleRow>();
            }

            worksheet.Titles.Add(new WorksheetTitleRow
            {
                Title          = title,
                ConfigureTitle = configureTitle
            });

            return(worksheet);
        }
コード例 #9
0
        /// <summary>
        ///     Generates an Excel worksheet from a list
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <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> ToWorksheet <T>(this IEnumerable <T> rows, string name, Action <ExcelColumn> configureColumn = null, Action <ExcelRange> configureHeader = null, Action <ExcelRange> configureHeaderRow = null, Action <ExcelRange, T> configureCell = null)
        {
            var worksheet = new WorksheetWrapper <T>()
            {
                Name               = name,
                Package            = new ExcelPackage(),
                Rows               = rows,
                Columns            = new List <WorksheetColumn <T> >(),
                ConfigureHeader    = configureHeader,
                ConfigureColumn    = configureColumn,
                ConfigureHeaderRow = configureHeaderRow,
                ConfigureCell      = configureCell
            };

            return(worksheet);
        }
コード例 #10
0
        /// <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);
        }
コード例 #11
0
        /// <summary>
        ///     Adds a column mapping.  If no column mappings are specified all public properties will be used
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="worksheet"></param>
        /// <param name="map"></param>
        /// <param name="columnHeader"></param>
        /// <param name="configurationAction"></param>
        /// <returns></returns>
        public static WorksheetWrapper <T> WithColumn <T>(this WorksheetWrapper <T> worksheet, Func <T, object> map,
                                                          string columnHeader, Action <IExcelConfiguration <T> > configurationAction = null)
        {
            IExcelConfiguration <T> configuration = DefaultExcelConfiguration <T> .Instance;

            configurationAction?.Invoke(configuration);

            worksheet.Columns.Add(new WorksheetColumn <T>
            {
                Map             = map,
                ConfigureHeader = configuration.ConfigureHeader,
                ConfigureColumn = configuration.ConfigureColumn,
                Header          = columnHeader,
                ConfigureCell   = configuration.ConfigureCell
            });
            return(worksheet);
        }
コード例 #12
0
        /// <summary>
        ///     Generates an Excel worksheet from a list
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rows"></param>
        /// <param name="name"></param>
        /// <param name="configurationAction"></param>
        /// <returns></returns>
        public static WorksheetWrapper <T> ToWorksheet <T>(this IEnumerable <T> rows, string name, Action <IExcelConfiguration <T> > configurationAction = null)
        {
            IExcelConfiguration <T> configuration = DefaultExcelConfiguration <T> .Instance;

            configurationAction?.Invoke(configuration);

            var worksheet = new WorksheetWrapper <T>
            {
                Name               = name,
                Package            = new ExcelPackage(),
                Rows               = rows,
                Columns            = new List <WorksheetColumn <T> >(),
                ConfigureHeader    = configuration.ConfigureHeader,
                ConfigureColumn    = configuration.ConfigureColumn,
                ConfigureHeaderRow = configuration.ConfigureHeaderRow,
                ConfigureCell      = configuration.ConfigureCell
            };

            return(worksheet);
        }
コード例 #13
0
        /// <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);
        }
コード例 #14
0
 /// <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);
 }
コード例 #15
0
 /// <summary>
 ///     Sets configurations of output Excel
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="worksheet"></param>
 /// <param name="configurationAction"></param>
 /// <returns></returns>
 public static WorksheetWrapper <T> WithConfiguration <T>(this WorksheetWrapper <T> worksheet, Action <ExcelCreateConfiguration <T> > configurationAction)
 {
     configurationAction?.Invoke(worksheet.Configuration);
     return(worksheet);
 }
コード例 #16
0
 /// <summary>
 ///     Indicates that the worksheet should not contain a header row
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="worksheet"></param>
 /// <returns></returns>
 public static WorksheetWrapper <T> WithoutHeader <T>(this WorksheetWrapper <T> worksheet)
 {
     worksheet.AppendHeaderRow = false;
     return(worksheet);
 }