Esempio n. 1
0
        public byte[] ListToExcel <T>(List <T> list)
        {
            ExcelFileCreator excelCreator;
            MemoryStream     output = new MemoryStream();
            var columns             = GetTypeDefinition(typeof(T));

            try
            {
                if (initColumn == null)
                {
                    excelCreator = new ExcelFileCreator(columns);
                }
                else
                {
                    foreach (var item in columns)
                    {
                        item.HFontBold  = initColumn.HFontBold;
                        item.HFontColor = initColumn.HFontColor;
                        item.HBackColor = initColumn.HBackColor;
                    }
                    excelCreator = new ExcelFileCreator(columns);
                }
            }
            catch
            {
                excelCreator = new ExcelFileCreator(columns);
            }

            ProcessRows <T>(list, columns, excelCreator);
            lastRowNumber = excelCreator.LastRownNumber;
            output        = (MemoryStream)excelCreator.SaveDocument();
            return(output.ToArray());
        }
Esempio n. 2
0
        public HSSFWorkbook ProcessListToExcel <T>(List <T> list)
        {
            var columns = GetTypeDefinition(typeof(T));

            try
            {
                globalExcelCreator = new ExcelFileCreator(columns);
            }
            catch
            {
                globalExcelCreator = new ExcelFileCreator(columns);
            }
            ProcessRows <T>(list, columns, globalExcelCreator);
            lastRowNumber = globalExcelCreator.LastRownNumber;
            return(globalExcelCreator.GetDocument());
        }
Esempio n. 3
0
        private void ProcessRows <T>(List <T> list, List <Column> columns, ExcelFileCreator excel)
        {
            var  orderedColumns = columns.OrderBy(x => x.ColumnOrder);
            Type type           = typeof(T);
            int  columnNumber   = 0;

            foreach (var element in list)
            {
                excel.CreateRow();
                columnNumber = 0;
                foreach (var column in orderedColumns)
                {
                    var value = type.GetProperty(column.PropName).GetValue(element, null);
                    System.Drawing.Color backgroundColor = GetColor(element, column.CellColor, type);

                    excel.CreateCellWithValue(columnNumber, value, column.PropType, backgroundColor);
                    ++columnNumber;
                }
            }
        }