Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: set Background Excel cell color using POI API

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default set Background Excel cell color using POI API

    Hi,

    It's my first time to use the POI API,
    I'm trying to change the background color of a cell but it won't change,
    although all other features in the HSSFCellStyle I used like borders and alignment worked perfectly, here's the code:

    HSSFCellStyle titleStyle = xls_workbook.createCellStyle();
    titleStyle.setFillBackgroundColor(HSSFColor.GREY_2 5_PERCENT.index);
    titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
    titleStyle.setBorderBottom(CellStyle.BORDER_MEDIUM );
    titleStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
    titleStyle.setBorderRight(CellStyle.BORDER_MEDIUM) ;
    titleStyle.setBorderTop(CellStyle.BORDER_MEDIUM);

    Font titleFont = xls_workbook.createFont();
    titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    titleStyle.setFont(titleFont);

    /***********************************/
    HSSFRow row = xls_sheet1.createRow((short)0);
    HSSFCell cells[]=new HSSFCell[columns];

    for(int i=0;i<columns;i++)
    {
    cells[i]=row.createCell(i);
    cells[i].setCellStyle(titleStyle);
    }

    cells[0].setCellValue("Time Relative");
    cells[1].setCellValue("I/O");
    cells[2].setCellValue("Signal Name");
    cells[3].setCellValue("Value to be set/checked");
    cells[4].setCellValue("Time Tolerance");
    cells[5].setCellValue("Comparison Type");
    cells[6].setCellValue("Comment");

    for(int i=0;i<columns;i++)
    {
    xls_sheet1.autoSizeColumn((short)i);
    }


    xls_workbook.write(xlsFileOut);
    xlsFileOut.close();


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: set Background Excel cell color using POI API

    Try calling Cell.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND )
    FWIW I recall having an issue with this in that the background fill color resulted in a black cell - always. I can't remember the situation but if you experience this, I believe it to be a reported bug in POI. You can use the foreground color or use an XSSF sheet (which resolved the issue for me).
    Last edited by copeg; March 25th, 2011 at 07:07 PM.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: set Background Excel cell color using POI API

    Thanks for your help
    I tried to use the XSSF sheet instead as you suggested but I was faced with the following error even after adding log4J classpath

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
    at tsxml_xls_generator.Generator.<init>(Generator.jav a:58)
    at tsxml_xls_generator.Main.main(Main.java:21)
    Java Result: 1

Similar Threads

  1. Background Workjer
    By fortune2k in forum Threads
    Replies: 3
    Last Post: October 26th, 2010, 09:19 AM
  2. background
    By b109 in forum AWT / Java Swing
    Replies: 0
    Last Post: May 24th, 2010, 06:37 AM
  3. Help with background and objects
    By Afromiffo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2010, 01:19 AM
  4. Background image on GUI
    By OBLITERATOR in forum AWT / Java Swing
    Replies: 3
    Last Post: March 5th, 2010, 12:10 PM
  5. JButton set background problem
    By ellias2007 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 25th, 2010, 12:15 AM