Thursday, April 25, 2013

C# how to generate Excel sheet from class dll using Reflection.

http://www.codeguru.com/csharp/csharp/cs_misc/reflection/article.php/c4257/An-Introduction-to-Reflection-in-C.htm


code 
make class ExcelUtility:::::::

    class ExcelUtility
    {
        private Application app = null;
        private Workbook workbook = null;
        private Worksheet worksheet = null;
        private Range workSheet_range = null;
    
        public ExcelUtility()
        {
            createDoc();
        }
        public void createDoc()
        {
            try
            {
                app = new Application();
                app.Visible = true;
                workbook = app.Workbooks.Add(1);
                worksheet = (Worksheet)workbook.Sheets[1];
            }
            catch (Exception e)
            {
                Console.Write("Error");
            }
            finally
            {
            }
        }

        public void createHeaders(int row, int col, string htext, string cell1,
        string cell2, int mergeColumns, string b, bool font, int size, string
        fcolor)
        {
            worksheet.Cells[row, col] = htext;
            workSheet_range = worksheet.get_Range(cell1, cell2);
            workSheet_range.Merge(mergeColumns);
            switch (b)
            {
                case "YELLOW":
                    workSheet_range.Interior.Color = System.Drawing.Color.Yellow.ToArgb();
                    break;
                case "GRAY":
                    workSheet_range.Interior.Color = System.Drawing.Color.Gray.ToArgb();
                    break;
                case "GAINSBORO":
                    workSheet_range.Interior.Color =
            System.Drawing.Color.Gainsboro.ToArgb();
                    break;
                case "Turquoise":
                    workSheet_range.Interior.Color =
            System.Drawing.Color.Turquoise.ToArgb();
                    break;
                case "PeachPuff":
                    workSheet_range.Interior.Color =
            System.Drawing.Color.PeachPuff.ToArgb();
                    break;
                default:
                    //  workSheet_range.Interior.Color = System.Drawing.Color..ToArgb();
                    break;
            }

            workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
            workSheet_range.Font.Bold = font;
            workSheet_range.ColumnWidth = size;
            if (fcolor.Equals(""))
            {
                workSheet_range.Font.Color = System.Drawing.Color.White.ToArgb();
            }
            else
            {
                workSheet_range.Font.Color = System.Drawing.Color.Black.ToArgb();
            }
        }

        public void addData(int row, int col, string data,
            string cell1, string cell2, string format)
        {
            worksheet.Cells[row, col] = data;
            workSheet_range = worksheet.get_Range(cell1, cell2);
            workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
            workSheet_range.NumberFormat = format;
        }

        public void addStringData(int row, int col, string data)
        {
          //  CellStyle cs = wb.createCellStyle();
            worksheet.Cells[row, col] = data;
         }
    }





