You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Sudheer Raju <rn...@gmail.com> on 2010/07/30 09:13:33 UTC

Write Image into Excel Header or Footer

Hello All,

I am trying to Write Image into Excel (2003) Header or Footer.
I have seen some posts where it was mentioned that it is not possible.

I am also not able to Read a File which has Image as a Header added, I am
getting the following Exception.
java.io.IOException: Unable to read entire header; 0 bytes read; expected
512 bytes
	at
org.apache.poi.poifs.storage.HeaderBlockReader.alertShortRead(HeaderBlockReader.java:130)
	at
org.apache.poi.poifs.storage.HeaderBlockReader.(HeaderBlockReader.java:81)
	at
org.apache.poi.poifs.filesystem.POIFSFileSystem.(POIFSFileSystem.java:151)

Below is my code:
	public static void readWaterMarkExcelFile() {
		String waterMarkExcelFileName = "D:/TestWaterMark_2.xls";
		try {
			POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
					waterMarkExcelFileName));
			HSSFWorkbook wb = new HSSFWorkbook(fs);
			HSSFSheet sheet = wb.getSheetAt(0);
			HSSFRow row;
			HSSFCell cell;

			int rows; // No of rows
			rows = sheet.getPhysicalNumberOfRows();

			int cols = 0; // No of columns
			int tmp = 0;

			// This trick ensures that we get the data properly even if it
			// doesn't start from first few rows
			for (int i = 0; i < 10 || i < rows; i++) {
				row = sheet.getRow(i);
				if (row != null) {
					tmp = sheet.getRow(i).getPhysicalNumberOfCells();
					if (tmp > cols)
						cols = tmp;
				}
			}

			for (int r = 0; r < rows; r++) {
				row = sheet.getRow(r);
				if (row != null) {
					for (int c = 0; c < cols; c++) {
						cell = row.getCell((short) c);
						if (cell != null) {
							// Your code here
						}
					}
				}
			}
		} catch (Exception ioe) {
			ioe.printStackTrace();
		}
	}

Let me in case you have any solution for this or the latest POI release has
a solution for this issue of Reading or Writing images into Excel Header or
Footer.

Or is there any other API to do this (such as jxl or any other).

Any help would be greatly appreciated.

Thanks,
Sudheer.
-- 
View this message in context: http://old.nabble.com/Write-Image-into-Excel-Header-or-Footer-tp29303430p29303430.html
Sent from the POI - User mailing list archive at Nabble.com.

Re: Write Image into Excel Header or Footer

Posted by Sudheer Raju <rn...@gmail.com>.
Sorry the code was not posted properly, find it below:


Sudheer Raju wrote:
> 
> Hello All,
> 
> I am trying to Write Image into Excel (2003) Header or Footer.
> I have seen some posts where it was mentioned that it is not possible.
> 
> I am also not able to Read a File which has Image as a Header added, I am
> getting the following Exception.
> java.io.IOException: Unable to read entire header; 0 bytes read; expected
> 512 bytes
> 	at
> org.apache.poi.poifs.storage.HeaderBlockReader.alertShortRead(HeaderBlockReader.java:130)
> 	at
> org.apache.poi.poifs.storage.HeaderBlockReader.<init>(HeaderBlockReader.java:81)
> 	at
> org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:151)
> 
> Below is my code:
> 	public static void readWaterMarkExcelFile() {
> 		String waterMarkExcelFileName = "D:/TestWaterMark_2.xls";
> 		try {
> 			POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
> 					waterMarkExcelFileName));
> 			HSSFWorkbook wb = new HSSFWorkbook(fs);
> 			HSSFSheet sheet = wb.getSheetAt(0);
> 			HSSFRow row;
> 			HSSFCell cell;
> 
> 			int rows; // No of rows
> 			rows = sheet.getPhysicalNumberOfRows();
> 
> 			int cols = 0; // No of columns
> 			int tmp = 0;
> 
> 			// This trick ensures that we get the data properly even if it
> 			// doesn't start from first few rows
> 			for (int i = 0; i < 10 || i < rows; i++) {
> 				row = sheet.getRow(i);
> 				if (row != null) {
> 					tmp = sheet.getRow(i).getPhysicalNumberOfCells();
> 					if (tmp > cols)
> 						cols = tmp;
> 				}
> 			}
> 
> 			for (int r = 0; r < rows; r++) {
> 				row = sheet.getRow(r);
> 				if (row != null) {
> 					for (int c = 0; c < cols; c++) {
> 						cell = row.getCell((short) c);
> 						if (cell != null) {
> 							// Your code here
> 						}
> 					}
> 				}
> 			}
> 		} catch (Exception ioe) {
> 			ioe.printStackTrace();
> 		}
> 	}
> 
> Let me in case you have any solution for this or the latest POI release
> has a solution for this issue of Reading or Writing images into Excel
> Header or Footer.
> 
> Or is there any other API to do this (such as jxl or any other).
> 
> Any help would be greatly appreciated.
> 
> Thanks,
> Sudheer.
> 

-- 
View this message in context: http://old.nabble.com/Write-Image-into-Excel-Header-or-Footer-tp29303430p29303442.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org