Inheritance: System.Attribute
Esempio n. 1
0
        protected virtual void ExecuteExport <T>(IEnumerable <T> collection)
        {
            if (InstalledExcel)
            {
                #region Initialization
                Microsoft.Office.Interop.Excel.Application xlexcel;
                Microsoft.Office.Interop.Excel.Workbook    xlWorkBook;
                Microsoft.Office.Interop.Excel.Worksheet   xlWorkSheet;
                Object misValue = System.Reflection.Missing.Value;
                xlexcel          = new Microsoft.Office.Interop.Excel.Application();
                xlWorkBook       = xlexcel.Workbooks.Add(misValue);
                xlWorkSheet      = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                xlWorkSheet.Name = typeof(T).Name;

                int xlRowIndex    = START_ROW_INDEX;
                int xlColumnIndex = START_COLUMN_INDEX;
                #endregion
                var properties = typeof(T).GetProperties();
                List <PropertyInfo> valiedProperties = new List <PropertyInfo>();
                foreach (PropertyInfo property in properties)
                {
                    ExcelAttribute attribute = property.GetCustomAttributes(typeof(ExcelAttribute), true).FirstOrDefault() as ExcelAttribute;
                    if (attribute != null)
                    {
                        if (!attribute.IsDisplay)
                        {
                            continue;
                        }
                        valiedProperties.Add(property);
                    }
                }

                #region Generation Column Header

                foreach (var item in valiedProperties)
                {
                    var headerName = (item.GetCustomAttributes(typeof(ExcelAttribute), true).FirstOrDefault() as ExcelAttribute).HeaderName;
                    if (String.IsNullOrEmpty(headerName))
                    {
                        headerName = item.Name;
                    }
                    xlWorkSheet.Cells[xlRowIndex, xlColumnIndex].Font.Bold      = true;
                    xlWorkSheet.Cells[xlRowIndex, xlColumnIndex].Font.Color     = OLE_HEADER_FONT_COLOR;
                    xlWorkSheet.Cells[xlRowIndex, xlColumnIndex].Interior.Color = OLE_HEADER_CELL_COLOR;
                    xlWorkSheet.Cells[xlRowIndex, xlColumnIndex] = headerName;
                    xlColumnIndex++;
                }
                #endregion
                #region Generation Data Row
                xlRowIndex++;
                foreach (var item in collection)
                {
                    xlColumnIndex = START_COLUMN_INDEX;
                    foreach (var property in valiedProperties)
                    {
                        var value = property.GetValue(item, null);
                        if (value == null)
                        {
                            value = String.Empty;
                        }
                        xlWorkSheet.Cells[xlRowIndex, xlColumnIndex] = value.ToString();
                        if (xlRowIndex % 2 == 0)
                        {
                            xlWorkSheet.Cells[xlRowIndex, xlColumnIndex].Interior.Color = OLE_EVEN_ROW_CELL_COLOR;
                        }
                        else
                        {
                            xlWorkSheet.Cells[xlRowIndex, xlColumnIndex].Interior.Color = OLE_ODD_ROW_CELL_COLOR;
                        }
                        xlColumnIndex++;
                    }
                    xlRowIndex++;
                }
                #endregion
                xlexcel.Visible = true;
                xlWorkSheet.Columns.AutoFit();
                OnExportCompleted(collection);
            }
        }
Esempio n. 2
0
        public override void Export <T>(IEnumerable <T> collection)
        {
            var properties = typeof(T).GetProperties();
            List <PropertyInfo> valiedProperties = new List <PropertyInfo>();

            foreach (PropertyInfo property in properties)
            {
                ExcelAttribute attribute = property.GetCustomAttributes(typeof(ExcelAttribute), true).FirstOrDefault() as ExcelAttribute;
                if (attribute != null)
                {
                    if (!attribute.IsDisplay)
                    {
                        continue;
                    }
                    valiedProperties.Add(property);
                }
            }

            StringBuilder output = new StringBuilder();

            #region Generation Column Header
            foreach (var item in valiedProperties)
            {
                var headerName = (item.GetCustomAttributes(typeof(ExcelAttribute), true).FirstOrDefault() as ExcelAttribute).HeaderName;
                if (String.IsNullOrEmpty(headerName))
                {
                    headerName = item.Name;
                }

                output.Append(headerName);
                if (valiedProperties.IndexOf(item) != valiedProperties.IndexOf(valiedProperties.Last()))
                {
                    output.Append(DELIMITER);
                }
            }
            output.AppendLine();
            #endregion

            #region Generation Data Row
            foreach (var item in collection)
            {
                foreach (var property in valiedProperties)
                {
                    var value = property.GetValue(item, null);
                    if (value == null)
                    {
                        value = String.Empty;
                    }
                    output.Append(value);
                    if (valiedProperties.IndexOf(property) != valiedProperties.IndexOf(valiedProperties.Last()))
                    {
                        output.Append(DELIMITER);
                    }
                }
                output.AppendLine();
            }
            #endregion

            WriteToFile(output.ToString());
            OnExportCompleted(collection);
        }