How Convert Json string to Excel in Xamarin Forms

abvabv USMember ✭✭✭
edited September 2018 in Xamarin.Forms

Hi ,I am trying to convert Json data to Excel in xamarin forms for that I am trying like

        using (var client = new HttpClient())
        using (var formData = new MultipartFormDataContent())
        {
            var fileFormat = "xlsx";
            formData.Add(new StringContent("TestDocument"), "FileName");
            formData.Add(new StringContent(fileFormat), "FileFormat");
            formData.Add(new StringContent(JsonConvert.SerializeObject(jsonData)), "Data");
            //Call WebAPI to get Excel  
            var response = client.PostAsync("http://demos.componentone.com/ASPNET/C1WebAPIService/api/excel", formData).Result;
            if (!response.IsSuccessStatusCode)
            {
                DisplayAlert("Error!", "", "Ok");
                return;
            }
            var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
            if (!Directory.Exists(tempPath))
            {
                Directory.CreateDirectory(tempPath);
            }
            //Save Excel to Tem directory.  
            var tempFilePath = Path.Combine(tempPath, string.Format("{0}.{1}", "Test", fileFormat));
            string newFileName = string.Empty;
            using (var newFile = File.Create(tempFilePath))
            {
                response.Content.ReadAsStreamAsync().Result.CopyTo(newFile);
                newFileName = newFile.Name;
            }
            Task.Delay(1000);
            Process.Start(newFileName);
        }

It is converting Json to excel with no data.What I am doing wrong or is there any new way to covert can anyone help me for this?

Posts

  • AsposeAspose Member ✭✭
    edited July 26

    If you need to use file conversion, its better to do that with third-party libraries.
    I would suggest you Aspose.Cells to convert json to excel.
    Here is a small example:

    // Instantiating a Workbook object
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    
    // Read File
    string jsonInput = File.ReadAllText("test.json");
    
    // Set Styles
    CellsFactory factory = new CellsFactory();
    Style style = factory.CreateStyle();
    style.HorizontalAlignment = TextAlignmentType.Center;
    style.Font.IsBold = true;
    
    // Set JsonLayoutOptions
    JsonLayoutOptions options = new JsonLayoutOptions();
    options.TitleStyle = style;
    options.ArrayAsTable = true;
    
    // Import JSON Data
    JSONUtility.ImportData(jsonInput, worksheet.Cells, 0, 0, options);
    
    // Save Excel file
    workbook.Save(dataDir + "importingFromJson.out.xlsx");
    

    Note: I am working as Developer Evangelist at Aspose.

  • NMackayNMackay GBInsider, University mod

    For some balance, there may be alternatives (it has a free community licence)

    https://www.syncfusion.com/kb/6903/how-to-create-an-excel-file-in-xamarin

Sign In or Register to comment.