Exemplo n.º 1
0
        private void GenerateItems <T>(IList <T> dataSource, ColumnFactory <T> factory, ISheet sheet,
                                       Int32 startRowIndex = 1)
            where T : class
        {
            if (dataSource == null || !dataSource.Any())
            {
                return;
            }

            var bodyStyle = this.Workbook.CreateCellStyle();

            bodyStyle.WrapText          = true;
            bodyStyle.VerticalAlignment = VerticalAlignment.Top;

            foreach (var item in dataSource)
            {
                var row = sheet.CreateRow(startRowIndex);

                var columnIndex = 0;
                startRowIndex++;
                var maxLines = 1;
                foreach (var column in factory.Columns)
                {
                    var cell = row.CreateCell(columnIndex);
                    cell.CellStyle = bodyStyle;
                    var cellValue = string.Empty;
                    if (column.CellLines != null)
                    {
                        var lines = column.CellLines(item);
                        maxLines  = Math.Max(lines.Length, maxLines);
                        cellValue = string.Join("\n", lines);
                    }
                    else if (column.Property != null)
                    {
                        var propertyValue = column.Property(item);
                        if (propertyValue != null)
                        {
                            cellValue = propertyValue.ToString();
                        }
                    }
                    cell.SetCellValue(cellValue);
                    columnIndex++;
                }
                row.Height = (short)(this.BodyLineHeight * maxLines + 100);
            }
        }
Exemplo n.º 2
0
        public void Export <T>(string sheetName, IList <T> items, Action <ColumnFactory <T> > columns)
            where T : class
        {
            var sheet = String.IsNullOrEmpty(sheetName)
                            ? this.Workbook.CreateSheet()
                            : this.Workbook.CreateSheet(sheetName);
            var factory = new ColumnFactory <T>();

            columns(factory);
            this.CurrentSheet = sheet;
            this.GenerateHeader(factory, sheet);
            this.GenerateItems(items, factory, sheet);

            var columnIndex = 0;

            foreach (var column in factory.Columns)
            {
                sheet.AutoSizeColumn(columnIndex);
                var width = column.ColumnWidth;
                if (width <= 0)
                {
                    if (column.WidthFunction != null)
                    {
                        var       currentWidth = sheet.GetColumnWidth(columnIndex);
                        const int rate         = ColumnBuilder <T> .ExcelWidthRate;
                        width = column.WidthFunction(currentWidth / rate) * rate;
                    }
                }
                if (width > 0)
                {
                    sheet.SetColumnWidth(columnIndex, width);
                }
                else
                {
                    sheet.SetColumnWidth(columnIndex, sheet.GetColumnWidth(columnIndex) + 500);
                }
                columnIndex++;
            }
            this.Save();
        }
Exemplo n.º 3
0
        private void GenerateHeader <T>(ColumnFactory <T> factory, ISheet sheet, int startRowIndex = 0)
            where T : class
        {
            var headRow     = sheet.CreateRow(startRowIndex);
            var columnIndex = 0;
            var headerStyle = this.Workbook.CreateCellStyle();

            headerStyle.FillForegroundColor = HSSFColor.LightCornflowerBlue.Index;
            headerStyle.FillPattern         = FillPattern.SolidForeground;
            var font = this.Workbook.CreateFont();

            font.Boldweight = 2330;
            headerStyle.SetFont(font);
            headerStyle.VerticalAlignment = VerticalAlignment.Center;
            headRow.Height = (short)(this.BodyLineHeight + 150);
            foreach (var column in factory.Columns)
            {
                var columnCell = headRow.CreateCell(columnIndex);
                columnCell.SetCellValue(column.ColumnTitle);
                columnCell.CellStyle = headerStyle;
                columnIndex++;
            }
            headRow.CreateCell(columnIndex);
        }