Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

package 

com.bethecoder.tutorials.jexcelapi.write;

import java.io.File;
import java.io.IOException;

import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.format.Colour;
import jxl.format.Pattern;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class CellBackgroundPatternTest {

  /**
   * @param args
   * @throws IOException 
   * @throws IOException 
   * @throws WriteException 
   * @throws BiffException 
   */
  public static void main(String[] args) throws IOException, WriteExceptio
n {
    //Creates a writable workbook with the given file name
    WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/JXL/
BGPattern.xls"));
    
    WritableSheet sheet = workbook.createSheet("My Sheet", 0);
    
    // Create the label, specifying content and format
    Label label = new Label(1, 2, "ABC", getCellFormat(Colour.GREEN, Patte
rn.GRAY_25));
    Label label2 = new Label(1, 4, "PQR", getCellFormat(Colour.BLUE, Patte
rn.GRAY_50));
    Label label3 = new Label(1, 6, "XYZ", getCellFormat(Colour.ORANGE, Pat
tern.GRAY_75));
    
    sheet.addCell(label);
    sheet.addCell(label2);
    sheet.addCell(label3); 
    
    //Writes out the data held in this workbook in Excel format
    workbook.write(); 
    
    //Close and free allocated memory 
    workbook.close(); 
  }
  
/*******************************With Bold*****************************/

public static WritableCellFormat getCellFormat(Colour colour, Pattern pattern)


throws WriteException {
/// WritableFont cellFont = new WritableFont(WritableFont.BOLD.ARIAL, 12);
WritableFont cellFont = new
WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);
WritableCellFormat cellFormat = new WritableCellFormat(cellFont);
cellFormat.setBackground(colour, pattern);

return cellFormat;
}

/*********************************Without Bold**********************/

public static WritableCellFormat getCellFormat2(Colour colour, Pattern pattern)


throws WriteException {
WritableFont cellFont = new
WritableFont(WritableFont.createFont("calibri"), 11);
// WritableFont cellFont = new
WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);
WritableCellFormat cellFormat = new
WritableCellFormat(cellFont);
cellFormat.setBackground(colour, pattern);

return cellFormat;
}

/******************************************************************/

}
   
It gives the following output, 

You might also like