Wednesday, 8 August 2012

working with excel in .net


string[] entities = new string[]{"new_custom1","new_custom3","new_custom4"};


 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

            if (xlApp == null)
            {
                Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
                return;
            }
            xlApp.Visible = true;

            Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            //Worksheet ws = (Worksheet)wb.Worksheets[1];          

            RetrieveAllEntitiesResponse retrieveallEntitiesResponse = (RetrieveAllEntitiesResponse)service.Execute(retrieveEntityRequest);

            foreach (var entityMetadata in retrieveallEntitiesResponse.EntityMetadata)
            {
                if (entityMetadata.LogicalName.Contains("new") && entityMetadata.DisplayName.LocalizedLabels.Count > 0 && entities.Contains(entityMetadata.LogicalName))
                {

                    Microsoft.Office.Interop.Excel.Worksheet newWorksheet;
                    newWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    newWorksheet.Name = entityMetadata.DisplayName.LocalizedLabels[0].Label;
                    int columnIndex = 1;
                    foreach (var attribute in entityMetadata.Attributes)
                    {
                        if (attribute.LogicalName.Contains("new") && attribute.Description.LocalizedLabels.Count > 0 && !attribute.Description.LocalizedLabels[0].Label.ToLower().Contains("internal"))
                        {
                            newWorksheet.Cells[1, columnIndex].Value2 = attribute.DisplayName.LocalizedLabels[0].Label;
                            newWorksheet.Cells[1, columnIndex].AddComment(attribute.Description.LocalizedLabels[0].Label);

                            columnIndex++;
                        }
                    }
                }
            }

http://csharp.net-informations.com/excel/csharp-format-excel.htm

No comments:

Post a Comment