Image stretched in Excel using POI - java

In my application I'm generating an Excel file using the POI API. One of the cells, the top left, of the document holds an image. The problem I'm getting is that the image is being stretched to fill the cell that holds it.
Depending on which resize-methods I use on the Picture object the image appears in various sizes but it always has the horizontal-vertical ratio that the cell which it is inside has, in other words it won't keep its own ratio.
This is my code:
titleRow = sheet.createRow(0);
titleRow.setHeightInPoints(25f);
titleRow.createCell(0);
sheet.getRow(0).getCell(0).setCellStyle(defaultTitleStyle(wb));
WebApplicationContext webCtx = ((WebApplicationContext)AtlasApplicationUtils.getCurrentApplication().getContext());
ServletContext sc = webCtx.getHttpSession().getServletContext();
String contextPath = sc.getContextPath();
// sc.getResourceAsStream(contextPath + "/VAADIN/themes/m2m/../m2m/img/gnd_logo_white.png")
String f = new File("").getAbsolutePath();
InputStream is = new FileInputStream(System.getProperty("user.dir") + "/src/main/webapp/VAADIN/themes/m2m/img/gnd_logo_white.png");
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
is.close();
CreationHelper helper = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(0);
anchor.setRow1(0);
sheet.autoSizeColumn(0);
Picture pict = drawing.createPicture(anchor, pictureIdx);
pict.getPreferredSize();
pict.resize(0.25);
The styling method:
protected CellStyle defaultTitleStyle(final HSSFWorkbook wb) {
CellStyle style;
final Font titleFont = wb.createFont();
titleFont.setFontHeightInPoints((short) 18);
titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
titleFont.setColor(IndexedColors.GREY_80_PERCENT.getIndex());
style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
// style.setFillBackgroundColor(IndexedColors.BLUE_GREY.getIndex());
style.setFont(titleFont);
HSSFPalette palette = wb.getCustomPalette();
palette.setColorAtIndex(HSSFColor.BLUE_GREY.index,
(byte) 7,
(byte) 64,
(byte) 127);
palette.setColorAtIndex(HSSFColor.GREY_80_PERCENT.index,
(byte) 228,
(byte) 228,
(byte) 228);
return style;
Does anyone know of a way to avoid this problem and let the image keep its original ratio?

There is a bit of warning in the documentation.
void resize()
Reset the image to the original size.
Please note, that this method works correctly only for workbooks with
default font size (Arial 10pt for .xls and Calibri 11pt for .xlsx). If
the default font is changed the resized image can be streched
vertically or horizontally.

I think you can try to add
anchor.setAnchorType(ClientAnchor.DONT_MOVE_AND_RESIZE);

Related

Apache POI write image and text excel

I am having trouble with writing image and text in a same cell and questions similar to StackOverflow
on addding image and text in same cell in excel and POI Excel HSSFPicture Image and ALT TEXT
, but the expected output is different and I cannot figure out what wrong with my code?
and expected output like as below
Here is my code;
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
//FileInputStream obtains input bytes from the image file
InputStream inputStream = new FileInputStream(k_pipe_img_file);
//Get the contents of an InputStream as a byte[].
byte[] bytes = IOUtils.toByteArray(inputStream);
//Adds a picture to the workbook
int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
//close the input stream
inputStream.close();
//Returns an object that handles instantiating concrete classes
CreationHelper helper = workbook.getCreationHelper();
//Creates the top-level drawing patriarch.
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor1 = new XSSFClientAnchor();
anchor1.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
anchor1.setCol1(1);
anchor1.setCol2(1);
anchor1.setRow1(2);
anchor1.setRow2(2);
//Creates a picture
Picture pict = drawing.createPicture(anchor1, pictureIdx);
pict.resize(1, 1);
Row row = sheet.createRow(2);
row.setHeight((short) 4000);
sheet.setColumnWidth(0, 4000);
Cell cell = row.createCell(0, CellType.STRING);
cell.setCellValue("Task 1");
sheet.setColumnWidth(1, 5000);
cell = row.createCell(1, CellType.STRING);
cell.setCellValue("Replace Kemplon-Pipe");
CellStyle style=row.getSheet().getWorkbook().createCellStyle();
style.setVerticalAlignment(VerticalAlignment.TOP);
cell.setCellStyle(style);
//Write the Excel file
FileOutputStream fileOut = new FileOutputStream(k_Task_file);
workbook.write(fileOut);
fileOut.close();
Code Output:
image occupies entire cell and text is behind the image.
Is there any possible solution?
In Excel sheets pictures are not in cells but hovers in a layer over the cells. They are anchored to the cells in the following manner:
A one cell anchor determines the upper left position of the picture. If used, the picture must be resized to its native size.
A two cell anchor determines the upper left position and the size of the picture. The first anchor determines the upper left position while the second anchor determines the bottom right position. So the size of the picture is given.
Each anchor can have a row and column given but also a dx and dy. The dx and dy will be added to the column's and row's position to determine the final position. The measurement unit for dx and dy is EMU. Apache poi provides org.apache.poi.util.Units to calculate EMU from points or pixels for example.
One of the problems is that apache poi has a method to get the width of a column in pixels while it only has a method to get the height of the row in points. So we have two different measurement units to be considered.
Another problem is that if we wants to respect the aspect ratio of the picture, then we need determining the native size of the picture file first. There are many questions/answers how to get the native size of a jpg file in Java. All answers are very complex and none is really good in my opinion.
The following example does positioning a picture over cell B3 anchored 20px from left cell border, 20pt from top cell border, in width up to 20px from right cell border and in height up to 10pt from bottom cell border.
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;
public class ExcelDrawImagesOverCell {
private static void drawImageOnExcelSheet(XSSFSheet sheet, int row, int col,
int left/*in px*/, int top/*in pt*/, int width/*in px*/, int height/*in pt*/, int pictureIdx) throws Exception {
CreationHelper helper = sheet.getWorkbook().getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setAnchorType(AnchorType.MOVE_AND_RESIZE);
anchor.setCol1(col); //first anchor determines upper left position
anchor.setRow1(row);
anchor.setDx1(Units.pixelToEMU(left)); //dx = left in px
anchor.setDy1(Units.toEMU(top)); //dy = top in pt
anchor.setCol2(col); //second anchor determines bottom right position
anchor.setRow2(row);
anchor.setDx2(Units.pixelToEMU(left + width)); //dx = left + wanted width in px
anchor.setDy2(Units.toEMU(top + height)); //dy= top + wanted height in pt
drawing.createPicture(anchor, pictureIdx);
}
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
CellStyle styleVertAlingTop = wb.createCellStyle();
styleVertAlingTop.setVerticalAlignment(VerticalAlignment.TOP);
Sheet sheet = wb.createSheet();
sheet.setColumnWidth(0, 15 * 256); //15 default characters width
sheet.setColumnWidth(1, 30 * 256); //30 default characters width
Row row = sheet.createRow(2);
row.setHeight((short)(100 * 20)); //100pt height * 20 = twips (twentieth of an inch point)
Cell cell = row.createCell(0);
cell.setCellValue("Task 1");
cell = row.createCell(1);
cell.setCellValue("Replace Kemplon-Pipe");
cell.setCellStyle(styleVertAlingTop);
InputStream is = new FileInputStream("samplePict.jpeg");
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
is.close();
int left = 20; // 20px
int top = 20; // 20pt
int width = Math.round(sheet.getColumnWidthInPixels(1) - left - left); //width in px
int height = Math.round(row.getHeightInPoints() - top - 10/*pt*/); //height in pt
drawImageOnExcelSheet((XSSFSheet)sheet, 2, 1, left, top, width, height, pictureIdx);
wb.write(new FileOutputStream("ExcelDrawImagesOverCell.xlsx"));
wb.close();
}
}
Result:

Not able to set custom color in XSSFCell Apache POI

I am trying to set some custom(from hexcode or rgb value) color to a xssfcell.But the color of the cell is becoming black even though I am giving some other color.I have tried doing this by the following ways :
File xlSheet = new File("C:\\Users\\IBM_ADMIN\\Downloads\\Excel Test\\Something3.xlsx");
System.out.println(xlSheet.createNewFile());
FileOutputStream fileOutISPR = new FileOutputStream("C:\\Users\\IBM_ADMIN\\Downloads\\Excel Test\\Something3.xlsx");
XSSFWorkbook isprWorkbook = new XSSFWorkbook();
XSSFSheet sheet = isprWorkbook.createSheet("TEST");
XSSFRow row = sheet.createRow(0);
XSSFCellStyle cellStyle = isprWorkbook.createCellStyle();
byte[] rgb = new byte[3];
rgb[0] = (byte) 24; // red
rgb[1] = (byte) 22; // green
rgb[2] = (byte) 219; // blue
XSSFColor myColor = new XSSFColor(rbg);
cellStyle.setFillForegroundColor(myColor);
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has");
cell.setCellStyle(cellStyle);
CellRangeAddress rangeAddress = new CellRangeAddress(0, 0, 0, 2);
sheet.addMergedRegion(rangeAddress);
int width = ((int)(90 * 0.73)) * 256;
sheet.setColumnWidth(cell.getColumnIndex(), width);
//sheet.autoSizeColumn(cell.getColumnIndex());
RegionUtil.setBorderBottom(XSSFCellStyle.BORDER_MEDIUM, rangeAddress, sheet, isprWorkbook);
RegionUtil.setBottomBorderColor(IndexedColors.RED.getIndex(), rangeAddress, sheet, isprWorkbook);
XSSFCell cell2 = row.createCell(11);
cell2.setCellValue("222222222222222");
isprWorkbook.write(fileOutISPR);
//END of the program
XSSFCellStyle cellStyle = isprWorkbook.createCellStyle();
byte[] rgb = new byte[3];
rgb[0] = (byte) 24; // red
rgb[1] = (byte) 22; // green
rgb[2] = (byte) 219; // blue
XSSFColor myColor = new XSSFColor(rgb);
cellStyle.setFillForegroundColor(myColor);//1st method
//cellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128)));//2nd method
//XSSFColor myColor = new XSSFColor(Color.decode("0XFFFFFF"));
cellStyle.setFillForegroundColor(myColor);//3rd Method
I tried many other ways mentioned in answers to related questions but none of those solved my problem.
Please help me out.
This is caused by an incompleteness of Package org.apache.poi.ss.util.
PropertyTemplate as well as CellUtil and RegionUtilare be based on ss.usermodel level only and not on xssf.usermodel level. But org.apache.poi.ss.usermodel.CellStyle does not know something about a setFillForegroundColor(Color color) until now. It only knows setFillForegroundColor(short bg). So ss.usermodel level simply cannot set a Color as fill foreground color until now. Only a short (a color index) is possible.
If it comes to the question why setting the color is necessary when only the border shall be set using org.apache.poi.ss.util then the answer is, it is necessary because both, color and border, are in the same CellStyle. Thats why when adding the border settings to the CellStyle, the color settings must be maintain and finally be set new.
So in conclusion, there is not a way out of this dilemma. If you need using org.apache.poi.ss.util then you cannot use setFillForegroundColor(XSSFColor color) the same time. The only hope is setFillForegroundColor(Color color) will be added to org.apache.poi.ss.usermodel.CellStyle in later versions of apache poi.
As a workaround you can use conditional formatting to set custom colors after you've set all other format options (alignment, borders...) with cell style.
Here is a working (Kotlin) example that defines a custom color to distinguish between even and odd rows:
private fun setEvenOddColorFormatting(sheet: XSSFSheet) {
val sheetConditionalFormatting = sheet.sheetConditionalFormatting
val rule = sheetConditionalFormatting.createConditionalFormattingRule("MOD(ROW(), 2) = 0")
val formatForRule = rule.createPatternFormatting()
formatForRule.setFillBackgroundColor(XSSFColor(byteArrayOf(221.toByte(), 235.toByte(), 247.toByte())))
formatForRule.fillPattern = PatternFormatting.SOLID_FOREGROUND
val region = arrayOf(CellRangeAddress(0, sheet.lastRowNum,0,10))
sheetConditionalFormatting.addConditionalFormatting(region, rule)
}
A drawback is, that you have to write the rule as excel function. But you should be able to use an always true function and only set the region.

Not able to insert image into excelsheet

Hi I am trying to insert image into excel in android, using the following code but not able to do so, please help !!
// Create a path where we will place our List of objects on external // storage
File file = new File(context.getExternalFilesDir(null), "abc.xls");
FileOutputStream fileOS = null;
//add picture data to this workbook.
InputStream is = text.getResources().getAssets().open("images.jpg");
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
is.close();
CreationHelper helper = wb.getCreationHelper();
//create sheet
Sheet sheet = wb.createSheet();
// Create the drawing patriarch. This is the top level container for all shapes.
Drawing drawing = sheet.createDrawingPatriarch();
//add a picture shape
ClientAnchor anchor = helper.createClientAnchor();
//set top-left corner of the picture,
//subsequent call of Picture#resize() will operate relative to it
anchor.setCol1(0);
anchor.setRow1(0);
Picture pict = drawing.createPicture(anchor, pictureIdx);
//auto-size picture relative to its top-left corner
// pict.resize();
// if(wb instanceof XSSFWorkbook) file += "x";
fileOS= new FileOutputStream(file);
wb.write(fileOS);
Follow this:
FileInputStream fis = new FileInputStream(imagePath);
int b;
byte[] bytes = IOUtils.toByteArray(fis);
fis.close();
// This will insert the picture from start cell to end cell of excel
// sheet.
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
start.getCol(), start.getRow(), end.getCol(), end.getRow());
anchor.setAnchorType(2);
int index = wb.addPicture(bytes, HSSFWorkbook.PICTURE_TYPE_JPEG);
// Create the drawing patriarch. This is the top level container for all shapes.
Drawing patriarch = sheet.createDrawingPatriarch();
try {
HSSFPicture picture = patriarch.createPicture(anchor, index);
// picture.resize();
} catch (Exception e) {
String err = e.getMessage();
}
Here start is the starting cell reference for the left-top corner of image. Similarly, end is the ending cell reference for the left-top corner of image.
CellReference start;
CellReference end;
and Note this.
// Create the drawing patriarch. This is the top level container for all shapes.
Drawing drawing = sheet.createDrawingPatriarch();
Be wary of using this code. In case of Image insertion in the same sheet this code creates new Patriarch every time for new picture/image to be inserted. Make sure that for the second time image insertion this code picks the old Patriarch object for the particular sheet where insertion is to be done.

Insert an image in Excel through JXL or POI

I am stuck in inserting an image into an excel file. The condition is I don't want a full size image to be displayed. The image size is of the regular pixles (1280 * 1024) but in the excel I want to display around 10% of it. If someone double clicks on the image, then the full size image should be displayed. When we get out of the cell, then it should again be of the same size (10%). Any help would be really appreciated.
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("My Sample Excel");
InputStream inputStream = new FileInputStream("C:/opt/ZZEclipseWorkspace2/WFCLAuto/ScreenShots/12_Mar_2013__09_40_22PM_192.168.30.145.jpeg");
byte[] bytes = IOUtils.toByteArray(inputStream);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
inputStream.close();
CreationHelper helper = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
//set top-left corner for the image
anchor.setCol1(1);
anchor.setRow1(2);
Picture pict = drawing.createPicture(anchor, pictureIdx);
pict.resize(0.1);
FileOutputStream fileOut = null;
fileOut = new FileOutputStream("C:/opt/ZZEclipseWorkspace2/WFCLAuto/ScreenShots/testing1.xls");
wb.write(fileOut);
fileOut.close();
Try to set the top left and bottom right corners so it only fills the section your want.
HSSFClientAnchor anchor;
anchor=new HSSFClientAnchor(0,0,0,255,(short)1,2,(short)7,10);
anchor.setAnchorType(2);
This code will put the picture in a rectangle bounded by corner 1,2 and corner 7,10. I am not sure about adding the ability to double click on it though.

Changing cell color using apache poi

