Bug 58422

Summary: How to add image to header of Excel?
Product: POI Reporter: RatiKanata Pal <pal.ratikanta>
Component: SXSSFAssignee: POI Developers List <dev>
Status: RESOLVED DUPLICATE    
Severity: normal    
Priority: P2    
Version: 3.9-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: Windows NT   

Description RatiKanata Pal 2015-09-16 14:04:55 UTC
Hi,
I want to add a image to Header of excel .As long I can see all the methods of "org.apache.poi.ss.usermodel.HeaderFooter" class is have string argument .How can I add image to header portion??

Code:
-------------------------------------------------------------------------------------

public static void main(String[] args) {
		try {
			HeaderFooter vHead = null;
			int idx = 0;
			int idy = 0;
			Cell vFirstCell = null;
			Cell vLastCell = null;
			Cell vCell = null;
			FileOutputStream fileOut = new FileOutputStream("poi-test.xlsx");
			SXSSFWorkbook workbook = new SXSSFWorkbook(100);
			SXSSFSheet worksheet = (SXSSFSheet) workbook.createSheet("POI Worksheet");
			vHead = worksheet.getHeader();

			InputStream my_banner_image = new FileInputStream("C:\\Users\\RATI\\Desktop\\rati.jpg");
			byte[] bytes = IOUtils.toByteArray(my_banner_image);
			int my_picture_id = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
			my_banner_image.close();
			CreationHelper helper = workbook.getCreationHelper();
			Drawing drawing = worksheet.createDrawingPatriarch();
			ClientAnchor anchor = helper.createClientAnchor();
			Picture pict = drawing.createPicture(anchor, my_picture_id);
			vHead.setCenter(HSSFHeader.font("Times New Roman", "Bold") + HSSFHeader.fontSize((short) 12) + "Rati");
			vHead.setRight(HSSFHeader.font("Times New Roman", "Bold") + HSSFHeader.fontSize((short) 8) + "&[Picture]");
			
			// index from 0,0... cell A1 is cell(0,0)
			Row row1 = worksheet.createRow(idx++);

			vCell = row1.createCell(idy++);
			vFirstCell = vCell;
			vCell.setCellValue("NAME1");
			vCell = row1.createCell(idy++);
			vCell.setCellValue("NAME2");
			vCell = row1.createCell(idy++);
			vCell.setCellValue("NAME3");
			vCell = row1.createCell(idy++);
			vCell.setCellValue("NAME4");
			vCell = row1.createCell(idy++);
			vCell.setCellValue("NAME5");

			idy = 0;

			row1 = worksheet.createRow(idx++);

			vCell = row1.createCell(idy++);
			vFirstCell = vCell;
			vCell.setCellValue("NAME1");
			vCell = row1.createCell(idy++);
			vCell.setCellValue("NAME2");
			vCell = row1.createCell(idy++);
			vCell.setCellValue("NAME3");
			vCell = row1.createCell(idy++);
			vCell.setCellValue("NAME4");
			vCell = row1.createCell(idy++);
			vCell.setCellValue("NAME5");

			for (int i = 0; i < 100; i++) {

				row1 = worksheet.createRow(idx++);
				idy = 0;
				for (int j = 0; j < 5; j++) {
					vCell = row1.createCell(idy++);
					vCell.setCellValue("TEST--" + i + "--DATA--" + j);
					vLastCell = vCell;
				}
			}

			for (int i = 0; i < 5; i++) {
				// worksheet.autoSizeColumn(i);
			}

			worksheet.setAutoFilter(new CellRangeAddress(1, 101, 0, 4));
			workbook.write(fileOut);
			fileOut.flush();
			fileOut.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
Comment 1 Javen O'Neal 2015-10-07 10:12:06 UTC
You should post this question to the poi-user group [1]. The group is pretty active and you're more likely to get responses there for usage questions.

We usually only open bugs if we have figured out that POI is missing a feature or there's a bug in an existing feature.

You may also be interested at looking at some similar open bugs:
bug 53309 - similar request for XSSFWorkbooks
bug 46704 - similar request for HSSFWorkbooks
bug 58237 - similar request for slideshow presentations

[1] http://poi.apache.org/mailinglists.html
Comment 2 Dominik Stadler 2015-10-30 18:03:52 UTC
I am closing this one as duplicate of the XSSF-related bug as we should be able to reuse an implementation for both XSSF and SXSSF and thus we should avoid two places for discussions about this.

*** This bug has been marked as a duplicate of bug 53309 ***