Class 2 :: Program
namespace ConsoleApplication1
{
    class Program
    {
        static void Main1()
        {
            int currentRow = 1;

            int columnForInterface = 1;
            int columnForMethod = columnForInterface + 1;
            int columnForParameter = columnForMethod + 1;
            int columnForProperties = columnForParameter + 1;

            ExcelUtility excell_app = new ExcelUtility();

            excell_app.createHeaders(currentRow, columnForInterface, "Interface_name", "A1", "A1", 1, "YELLOW", true, 10, "n");
            excell_app.createHeaders(currentRow, columnForMethod, "Public Method", "B1", "B1", 1, "YELLOW", true, 10, "n");
            excell_app.createHeaders(currentRow, columnForParameter, "Parameters", "C1", "C1", 1, "YELLOW", true, 10, "n");
            excell_app.createHeaders(currentRow, columnForProperties, "Extended Parameters", "D1", "D1", 1, "YELLOW", true, 10, "n");

            currentRow = currentRow + 1;

            Assembly serviceAssembly = Assembly.LoadFrom("xyz.dll");
            Assembly viewDTOAssembly = Assembly.LoadFrom("abc.dll");

            Type[] serviceAssemblyTypeCollection = serviceAssembly.GetTypes();
            Type[] viewDTOAssemblyTypeCollection = serviceAssembly.GetTypes();

            foreach (Type serviceAssemblyType in serviceAssemblyTypeCollection)
            {
                if (!serviceAssemblyType.IsInterface)
                    continue;

                // ------------------------------
                excell_app.addStringData(currentRow, columnForInterface, serviceAssemblyType.Name);
                // ------------------------------

                MethodInfo[] methods = serviceAssemblyType.GetMethods();
                string methodNames = string.Empty;
                string parameterNames = string.Empty;
                string parameterTypes = string.Empty;
                foreach (MethodInfo method in methods)
                {
                    Console.WriteLine("\t{0}", method.Name);
                    methodNames += method.Name + "\n";
                    // ------------------------------
                    excell_app.addStringData(currentRow, columnForMethod, method.Name);
                    // ------------------------------

                    bool methodHasParameters = false;

                    ParameterInfo[] pinfo = method.GetParameters();
                    foreach (ParameterInfo parameterInfo in pinfo)
                    {
                        Type parameterType = parameterInfo.ParameterType;
                        if (parameterType.IsGenericType)
                        {
                            PropertyInfo[] propertyInfoCollection = parameterType.GetProperties();

                            foreach (PropertyInfo info in propertyInfoCollection)
                            {
                                string fullName = info.PropertyType.FullName;
                                Type typeInViewDTO = viewDTOAssembly.GetType(fullName);

                                if (typeInViewDTO == null)
                                {
                                    Console.WriteLine("Type Not Found in View Dto Assembly");
                                   
                                }
                                else
                                {
                                    methodHasParameters = true;
                                    // ------------------------------
                                    //excell_app.addStringData(currentRow, columnForParameter, fullName);
                                    excell_app.addStringData(currentRow, columnForMethod, method.Name);
                                    excell_app.addStringData(currentRow, columnForParameter, fullName + "::" + parameterInfo.Name);
                                    currentRow = currentRow + 1;
                                    // ------------------------------

                                    //Console.WriteLine("\t\t\t{0}", parameterType);
                                    //parameterTypes += parameterType + "\n";
                                    //parameterNames += parameterInfo.Name + "\n";
                                }
                            }
                        }
                        else
                        {
                            string fullName = parameterType.FullName;
                            Type typeInViewDTO = viewDTOAssembly.GetType(fullName);

                            if (typeInViewDTO == null)
                            {
                                excell_app.addStringData(currentRow, columnForMethod, method.Name);
                                excell_app.addStringData(currentRow, columnForParameter, fullName + "::" + parameterInfo.Name);
                                currentRow = currentRow + 1;
                                Console.WriteLine("Type Not Found in View Dto Assembly");
                            }
                            else
                            {
                                //PropertyInfo[] arrayOfProperties = typeInViewDTO.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                                PropertyInfo[] arrayOfProperties = typeInViewDTO.GetPublicProperties();
                                if (arrayOfProperties == null || arrayOfProperties.Length == 0)
                                {
                                    excell_app.addStringData(currentRow, columnForMethod, method.Name);
                                    excell_app.addStringData(currentRow, columnForParameter, fullName + "::" + parameterInfo.Name);
                                    currentRow = currentRow + 1;
                                    continue;
                                }
                                else
                                {
                                    StringBuilder propertyBuilder = new StringBuilder();
                                    foreach (PropertyInfo propertyOfDTO in arrayOfProperties)
                                    {
                                        propertyBuilder.Append(propertyOfDTO.Name);
                                        propertyBuilder.Append("\n");
                                    }
                                    excell_app.addStringData(currentRow, columnForMethod, method.Name);
                                    excell_app.addStringData(currentRow, columnForParameter, fullName + "::" + parameterInfo.Name);
                                    excell_app.addStringData(currentRow, columnForProperties, propertyBuilder.ToString());
                                    currentRow = currentRow + 1;
                                }
                               
                                //Console.WriteLine("\t\t\t{0}", parameterType);
                                //parameterTypes += parameterType + "\n";
                                //parameterNames += parameterInfo.Name + "\n";
                            }
                            methodHasParameters = true;
                        }
                    }

                    if (!methodHasParameters)
                    {
                        excell_app.addStringData(currentRow, columnForParameter, "------------");
                        currentRow = currentRow + 1;
                    }
                   
                }


                // ------------------------------

                //if (methodNames.EndsWith("\n"))
                //    methodNames = methodNames.Substring(0, methodNames.Length - 2);
                //excell_app.addStringData(classCount, methodCount, methodNames);
                //if (parameterNames.EndsWith("\n"))
                //    parameterNames = parameterNames.Substring(0, parameterNames.Length - 2);
                //excell_app.addStringData(classCount, methodCount + 1, parameterTypes + parameterNames);

                // ------------------------------

                currentRow = currentRow + 2;
               

            }
            Console.ReadLine();
        }
    }



    public static class MyExtensions
    {
        public static PropertyInfo[] GetPublicProperties(this Type type)
        {
            if (type.IsInterface)
            {
                var propertyInfos = new List<PropertyInfo>();

                var considered = new List<Type>();
                var queue = new Queue<Type>();
                considered.Add(type);
                queue.Enqueue(type);
                while (queue.Count > 0)
                {
                    var subType = queue.Dequeue();
                    foreach (var subInterface in subType.GetInterfaces())
                    {
                        if (considered.Contains(subInterface)) continue;

                        considered.Add(subInterface);
                        queue.Enqueue(subInterface);
                    }

                    var typeProperties = subType.GetProperties(
                        BindingFlags.FlattenHierarchy
                        | BindingFlags.Public
                        | BindingFlags.Instance);

                    var newPropertyInfos = typeProperties.Where(x => !propertyInfos.Contains(x));

                    propertyInfos.InsertRange(0, newPropertyInfos);
                }

                return propertyInfos.ToArray();
            }

            return type.GetProperties(BindingFlags.FlattenHierarchy
                | BindingFlags.Public | BindingFlags.Instance);
        }
    }
}

Creating mirror of BST