コード例 #1
0
        public byte[] SetTitleDecoration(byte[] workBookBytes, DecorationContext context, ExportOption exportOption)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (exportOption == null)
            {
                return(workBookBytes);
            }

            if (string.IsNullOrWhiteSpace(exportOption.Title))
            {
                return(workBookBytes);
            }

            List <BaseDecorationAttribute> basesList = new List <BaseDecorationAttribute>();

            basesList.AddRange(context.TypeDecoInfo.TypeDecoAttrs);
            basesList.AddRange(context.TypeDecoInfo.propertyDecoInfos.SelectMany(pd => pd.PropertyAttrs));

            IWorkbook workbook = workBookBytes.ConvertBytesToWorkBook();

            var titleAttr = (TitleAttribute)basesList.FirstOrDefault(bd => bd.GetType() == typeof(TitleAttribute));

            ICellStyle cellStyle = workbook.CreateCellStyle();

            cellStyle.Alignment           = titleAttr.HorizontalAlignment;
            cellStyle.VerticalAlignment   = titleAttr.VerticalAlignment;
            cellStyle.FillForegroundColor = titleAttr.FillForegroundColor;
            cellStyle.FillPattern         = titleAttr.FillPattern;
            cellStyle.BorderBottom        = titleAttr.BorderBottom;
            cellStyle.BorderLeft          = titleAttr.BorderLeft;
            cellStyle.BorderRight         = titleAttr.BorderRight;


            IFont font = workbook.CreateFont();

            font.Color              = (short)titleAttr.FontColor.GetHashCode();
            font.FontName           = titleAttr.FontName;
            font.FontHeightInPoints = titleAttr.FontSize;
            if (titleAttr.IsBold)
            {
                font.IsBold = true;
            }
            cellStyle.SetFont(font);


            ISheet sheet = workbook.GetSheet(exportOption.SheetName);
            IRow   row   = sheet.GetRow(0);

            for (int i = 0; i < row.PhysicalNumberOfCells; i++)
            {
                row.GetCell(i).CellStyle = cellStyle;
            }
            return(workbook.ConvertWorkBookToBytes());
        }
コード例 #2
0
        public static List <IFilter> CreateFilters(DecorationContext context)
        {
            List <IFilter> filters = new List <IFilter>();
            List <BaseDecorationAttribute> bdecoAttrs = new List <BaseDecorationAttribute>();

            bdecoAttrs.AddRange(context.TypeDecoInfo.TypeDecoAttrs);
            bdecoAttrs.AddRange(context.TypeDecoInfo.propertyDecoInfos.SelectMany(pd => pd.PropertyAttrs));
            bdecoAttrs.Distinct(new DecorationCompare()).ToList().ForEach(bd =>
            {
                filters.Add(FilterFactory.CreateInstance(bd));
            });
            return(filters);
        }
コード例 #3
0
        public byte[] ExportExcel <TExport>(ExportOption exportOption, List <TExport> datas) where TExport : class, new()
        {
            var provider = this._exportProvider;

            if (exportOption.ExcelExportProvider != null)
            {
                provider = exportOption.ExcelExportProvider;
            }

            IWorkbook workbook = null;

            if (exportOption.ExcelType == Core.ExcelExport.Enums.ExcelType.XLS)
            {
                workbook = new HSSFWorkbook();
                if (datas.Count > 65536)
                {
                    throw new NotSupportedException("xls文档最大长度是65536行");
                }
            }
            else if (exportOption.ExcelType == Core.ExcelExport.Enums.ExcelType.XLSX)
            {
                workbook = new XSSFWorkbook();
                if (datas.Count > 1048576)
                {
                    throw new NotSupportedException("xlsx文档最大长度是1048576行");
                }
            }
            if (workbook == null)
            {
                throw new NotSupportedException($"{nameof(workbook)}不能是null");
            }

            DataSet dataSet = this.FindDataSource <TExport>(datas, exportOption.PropertyNames);

            byte[] workbookBytes = this.Export(exportOption, workbook, dataSet);

            DecorationContext context = new DecorationContext {
                TypeDecoInfo = TypeDecoInfoFactory.CreateTypeDecoInfo <TExport>()
            };

            List <IFilter> filters = FiltersFactory.CreateFilters(context);

            filters.ForEach(f =>
            {
                workbookBytes = f.Filter(workbookBytes, context, exportOption, provider);
            });

            return(workbookBytes);
        }
コード例 #4
0
 public byte[] Filter(byte[] workBookBytes, DecorationContext context, ExportOption exportOption, IExcelExportProvider exportProvider)
 {
     return(exportProvider.SetTitleDecoration(workBookBytes, context, exportOption));
 }