Пример #1
0
        /// <summary>
        /// get headers
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public List <string> GetPropertyDisplayNames(List <string> titleList = null, ResourceManager rm = null)
        {
            titleList = (titleList == null ? new List <string>() : titleList);

            foreach (var propertyInfo in ExcelColumns)
            {
                object[] attrs = propertyInfo.GetCustomAttributes(true);
                foreach (object attr in attrs)
                {
                    ///if this column need to export
                    ExcelColumnAttribute authAttr = attr as ExcelColumnAttribute;
                    if (authAttr != null)
                    {
                        var titleName       = propertyInfo.GetDisplayName();
                        var displayAttrInfo = propertyInfo.GetCustomAttributes(true).Where(it => it.GetType() == typeof(DisplayAttribute)).SingleOrDefault();
                        if (displayAttrInfo != null)
                        {
                            var displayAttr  = displayAttrInfo as DisplayAttribute;
                            var resourceType = displayAttr.ResourceType;
                            if (resourceType != null && rm != null)
                            {
                                //var rm = new ResourceManager(resourceType);
                                titleName = rm.GetString(titleName);
                            }
                        }
                        titleList.Add(titleName);
                    }

                    ///if this object of collection need to export
                    var childDataAttr = attr as ChildDataAttribute;
                    if (childDataAttr != null)
                    {
                        var childHelper = new ExcelColumnHelper(childDataAttr.type);
                        childHelper.GetPropertyDisplayNames(titleList);
                    }
                }
            }

            return(titleList);
        }
Пример #2
0
        /// <summary>
        /// edit this sheet
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dataList"></param>
        /// <param name="worksheet"></param>
        /// <returns></returns>
        public static ISheet ExcelSheet <T>(this IEnumerable <T> dataList, ISheet worksheet, IWorkbook workbook, ResourceManager rm = null)
        {
            var datatype = typeof(T);

            //Insert titles
            var row = worksheet.CreateRow(0);
            var execlColumnHelper = new ExcelColumnHelper(datatype);
            var titleList         = execlColumnHelper.GetPropertyDisplayNames(rm: rm);

            for (int cellNumber = 0; cellNumber < titleList.Count; cellNumber++)
            {
                row.CreateCell(cellNumber).SetCellValue(titleList[cellNumber]);
            }

            var numberOfColumns = 0;
            //Insert data values
            var rowNumber = 1;

            foreach (var row_item in dataList)
            {
                ///row
                var valueList = execlColumnHelper.GetPropertyValues(row_item);
                //var mergeRows = item.GetPropertyMergeRows();
                ///TODO if need merge columns
                //if (mergeRows > 1)
                //{
                //    worksheet.AddMergedRegion(new CellRangeAddress(rowNumber, rowNumber + mergeRows, rowNumber, rowNumber));
                //}
                var cellNumber  = 0;
                var new_rowData = true;
                foreach (var values in valueList)
                {
                    var tmpRow = worksheet.GetRow(rowNumber);
                    if (tmpRow == null)
                    {
                        tmpRow = worksheet.CreateRow(rowNumber);
                    }


                    foreach (var value in values)
                    {
                        var cell = tmpRow.CreateCell(cellNumber);
                        //cell.SetCellType(CellType.)
                        cell.SetCellValueByType(workbook, value);
                        numberOfColumns = cellNumber;
                        cellNumber++;
                    }
                    if (new_rowData)
                    {
                        new_rowData = false;
                    }
                    else
                    {
                        cellNumber = cellNumber - values.Count;
                        rowNumber++;
                    }
                }
                rowNumber++;
            }

            worksheet.Autobreaks = true;
            for (int i = 0; i <= numberOfColumns; i++)
            {
                worksheet.AutoSizeColumn(i);
            }

            return(worksheet);
        }