Example #1
0
        /// <summary>
        /// 输出文件模板
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="template"></param>
        /// <param name="stream"></param>
        static public void OutputTemplateFile <TModel>(this ExcelTemplate <TModel> template, Stream stream, string sheetName = null) where TModel : new()
        {
            if (template == null)
            {
                throw new NullReferenceException();
            }

            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            IMappingConfiguration <TModel> mapping = template;
            var workbook = new HSSFWorkbook();

            try
            {
                var sheet        = workbook.CreateSheet(sheetName ?? GetSheetNameFromType <TModel>());
                var row          = sheet.CreateRow(0);
                var defaultStyle = workbook.CreateCellStyle();
                // 默认列样式,有边框线
                defaultStyle.BorderTop        = BorderStyle.Thin;
                defaultStyle.TopBorderColor   = NPOI.HSSF.Util.HSSFColor.Black.Index;
                defaultStyle.BorderRight      = BorderStyle.Thin;
                defaultStyle.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                defaultStyle.BorderBottom     = BorderStyle.Thin;
                defaultStyle.TopBorderColor   = NPOI.HSSF.Util.HSSFColor.Black.Index;
                defaultStyle.BorderLeft       = BorderStyle.Thin;
                defaultStyle.LeftBorderColor  = NPOI.HSSF.Util.HSSFColor.Black.Index;

                // 标题样式:居中、加粗、有边框线
                var headerStyle = workbook.CreateCellStyle();
                headerStyle.CloneStyleFrom(defaultStyle);
                var headerFont = workbook.CreateFont();
                headerFont.Boldweight = 600;
                headerStyle.SetFont(headerFont);
                headerStyle.Alignment = HorizontalAlignment.Center;

                foreach (var property in mapping.GetPropertyMappings())
                {
                    sheet.SetDefaultColumnStyle(property.ColumnIndex, defaultStyle);

                    var column = row.CreateCell(property.ColumnIndex, CellType.String);
                    column.SetCellValue(GetColumnNameFromProperty(property.PropertyExpression));
                    column.CellStyle = headerStyle;
                    sheet.AutoSizeColumn(property.ColumnIndex);
                }
                sheet.CreateFreezePane(0, 1);
                workbook.Write(stream);
            }
            finally
            {
                workbook.Close();
            }
        }
Example #2
0
        SortedDictionary <int, IPropertyMapper <TModel> > CreatePropertyMappers()
        {
            var propertyMappers = new SortedDictionary <int, IPropertyMapper <TModel> >();

            foreach (var propertyMapping in _entityMapping.GetPropertyMappings())
            {
                var expression = propertyMapping.PropertyExpression;
                if (expression.NodeType != ExpressionType.Lambda)
                {
                    throw new InvalidOperationException("无效的属性访问表达式" + expression);
                }

                propertyMappers[propertyMapping.ColumnIndex] = (IPropertyMapper <TModel>)Activator.CreateInstance(
                    typeof(PropertyMapper <,>).MakeGenericType(typeof(TModel), ((LambdaExpression)expression).ReturnType)
                    , expression);
            }
            return(propertyMappers);
        }