Search code examples
c#.netasposeaspose-cells

Change data type of excel Cell using Aspose?


We are to show data with Currency symbol ("TRL") in Excel cell with Currency type and we write the excel cell using this data "TRL 100.00" then that cell automatically converted in to General type instead of Currency type although we have changed the format of the particular cell using (styleFlag's property NumberFormat=true)


Solution

  • Please see the following sample code, its comments and the screenshot showing the output Excel file. The code first formats the cell A1 with TRL currency format. Then it formats the entire column C with TRL currency format.

    C#

    //Create workbook
    Workbook wb = new Workbook();
    
    //Access first worksheet
    Worksheet ws = wb.Worksheets[0];
    
    //Add some value in cell A1
    Cell cell = ws.Cells["A1"];
    cell.PutValue(22);
    
    //Format cell A1 with TRL formatting
    Style st = cell.GetStyle();
    st.Custom = "[$TRL]\\ #,##0.00";
    cell.SetStyle(st);
    
    //Make the entire column C with TRL formatting
    StyleFlag flg = new StyleFlag();
    flg.NumberFormat = true;
    ws.Cells.Columns[2].ApplyStyle(st, flg);
    
    //Now enter data in column C cells
    ws.Cells["C5"].PutValue(31);
    ws.Cells["C6"].PutValue(32);
    ws.Cells["C7"].PutValue(33);
    ws.Cells["C8"].PutValue(34);
    
    //Save the workbook
    wb.Save("output.xlsx");
    

    Screenshot: enter image description here

    Update - I

    Screenshot showing the result of this following line.

    st.Custom = "[$฿-41E]#,##0.00";
    

    enter image description here

    Note: I am working as Developer Evangelist at Aspose