Ejemplo n.º 1
0
        private void AddRows(IXLWorksheet worksheet, ExcelDataRegion dataRegion)
        {
            var row = dataRegion.Coordinates.DataRowInitial;

            foreach (var item in dataRegion.DataList)
            {
                var column = dataRegion.Coordinates.DataColumnInitial;
                if (dataRegion.ColumnsToExcel.Count() == 0)
                {
                    return;
                }

                foreach (var property in dataRegion.ColumnsToExcel)
                {
                    var p = item.GetType().GetProperty(property.Name);
                    if (p != null)
                    {
                        SetFormat(worksheet.Cell(row, column), p.GetValue(item, null), property.FormatWithEuros);
                        worksheet.Cell(row, column).Value = p.GetValue(item, null);
                    }
                    column++;
                }
                row++;
            }
        }
Ejemplo n.º 2
0
        private void AddHeader(IXLWorksheet worksheet, ExcelDataRegion dataRegion)
        {
            var headerRow    = dataRegion.Coordinates.HeaderRowInitial;
            var headerColumn = dataRegion.Coordinates.HeaderColumnInitial;

            foreach (var name in dataRegion.HeaderNames)
            {
                worksheet.Cell(headerRow, headerColumn).Value = name;
                headerColumn++;
            }
        }
Ejemplo n.º 3
0
        private void SetStyles(IXLWorksheet worksheet, ExcelDataRegion dataRegion)
        {
            //Rango de la cabecera
            int firstCellRow    = dataRegion.Coordinates.HeaderRowInitial;
            int firstCellColumn = dataRegion.Coordinates.HeaderColumnInitial;
            int lastCellRow     = dataRegion.Coordinates.HeaderRowInitial;
            int lastCellColumn  = dataRegion.ColumnsToExcel.Count();
            var rngHeader       = worksheet.Range(firstCellRow, firstCellColumn, lastCellRow, lastCellColumn);

            _excelStyle.ApplyStyles(rngHeader);
            worksheet.Columns().AdjustToContents();
        }
Ejemplo n.º 4
0
        private void GenerateSheets(string sheetName, ExcelColumn[] columnsToExcel,
                                    List <T> dataList, string[] headerNames)
        {
            if (_sheets != null)
            {
                foreach (var sheet in _sheets)
                {
                    using (IXLWorksheet worksheet = _workbook.Worksheets.Add(sheet.Name))
                    {
                        AddRegions(worksheet, sheet);
                    }
                }
            }
            else
            {
                _sheets = new List <ExcelSheet>();
                var excelSheet = new ExcelSheet();
                var regions    = new List <ExcelDataRegion>();
                var region     = new ExcelDataRegion();

                region.ColumnsToExcel = columnsToExcel;
                region.HeaderNames    = headerNames;
                region.DataList       = new List <object>();
                region.DataList.AddRange(dataList);
                region.Coordinates = new ExcelCoordinates();
                region.Coordinates.DataColumnInitial   = 1;
                region.Coordinates.DataRowInitial      = 2;
                region.Coordinates.HeaderColumnInitial = 1;
                region.Coordinates.HeaderRowInitial    = 1;
                regions.Add(region);
                excelSheet.Name    = sheetName;
                excelSheet.Regions = regions;

                using (IXLWorksheet worksheet = _workbook.Worksheets.Add(sheetName))
                {
                    AddRegions(worksheet, excelSheet);
                }
            }
        }