Setting Styles/Formats for Columns in Excel

Last post 02-09-2009, 2:59 PM by nausherwan.aslam. 6 replies.
Sort Posts: Previous Next
  •  11-10-2008, 6:57 PM 151853

    Setting Styles/Formats for Columns in Excel

    Hi,  I need to copy DateTime format from data grid into Excel. When I attempted to copy a DateTime column into Excel, I've noticed that the DateTime format is not reflecting in Excel.

    Just wondering, How can I set the Column type in Excel.

    Regards,
     
  •  11-10-2008, 9:35 PM 151863 in reply to 151853

    Re: Setting Styles/Formats for Columns in Excel

    Hi,

    Thank you for considering Aspose.

    You can use Cells.ApplyColumnStyle to assign the display format/Style of a Column. Following Sample code will help you get you desired results

    Sample code:

    Workbook wb = new Workbook();
     Worksheet sheet = wb.Worksheets[0];
     Style style1;
     StyleFlag flag1;
     style1 = wb.Styles[wb.Styles.Add()]; 
     style1.Custom = "yyyy-mm-dd";
    //your can also user style1.Number instead of style1.Cutom for predefine formats see the link
    // below for the detailed list of predefined Format Style 
    flag1 = new StyleFlag();
    flag1.NumberFormat = true;
    //Apply style to the first column.
    sheet.Cells.ApplyColumnStyle(0,style1, flag1);
    wb.Save("d:\\test\\rowandcolumn_formattings.xls");

    Following is the list of all predefined formats supported by Aspose.Cells,

    http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/setting-display-formats-of-numbers-dates.html

    Thank you & Best Regards,


    Nausherwan Aslam
    Support Developer,
    Aspose Sialkot Team
    Contact Us
     
  •  02-09-2009, 12:31 PM 164203 in reply to 151863

    Re: Setting Styles/Formats for Columns in Excel

    hi there

    im having problems with "ApplyColumnStyle" function... i used the exact same code as its posted on this thread and it doesnt work... but if instead of i just change the style of a specific cell it works....

    Aspose.Cells.Style style;
    StyleFlag flag;
    style = excelWorkbook.Styles[excelWorkbook.Styles.Add()];
    style.Name = "TESTE";
    style.Custom = "dd/mm/YYYY hh:mm:ss AM/PM";
    style.Number = 49;
    style.Font.Color = Color.Red;
    flag = new StyleFlag();
    flag.NumberFormat = false;

    //excelWorksheet.Cells.ApplyColumnStyle(1, style, flag); DOES NOT WORK

    //excelWorksheet.Cells[1,1].Style = style; WORKS!!!!

    i don't know why this happens...

     
  •  02-09-2009, 12:31 PM 164204 in reply to 151863

    Re: Setting Styles/Formats for Columns in Excel

    hi there

    im having problems with "ApplyColumnStyle" function... i used the exact same code as its posted on this thread and it doesnt work... but if instead of i just change the style of a specific cell it works....

    Aspose.Cells.Style style;
    StyleFlag flag;
    style = excelWorkbook.Styles[excelWorkbook.Styles.Add()];
    style.Name = "TESTE";
    style.Custom = "dd/mm/YYYY hh:mm:ss AM/PM";
    style.Number = 49;
    style.Font.Color = Color.Red;
    flag = new StyleFlag();
    flag.NumberFormat = false;

    //excelWorksheet.Cells.ApplyColumnStyle(1, style, flag); DOES NOT WORK

    //excelWorksheet.Cells[1,1].Style = style; WORKS!!!!

    i don't know why this happens...

    can you guys help me?

    TIA,
    Pedro

     
  •  02-09-2009, 1:04 PM 164208 in reply to 164204

    Re: Setting Styles/Formats for Columns in Excel

    Hi Pedro,

    Thank you for considering Aspose.

    Please change the value of flag.NumberFormat to true to apply the Number format style on the column. Please note that whenever you are going to set the display format style of the column / row (to specific number or custom), you have to make the flag.NumberFormat property as true.

    Thank You & Best Regards,


    Nausherwan Aslam
    Support Developer,
    Aspose Sialkot Team
    Contact Us
     
  •  02-09-2009, 1:40 PM 164213 in reply to 164208

    Re: Setting Styles/Formats for Columns in Excel

    doesn´t work... im not even testing the format...

    im testing the color... it works if the target is a SINGLE CELL.... if the target is ENTIRE COLUMN, doesn't work....

    dunno what to do

    TIA,
    Pedro
     
  •  02-09-2009, 2:59 PM 164223 in reply to 164213

    Re: Setting Styles/Formats for Columns in Excel

    Hi Pedro,

    Thank you for considering Aspose.

    For applying font color, please use the flag.FontColor = true; before applying the style to the column. Please see the modified sample code as following,

    Sample Code:

    Aspose.Cells.Style style;

    StyleFlag flag;

    style = excelWorkbook.Styles[excelWorkbook.Styles.Add()];

    style.Name = "TESTE";

    style.Custom = "dd/mm/YYYY hh:mm:ss AM/PM";

    style.Number = 49;

    style.Font.Color = Color.Red;

    flag = new StyleFlag();

    //Flag to apply Number format

    flag.NumberFormat = true;

    //Flag to apply Font Color

    flag.FontColor = true;

     

    Thank You & Best Regards,


    Nausherwan Aslam
    Support Developer,
    Aspose Sialkot Team
    Contact Us
     
View as RSS news feed in XML