public async Task <HttpResponseMessage> GetSpreadsheet([FromBody] RequestModel request)
        {
            Guid testOutput;

            if (string.IsNullOrWhiteSpace(request.guid) || !Guid.TryParse(request.guid.TrimStart('t'), out testOutput))
            {
                throw new System.Exception("Invalid GUID");
            }

            // authenticate with Forge
            dynamic oauth = await Get2LeggedTokenAsync(new Scope[] { Scope.DataRead });

            // get object on the bucket, should be just 1
            ObjectsApi objects = new ObjectsApi();

            objects.Configuration.AccessToken = oauth.access_token;
            dynamic objectsInBucket = await objects.GetObjectsAsync(request.guid);

            string objectId  = string.Empty;
            string objectKey = string.Empty;

            foreach (KeyValuePair <string, dynamic> objInfo in new DynamicDictionaryItems(objectsInBucket.items))
            {
                objectId  = objInfo.Value.objectId;
                objectKey = objInfo.Value.objectKey;
            }

            string xlsFileName = objectKey.Replace(".rvt", ".xls");
            var    xlsPath     = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"), request.guid, xlsFileName);

            if (File.Exists(xlsPath))
            {
                return(SendFile(xlsPath));// if the Excel file was already generated
            }
            DerivativesApi derivative = new DerivativesApi();

            derivative.Configuration.AccessToken = oauth.access_token;

            // get the derivative metadata
            dynamic metadata = await derivative.GetMetadataAsync(objectId.Base64Encode());

            foreach (KeyValuePair <string, dynamic> metadataItem in new DynamicDictionaryItems(metadata.data.metadata))
            {
                dynamic hierarchy = await derivative.GetModelviewMetadataAsync(objectId.Base64Encode(), metadataItem.Value.guid);

                dynamic properties = await derivative.GetModelviewPropertiesAsync(objectId.Base64Encode(), metadataItem.Value.guid);

                Workbook xls = new Workbook();
                foreach (KeyValuePair <string, dynamic> categoryOfElements in new DynamicDictionaryItems(hierarchy.data.objects[0].objects))
                {
                    string    name  = categoryOfElements.Value.name;
                    Worksheet sheet = new Worksheet(name);
                    for (int i = 0; i < 100; i++)
                    {
                        sheet.Cells[i, 0] = new Cell("");                 // unless we have at least 100 cells filled, Excel understand this file as corrupted
                    }
                    List <long> ids = GetAllElements(categoryOfElements.Value.objects);
                    int         row = 1;
                    foreach (long id in ids)
                    {
                        Dictionary <string, object> props = GetProperties(id, properties);
                        int collumn = 0;
                        foreach (KeyValuePair <string, object> prop in props)
                        {
                            sheet.Cells[0, collumn]   = new Cell(prop.Key.ToString());
                            sheet.Cells[row, collumn] = new Cell(prop.Value.ToString());
                            collumn++;
                        }

                        row++;
                    }

                    xls.Worksheets.Add(sheet);
                }
                xls.Save(xlsPath);
            }
            return(SendFile(xlsPath));
        }
示例#2
0
        public async Task ExcelObject([FromBody] ObjectModel objModel)
        {
            // authenticate with Forge Must have data read scope
            dynamic oauth = await OAuthController.GetInternalAsync();

            // get the user selected object
            ObjectsApi objects = new ObjectsApi();

            objects.Configuration.AccessToken = oauth.access_token;
            dynamic selectedObject = await objects.GetObjectDetailsAsync(objModel.bucketKey, objModel.objectName);

            string objectId  = selectedObject.objectId;
            string objectKey = selectedObject.objectKey;


            string xlsFileName = objectKey.Replace(".rvt", ".xls");
            var    xlsPath     = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"), objModel.bucketKey, xlsFileName);//.guid, xlsFileName);
            ///////////if (File.Exists(xlsPath))
            ///////////    return SendFile(xlsPath);// if the Excel file was already generated

            DerivativesApi derivative = new DerivativesApi();

            derivative.Configuration.AccessToken = oauth.access_token;

            // get the derivative metadata
            dynamic metadata = await derivative.GetMetadataAsync(objectId.Base64Encode());

            foreach (KeyValuePair <string, dynamic> metadataItem in new DynamicDictionaryItems(metadata.data.metadata))
            {
                dynamic hierarchy = await derivative.GetModelviewMetadataAsync(objectId.Base64Encode(), metadataItem.Value.guid);

                dynamic properties = await derivative.GetModelviewPropertiesAsync(objectId.Base64Encode(), metadataItem.Value.guid);

                Workbook xls = new Workbook();
                foreach (KeyValuePair <string, dynamic> categoryOfElements in new DynamicDictionaryItems(hierarchy.data.objects[0].objects))
                {
                    string    name  = categoryOfElements.Value.name;
                    Worksheet sheet = new Worksheet(name);
                    for (int i = 0; i < 100; i++)
                    {
                        sheet.Cells[i, 0] = new Cell("");                           // unless we have at least 100 cells filled, Excel understand this file as corrupted
                    }
                    List <long> ids = GetAllElements(categoryOfElements.Value.objects);
                    int         row = 1;
                    foreach (long id in ids)
                    {
                        Dictionary <string, object> props = GetProperties(id, properties);
                        int collumn = 0;
                        foreach (KeyValuePair <string, object> prop in props)
                        {
                            sheet.Cells[0, collumn]   = new Cell(prop.Key.ToString());
                            sheet.Cells[row, collumn] = new Cell(prop.Value.ToString());
                            collumn++;
                        }

                        row++;
                    }

                    xls.Worksheets.Add(sheet);
                }


                //Where to save the excel file
                string pathUser     = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
                string pathDownload = Path.Combine(pathUser, "Downloads", xlsFileName);

                //try catch save the file to the relevant place
                try
                {
                    var fstream = new System.IO.FileStream(pathDownload, FileMode.CreateNew);
                    xls.SaveToStream(fstream);
                }
                catch (Exception e)
                {
                    Debug.Print("Exception when calling ObjectsApi.DownloadObject: " + e.Message);
                }
            }
            //No need to return anything
            //return SendFile(xlsPath);
        }