Pictures in Excel
sheets are not cell contents. They are in a separate layer, called drawing, and are only anchored to the cells of the sheet. So pictures hover over the cells and also over the cell contents. If text and pictures shall not overlap, the text must be placed or aligned accordingly.
So what your screen shot shows is a picture anchored to A1
and resized, so it fits into the first 4 rows in height. For the text, the range A1:H4
is merged. Merged ranges show the content of top left cell. So the text must be set into cell A1
. A cell style to set horizontal alignment, vertical alignment and wrap text is needed. This style leads to horizontal and vertical centered text in merged range A1:H4
. So the text and the picture do not overlap.
Complete Example:
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;
class CreateExcelPictureAndText {
public static void main(String[] args) throws Exception {
//Workbook workbook = new HSSFWorkbook(); String filePath = "./Excel.xls";
Workbook workbook = new XSSFWorkbook(); String filePath = "./Excel.xlsx";
Sheet sheet = workbook.createSheet();
Row row = null;
//create cell style horizontal alignment - center, vertical alignment - center, wrap text
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setWrapText(true);
//insert picture's media into workbook
InputStream inputStream = new FileInputStream("./logo.png");
byte[] imageBytes = IOUtils.toByteArray(inputStream);
int pictureureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG);
inputStream.close();
//insert picture anchored over the cells of the sheet
CreationHelper helper = workbook.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(0); //col A
anchor.setRow1(0); //row 1
Picture pict = drawing.createPicture(anchor, pictureureIdx);
pict.resize(); //now picture is anchored at A1 and sized to it's original size
//get picture's original size
int pictOriginalWidthInPixels = pict.getImageDimension().width;
int pictOriginalHeightInPixels = pict.getImageDimension().height;
//get height of row 1 to 4
float rowHeightInPixels = 0f;
for (int r = 0; r < 4; r++) {
row = sheet.getRow(r); if (row == null) row = sheet.createRow(r);
float rowHeightInPoints = row.getHeightInPoints();
rowHeightInPixels += rowHeightInPoints * Units.PIXEL_DPI / Units.POINT_DPI;
}
//we want scaling in aspect ratio
float scale = rowHeightInPixels / pictOriginalHeightInPixels;
pict.resize(scale, scale); //now picture is resized to fit into the first 4 rows
//create merged cells for heading
sheet.addMergedRegion(new CellRangeAddress(0,3,0,7)); //merged region A1:H4
//set text for merged region in A1
row = sheet.getRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Golden Heights, 9/1, sector 3, Huda Techno Enclave,\n"
+"Madhapur (HITEC city), Hyderabad, Telangana - 500 081, India.\n"
+"Phone: 91 40.23116868 Email: info@sysintelli.com");
cell.setCellStyle(cellStyle);
//set column widths
for (int c = 0; c < 8; c++) {
sheet.setColumnWidth(c, 15*256); //column width 15 default character widths
}
FileOutputStream out = new FileOutputStream(filePath);
workbook.write(out);
out.close();
workbook.close();
}
}
Result:
