I want to auto-size columns in excel, but without spending too much performance.
The built-in auto-size of Apache POI is very slow (didn't finish after couple hours with 1 million rows).
To save performance I just want to approximate the cell width, but for that I need the Font Metrics.
Apache POI has a class called FontDetails, but it does not work on its own.
The class StaticFontMetrics seems to be the one actually loading the metrics, but it is not public.
But even with copying the protected code to my workspace and making it accessible it fails to load the Font Metrics.
How can I get those Metrics? Will java.awt.FontMetrics always return an accurate result?
EDIT: The stacktrace I get when trying to get the Metrics of a Font:
Caused by: java.lang.IllegalArgumentException: The supplied FontMetrics doesn't know about the font 'Calibri', so we can't use it. Please add it to your font metrics file (see StaticFontMetrics.getFontDetails
at ourpackagestructure.apachepoi.FontDetails.create(FontDetails.java:106)
at ourpackagestructure.apachepoi.StaticFontMetrics.getFontDetails(StaticFontMetrics.java:94)
Apache poi uses AttributedString and TextLayout to get the bounds out of a text in a special font.
So, as long as the whole column is in same font, best approach would be first get the longest string which shall be stored in that column. Then get the width of that string in that font using java.awt.font.TextLayout. Then set this as the column width.
Note, the column width in Excel is set in in units of 1/256th of a default character width. So in addition to the width of the string in that font in pixels, you need the width of a default character to calculate the Excel column width.
Example:
import java.awt.font.FontRenderContext;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.text.AttributedString;
import java.awt.geom.Rectangle2D;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.SheetUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ApachePoiGetStringWidth {
public static void main(String args[]) throws Exception {
String testString = "Lorem ipsum semit dolor";
String fontName = "Calibri";
short fontSize = 24;
boolean italic = true;
boolean bold = false;
Workbook workbook = new XSSFWorkbook();
Font font = workbook.createFont();
font.setFontHeightInPoints(fontSize);
font.setFontName(fontName);
font.setItalic(italic);
font.setBold(bold);
CellStyle style = workbook.createCellStyle();
style.setFont(font);
AttributedString attributedString = new AttributedString(testString);
attributedString.addAttribute(TextAttribute.FAMILY, font.getFontName(), 0, testString.length());
attributedString.addAttribute(TextAttribute.SIZE, (float)font.getFontHeightInPoints());
if (font.getBold()) attributedString.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, 0, testString.length());
if (font.getItalic()) attributedString.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, 0, testString.length());
FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
TextLayout layout = new TextLayout(attributedString.getIterator(), fontRenderContext);
Rectangle2D bounds = layout.getBounds();
double frameWidth = bounds.getX() + bounds.getWidth();
System.out.println(frameWidth);
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(2);
Cell cell = row.createCell(2);
cell.setCellValue(testString);
cell.setCellStyle(style);
int defaultCharWidth = SheetUtil.getDefaultCharWidth(workbook);
sheet.setColumnWidth(2, (int)Math.round(frameWidth / defaultCharWidth * 256));
try (java.io.FileOutputStream out = new java.io.FileOutputStream("Excel.xlsx")) {
workbook.write(out);
}
workbook.close();
}
}
Related
I'm trying to copy from cells from one workbook to another with the latest version of Apache POI (4.1.2).
If both workbooks are .xlsx files, everything works fine. But if the source workbook is an (old) .xls file and the destination workbook is an .xlsx file, the following code fails
// Copy style from old cell and apply to new cell
CellStyle newCellStyle = targetWorkbook.createCellStyle();
newCellStyle.cloneStyleFrom(sourceCell.getCellStyle());
targetCell.setCellStyle(newCellStyle);
The exception that's thrown is:
java.lang.IllegalArgumentException: Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle
If we can't use cloneStyleFrom when the files (or Workbook objects) are of different types, how can we convert a HSSFCellStyle object to a XSSFCellStyle?
The answer to your question "How can we convert a HSSFCellStyle object to a XSSFCellStyle?" is: We can't do that using apache poi 4.1.2. This simply is not supported as clearly stated in CellStyle.cloneStyleFrom: "However, both of the CellStyles will need to be of the same type (HSSFCellStyle or XSSFCellStyle)."
The other question is: Should we at all convert one cell style into another? Or what use cases are there for CellStyle.cloneStyleFrom at all? In my opinion there are none. There are Excel limitations for the count of unique cell formats/cell styles. See Excel specifications and limits. So we should not create a single cell style for each single cell because then those limitations will be reached very fast. So instead of cloning cell styles we should get the style properties from source style style1 and then using CellUtil.setCellStyleProperties to set those style properties to the other cell in question. This method attempts to find an existing CellStyle that matches the cell's current style plus styles properties in properties. A new style only is created if the workbook does not contain a matching style.
Since your question title is "Copy cells between Excel workbooks with Apache POI", I have created a working draft of how I woud do this.
The following code first gets a existent Workbook.xls as HSSFWorkbook wb1 and creates a new XSSFWorkbook wb2. Then it loops over all cells of the first sheet of wb1 and tries copying those cells into the first sheet of wb2. To do so there is a method copyCells(Cell cell1, Cell cell2) which uses copyStyles(Cell cell1, Cell cell2). The latter gets the style properties from source style style1 got from cell1 and then uses CellUtil.setCellStyleProperties to set those style properties to cell2. For copying fonts copyFont(Font font1, Workbook wb2) is used. This tries creating new fonts in wb2 only if such a font is not already present in that workbook. This is necessary because there also is a limit of unique font types per workbook in Excel.
Working example:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.util.CellUtil;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;
class ExcelCopyCells {
static Font copyFont(Font font1, Workbook wb2) {
boolean isBold = font1.getBold();
short color = font1.getColor();
short fontHeight = font1.getFontHeight();
String fontName = font1.getFontName();
boolean isItalic = font1.getItalic();
boolean isStrikeout = font1.getStrikeout();
short typeOffset = font1.getTypeOffset();
byte underline = font1.getUnderline();
Font font2 = wb2.findFont(isBold, color, fontHeight, fontName, isItalic, isStrikeout, typeOffset, underline);
if (font2 == null) {
font2 = wb2.createFont();
font2.setBold(isBold);
font2.setColor(color);
font2.setFontHeight(fontHeight);
font2.setFontName(fontName);
font2.setItalic(isItalic);
font2.setStrikeout(isStrikeout);
font2.setTypeOffset(typeOffset);
font2.setUnderline(underline);
}
return font2;
}
static void copyStyles(Cell cell1, Cell cell2) {
CellStyle style1 = cell1.getCellStyle();
Map<String, Object> properties = new HashMap<String, Object>();
//CellUtil.DATA_FORMAT
short dataFormat1 = style1.getDataFormat();
if (BuiltinFormats.getBuiltinFormat(dataFormat1) == null) {
String formatString1 = style1.getDataFormatString();
DataFormat format2 = cell2.getSheet().getWorkbook().createDataFormat();
dataFormat1 = format2.getFormat(formatString1);
}
properties.put(CellUtil.DATA_FORMAT, dataFormat1);
//CellUtil.FILL_PATTERN
//CellUtil.FILL_FOREGROUND_COLOR
FillPatternType fillPattern = style1.getFillPattern();
short fillForegroundColor = style1.getFillForegroundColor(); //gets only indexed colors, no custom HSSF or XSSF colors
properties.put(CellUtil.FILL_PATTERN, fillPattern);
properties.put(CellUtil.FILL_FOREGROUND_COLOR, fillForegroundColor);
//CellUtil.FONT
Font font1 = cell1.getSheet().getWorkbook().getFontAt(style1.getFontIndexAsInt());
Font font2 = copyFont(font1, cell2.getSheet().getWorkbook());
properties.put(CellUtil.FONT, font2.getIndexAsInt());
//BORDERS
BorderStyle borderStyle = null;
short borderColor = -1;
//CellUtil.BORDER_LEFT
//CellUtil.LEFT_BORDER_COLOR
borderStyle = style1.getBorderLeft();
properties.put(CellUtil.BORDER_LEFT, borderStyle);
borderColor = style1.getLeftBorderColor();
properties.put(CellUtil.LEFT_BORDER_COLOR, borderColor);
//CellUtil.BORDER_RIGHT
//CellUtil.RIGHT_BORDER_COLOR
borderStyle = style1.getBorderRight();
properties.put(CellUtil.BORDER_RIGHT, borderStyle);
borderColor = style1.getRightBorderColor();
properties.put(CellUtil.RIGHT_BORDER_COLOR, borderColor);
//CellUtil.BORDER_TOP
//CellUtil.TOP_BORDER_COLOR
borderStyle = style1.getBorderTop();
properties.put(CellUtil.BORDER_TOP, borderStyle);
borderColor = style1.getTopBorderColor();
properties.put(CellUtil.TOP_BORDER_COLOR, borderColor);
//CellUtil.BORDER_BOTTOM
//CellUtil.BOTTOM_BORDER_COLOR
borderStyle = style1.getBorderBottom();
properties.put(CellUtil.BORDER_BOTTOM, borderStyle);
borderColor = style1.getBottomBorderColor();
properties.put(CellUtil.BOTTOM_BORDER_COLOR, borderColor);
CellUtil.setCellStyleProperties(cell2, properties);
}
static void copyCells(Cell cell1, Cell cell2) {
switch (cell1.getCellType()) {
case STRING:
/*
//TODO: copy HSSFRichTextString to XSSFRichTextString
RichTextString rtString1 = cell1.getRichStringCellValue();
cell2.setCellValue(rtString1); // this fails if cell2 is XSSF and rtString1 is HSSF
*/
String string1 = cell1.getStringCellValue();
cell2.setCellValue(string1);
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell1)) {
Date date1 = cell1.getDateCellValue();
cell2.setCellValue(date1);
} else {
double cellValue1 = cell1.getNumericCellValue();
cell2.setCellValue(cellValue1);
}
break;
case FORMULA:
String formula1 = cell1.getCellFormula();
cell2.setCellFormula(formula1);
break;
//case : //TODO: further cell types
}
copyStyles(cell1, cell2);
}
public static void main(String[] args) throws Exception {
Workbook wb1 = WorkbookFactory.create(new FileInputStream("Workbook.xls"));
Workbook wb2 = new XSSFWorkbook();
Sheet sheet1 = wb1.getSheetAt(0);
Sheet sheet2 = wb2.createSheet();
Set<Integer> columns = new HashSet<Integer>();
Row row2 = null;
Cell cell2 = null;
for (Row row1 : sheet1) {
row2 = sheet2.createRow(row1.getRowNum());
for (Cell cell1 : row1) {
columns.add(cell1.getColumnIndex());
cell2 = row2.createCell(cell1.getColumnIndex());
copyCells(cell1, cell2);
}
}
wb1.close();
for (Integer column : columns) {
sheet2.autoSizeColumn(column);
}
FileOutputStream out = new FileOutputStream("Workbook.xlsx");
wb2.write(out);
out.close();
wb2.close();
}
}
If Workbook.xls looks like this:
then the resulting Workbook.xlsx looks like this:
Note: This is a working draft and needs to be completed. See TODO comments in the code. RichTextString cell values needs to be considered. Further cell types needs to be considered.
Method copyStyles only provides copying data format, fill pattern and fill foreground color (only for indexed colors), font and borders. Further cell style properties needs to be considered.
Could you please suggest on next case?
I`ve set up dataformat and cell type ("#,###.00", NUMERIC)
(I want thounsand separator plus two decimal numbers)
It works as I expected but to have formatted cells I need to select them first
Before selection data looks not formatted
In other words I have to select cell so that it is formatted, otherwise it stays without any formatting
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.RIGHT);
style.setLocked(locked);
style.setDataFormat(workbook.createDataFormat().getFormat("#,###.00"));
cell.setCellStyle(style);
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(<big decimal value>.toString());
Simply do not set cell value as string if you need a numeric cell value. If you set cell value as String, then the cell type also will be string. This is independent of setting CellType before setting cell value. While setting a String cell value the type changes to string always.
See API documentation which shows that Cell.setCellType is deprecated and what Cell.setCellValue methods are possible.
You needs setting a double cell value if cell shall have numeric content.
Example:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.math.BigDecimal;
import java.util.Random;
class CreateExcelCellNumberFormat {
public static void main(String[] args) throws Exception {
try (Workbook workbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {
CellStyle style = workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat("#,###.00"));
Sheet sheet = workbook.createSheet();
for (int r = 0; r < 10; r++) {
Cell cell = sheet.createRow(r).createCell(0);
cell.setCellStyle(style);
BigDecimal bigDecimal = new BigDecimal(new Random().nextDouble() * 10000000000000d);
cell.setCellValue(bigDecimal.doubleValue());
}
sheet.setColumnWidth(0, 25 * 256);
workbook.write(fileout);
}
}
}
I want to set the content of cell value with a combination of bold and italic.
like "This is Sample content."
However, this is not working using XSSFRichTextString.
I am using apache poi library version 4.0.1. I have tried making my content bold and italic combination using XSSFRichTextString. I have appended the string using by passing two arguments in method cell1Value.append("sample ", fontBold);i.e. String and Font.
XSSFRichTextString cell1Value= new XSSFRichTextString("This is ");
XSSFFont fontBold= wb.createFont();
fontBold.setBold(true); //set bold
fontBold.setUnderline(HSSFFont.U_SINGLE);
XSSFFont fontItalic= wb.createFont();
fontItalic.setItalic(true); //set italic
cell1Value.append("sample ",fontBold);
cell1Value.append("content", fontItalic);
System.err.println(cell1Value.getCTRst());
Cell cell1 = row.createCell(0);
cell1.setCellValue(cell1Value);
I am expecting "sample" to be bold and "content" to be italic. However, the underline is working fine and my "sample" word is underlined properly. Please suggest what I am missing?
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TextBoldItalic {
public static void main(String[] args) throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
XSSFFont fontBold = wb.createFont();
fontBold.setBold(true);
XSSFFont fontItalic = wb.createFont();
fontItalic.setItalic(true);
XSSFFont fontBoldItalic = wb.createFont();
fontBoldItalic.setBold(true);
fontBoldItalic.setItalic(true);
XSSFRichTextString cellValue = new XSSFRichTextString();
cellValue.append("This is ", fontBold);
cellValue.append("sample ", fontItalic);
cellValue.append("content", fontBoldItalic);
cell.setCellValue(cellValue);
OutputStream fileOut = new FileOutputStream("TextBoldItalic.xlsx");
wb.write(fileOut);
wb.close();
}
}
This code works for me and gives me this result in LibreOffice. OpenOffice is fine, too. No MS Excel here for a test, sorry. Of course tools like this Online-Excel-Viewer will not do it right. So, please try my code and make a report.
As the code looks plausible, just doing the complete run:
The following I have tested with
org.apache.poi / poi / 3.16
org.apache.poi / poi-ooxml / 3.16
It worked.
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet("With Rich Text");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
XSSFFont fontPlain = wb.createFont();
XSSFFont fontBoldItalic = wb.createFont();
fontBoldItalic.setBoldItalic(true);
fontBoldItalic.setItalic(true);
XSSFFont fontItalic = wb.createFont();
fontItalic.setItalic(true);
XSSFRichTextString cell1Value= new XSSFRichTextString("This is ");
cell1Value.applyFont(fontPlain);
cell1Value.append("sample ", fontBoldItalic);
cell1Value.append("content", fontItalic);
cell.setCellValue(cell1Value);
wb.write(new FileOutputStream(xlsxFile));
} catch (IOException e) {
e.printStackTrace();
}
My guess is a variable mix-up or something trivial. Maybe the font.
The problem using WPS Spreadsheets is that they claim to be most compatible to Excel but sometimes they totally fail. This time they misinterpret all Boolean font settings (bold, italic, strike) if they explicitly are set true.
Office Open XML provides Boolean elements having val attribute. Example: <b val="true"/> or <b val="false"/> or also <b val="1"/> or <b val="0"/>. But for set bold font having <b/> would be enough. And for not set bold font simply not having the b element at all would be enough.
Apache poi always sets <b val="true"/> for bold and <b val="false"/> for not bold. But WPS Spreadsheets now seems to misinterpret <b val="true"/>. It expects <b/> only.
The following code is the most compatible code for creating rich text strings for Excel. It supports Office Open XML (*.xlsx) as well as BIFF (*.xls) and it corrects the <Boolean val="true"/> to <Boolean/> only.
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
class CreateExcelRichText {
static RichTextString createRichTextString(Workbook workbook, String[] textParts, Font[] fonts) {
CreationHelper creationHelper = workbook.getCreationHelper();
RichTextString richTextString = creationHelper.createRichTextString(String.join("", textParts));
int start = 0;
int end = 0;
for (int tp = 0; tp < textParts.length; tp ++) {
Font font = null;
if (tp < fonts.length) font = fonts[tp];
end += textParts[tp].length();
if (font != null) richTextString.applyFont(start, end, font);
start += textParts[tp].length();
}
if (richTextString instanceof XSSFRichTextString) {
//unset val="true" for boolean objects
XSSFRichTextString xSSFRichTextString = (XSSFRichTextString)richTextString;
String[] boolenanObjectsToUnset = new String[]{"b", "i", "strike"};
for (String boolenanObjectToUnset : boolenanObjectsToUnset) {
XmlObject[] xmlObjects = xSSFRichTextString.getCTRst().selectPath(
"declare namespace main='http://schemas.openxmlformats.org/spreadsheetml/2006/main' " +
".//main:" + boolenanObjectToUnset);
for (XmlObject xmlObject : xmlObjects) {
CTBooleanProperty booleanProperty = (CTBooleanProperty)xmlObject;
if (booleanProperty.getVal()) booleanProperty.unsetVal();
}
}
}
return richTextString;
}
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
//Workbook workbook = new HSSFWorkbook();
Font font = workbook.createFont();
Font fontBoldItalic = workbook.createFont();
fontBoldItalic.setBold(true);
fontBoldItalic.setItalic(true);
Font fontItalic = workbook.createFont();
fontItalic.setItalic(true);
Font fontStrikeout = workbook.createFont();
fontStrikeout.setStrikeout(true);
String[] textParts = new String[]{"This is ", "Sample ", "content. ", "This is crossed out."};
Font[] fonts = new Font[]{font, fontBoldItalic, fontItalic, fontStrikeout};
RichTextString richTextString = createRichTextString(workbook, textParts, fonts);
Sheet sheet = workbook.createSheet();
sheet.createRow(0).createCell(0).setCellValue(richTextString);
String fileName = (workbook instanceof XSSFWorkbook)?"Excel.xlsx":"Excel.xls";
FileOutputStream out = new FileOutputStream(fileName);
workbook.write(out);
out.close();
workbook.close();
}
}
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:
i have been searching the web and found no real good example for applying a gradient color to an excel sheet cell using Apache Poi.
The example I found are pretty old and the classes not really exist anymore in the current Apache Poi version. I'm currently using Apache Poi version 3.16.
Can somebody point out the steps which are needed to apply a gradient color to excel sheet using the poi library. All hints are appreciated.
There is always still not a possibility to set gradient cell fills using the default actual apache poi versions.
So I suspect that the code you found was for XSSF (*.xlsx) and for the code you found it was just not mentioned that this code needs the full jar of all of the schemas ooxml-schemas-*.jar or poi-ooxml-full-*.jar in the class path as mentioned in faq-N10025.
The following example works but also needs the full jar of all of the schemas in the class path as mentioned in faq-N10025.
It first sets pattern fill settings to the CellStyle only to have some fill to get the fill index from it. Then it gets the low level CTFill used in this CellStyle. An then it unsets the pattern fill and then it sets the gradient fill.
To get informations about how to use CTFill one needs download the sources of ooxml-schemas and do javadoc. There is no API documentation for ooxml-schemas public available.
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTGradientFill;
public class CreateExcelCellGradientFillColor {
public static void main(String[] args) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
XSSFCellStyle cellstyle = workbook.createCellStyle();
//set pattern fill settings only to have some fill to get the fill index from it
cellstyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//get fill index used in this CellStyle
int fillidx = (int)cellstyle.getCoreXf().getFillId();
//get the low level CTFill used in this CellStyle
CTFill ctfill = workbook.getStylesSource().getFillAt(fillidx).getCTFill();
System.out.println(ctfill);
//unset the pattern fill
ctfill.unsetPatternFill();
//now low level set the gradient fill
byte[] rgb1 = new byte[3];
rgb1[0] = (byte) 0; // red
rgb1[1] = (byte) 0; // green
rgb1[2] = (byte) 255; // blue
byte[] rgb2 = new byte[3];
rgb2[0] = (byte) 255; // red
rgb2[1] = (byte) 255; // green
rgb2[2] = (byte) 255; // blue
CTGradientFill ctgradientfill = ctfill.addNewGradientFill();
ctgradientfill.setDegree(90.0);
ctgradientfill.addNewStop().setPosition(0.0);
ctgradientfill.getStopArray(0).addNewColor().setRgb(rgb1);
ctgradientfill.addNewStop().setPosition(0.5);
ctgradientfill.getStopArray(1).addNewColor().setRgb(rgb2);
ctgradientfill.addNewStop().setPosition(1.0);
ctgradientfill.getStopArray(2).addNewColor().setRgb(rgb1);
System.out.println(ctfill);
Cell cell = row.createCell(0);
cell.setCellValue("");
cell.setCellStyle(cellstyle);
FileOutputStream out = new FileOutputStream("CreateExcelCellGradientFillColor.xlsx");
workbook.write(out);
out.close();
workbook.close();
}
}