I'm using Apache POI to read data in a spreadsheet of part numbers. I look up the part number in our database, if we have a CAD drawing of the part I color the part number cell green, if we don't I color it red. After the processing is done the spreadsheet is saved. The problem I'm having is that every cell in that column comes out green. I've stepped through the code, the logic to look up the part number is working fine and the logic to determine what color the cell should be and setting the color and fill appears to work as well. Any ideas what I'm doing wrong here?
Thanks.
//Check the parts
for(int r=1;r<sheet.getPhysicalNumberOfRows();r++) {
String partNumber = null;
switch(cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
long pNum = (long) cell.getNumericCellValue();
partNumber = String.valueOf(pNum);
break;
case HSSFCell.CELL_TYPE_STRING:
partNumber = cell.getStringCellValue();
break;
default:
logger.info("Part Number at row " + r + " on sheet " + partList.getSheetName(s) + "is of an unsupported type");
}
try {
List<String> oldMaterialNumbers = getOldMaterialNumbers(partNumber);
boolean gotDrawing = checkPartNumber(oldMaterialNumbers, partNumber);
//If there's a drawing then color the row green, if not red.
short bgColorIndex = gotDrawing
?HSSFColor.LIGHT_GREEN.index //42
:HSSFColor.RED.index; //10
HSSFCell curCell = row.getCell(partNumberColumn);
HSSFCellStyle curStyle = curCell.getCellStyle();
curStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
curStyle.setFillForegroundColor(bgColorIndex);
curCell.setCellStyle(curStyle);
}catch(Exception e) {
throw e;
}
}
Short version: Create styles only once, use them everywhere.
Long version: use a method to create the styles you need (beware of the limit on the amount of styles).
private static Map<String, CellStyle> styles;
private static Map<String, CellStyle> createStyles(Workbook wb){
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
DataFormat df = wb.createDataFormat();
CellStyle style;
Font headerFont = wb.createFont();
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
headerFont.setFontHeightInPoints((short) 12);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFont(headerFont);
styles.put("style1", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFont(headerFont);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("date_style", style);
...
return styles;
}
you can also use methods to do repetitive tasks while creating styles hashmap
private static CellStyle createBorderedStyle(Workbook wb) {
CellStyle style = wb.createCellStyle();
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
return style;
}
then, in your "main" code, set the style from the styles map you have.
Cell cell = xssfCurrentRow.createCell( intCellPosition );
cell.setCellValue( blah );
cell.setCellStyle( (CellStyle) styles.get("style1") );
To create your cell styles see: http://poi.apache.org/spreadsheet/quick-guide.html#CustomColors.
Custom colors
HSSF:
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("Default Palette");
//apply some colors from the standard palette,
// as in the previous examples.
//we'll use red text on a lime background
HSSFCellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIME.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.RED.index);
style.setFont(font);
cell.setCellStyle(style);
//save with the default palette
FileOutputStream out = new FileOutputStream("default_palette.xls");
wb.write(out);
out.close();
//now, let's replace RED and LIME in the palette
// with a more attractive combination
// (lovingly borrowed from freebsd.org)
cell.setCellValue("Modified Palette");
//creating a custom palette for the workbook
HSSFPalette palette = wb.getCustomPalette();
//replacing the standard red with freebsd.org red
palette.setColorAtIndex(HSSFColor.RED.index,
(byte) 153, //RGB red (0-255)
(byte) 0, //RGB green
(byte) 0 //RGB blue
);
//replacing lime with freebsd.org gold
palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
//save with the modified palette
// note that wherever we have previously used RED or LIME, the
// new colors magically appear
out = new FileOutputStream("modified_palette.xls");
wb.write(out);
out.close();
XSSF:
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell( 0);
cell.setCellValue("custom XSSF colors");
XSSFCellStyle style1 = wb.createCellStyle();
style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128)));
style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
I believe it is because cell.getCellStyle initially returns the default cell style which you then change.
Create styles like this and apply them to cells:
cellStyle = (XSSFCellStyle) cell.getSheet().getWorkbook().createCellStyle();
Although as the previous poster noted try and create styles and reuse them.
There is also some utility class in the XSSF library that will avoid the code I have provided and automatically try and reuse styles. Can't remember the class 0ff hand.
checkout the example here
http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/ss/examples/BusinessPlan.java
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
For apache POI 3.9 you can use the code bellow:
HSSFCellStyle style = workbook.createCellStyle()
style.setFillForegroundColor(HSSFColor.YELLOW.index)
style.setFillPattern((short) FillPatternType.SOLID_FOREGROUND.ordinal())
The methods for 3.9 version accept short and you should pay attention to the inputs.

Categories

Resources