Search code examples
c#asp.net-web-api.net-6.0asposeaspose-cells

Set excel file headers to Pascal Case in Aspose .net 6


I've following code that maps given json array of objects to excel file. But it outputs the file with headers in lower case. I want the headers to be in Pascal Case. Can someone help or lead me to right direction? Either I should do it with json functions or aspose methods. I'm confused, as I couldn't find anything about header's cases for excel in documentation of Aspose. Thank you.

private async Task<string> JsonToExcel(string jsonString)
    {
        try
        {
            Workbook workbook = new();
            Worksheet worksheet = workbook.Worksheets[0];
            // Set JsonLayoutOptions
            JsonLayoutOptions options = new();
            options.ArrayAsTable = true;
            // Import JSON Data
            JsonUtility.ImportData(jsonString, worksheet.Cells, 0, 0, options);
            // Save Excel file
            Guid guid = Guid.NewGuid();
            string filename = DateTimeOffset.Now.ToString("yyyy_MM_dd") + guid.ToString() + ".xlsx";
            string path = _hostEnvironment.ContentRootPath + "Output\\";
            worksheet.PageSetup.PrintGridlines = true;
            worksheet.PageSetup.FitToPagesWide = 1;
            workbook.Save(path + filename, SaveFormat.Xlsx);
            return (path + filename);
        }
        catch (Exception)
        {
            throw;
        }
    }

As shown in output.output excel file

I tried to set headers to camel case using reference of PageSetup of Worksheet as shown in the documentation but nothing related to pascal case :(

// Instantiating a Workbook object
Workbook excel = new Workbook();
// Obtaining the reference of the PageSetup of the worksheet
PageSetup pageSetup = excel.Worksheets[0].PageSetup;
// Setting worksheet name at the left section of the header
pageSetup.SetHeader(0, "&A");

Solution

  • You may easily accomplish the task via TextInfo.ToTitleCase(String) method of System.Globalization namespace. This method will convert string to sort of Pascal Case for your needs. So, once you import data into the worksheet via Aspose.Cells APIs, you may re-insert the text (in the first row) into the cells after using System.Globalization APIs. See the sample code segment that you may add before saving to Excel spreadsheet for your reference:

    e.g.

    Sample code:

    ...
    // Creates a TextInfo based on the "en-US" culture.
    TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
    
    //Get the first (header) row
    Row row = worksheet.Cells.Rows[0];
    
    IEnumerator ie = row.GetEnumerator();
    while (ie.MoveNext())
    {
        Cell cell1 = (Cell)ie.Current;
        string stringValue = cell1.StringValue;
        stringValue = myTI.ToTitleCase(stringValue);
         cell1.PutValue(stringValue);
    }
    ....
    

    Hope, this helps.

    You may also post your queries or comments in dedicated forums.

    PS. I am working as Support developer/ Evangelist at Aspose.