I am trying to display an image into an excel sheet using poi. Here's the source code :
//...reading inputStream
byte[] bytes = IOUtils.toByteArray(inputStream);
//Debugging shows that pictureIdx value is 0
int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
Drawing drawing = sheet.createDrawingPatriarch();
CreationHelper helper = workbook.getCreationHelper();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(1);
Picture pict = drawing.createPicture(anchor, pictureIdx);
//...Saving workbook
When I open the generated excel file, no image is displayed and I am getting instead this error message :
Repaired records: Drawing from /xl/drawings/drawing1.xml-Part (Drawingsform)
Any help will be more appreciated. Many Thanks.
p.s: I am using Java 8 and MS Excel 2013
Related
FileInputStream fis =new FileInputStream("D:\\Axis SwiftTrade\\Axis_swift\\Axis_swift_update\\Reports\\ScreenshotFile.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet readable_sheet = wb.getSheet("Sheet1");
System.out.println("1141");
//FileInputStream obtains input bytes from the image file
FileInputStream inputStream = new FileInputStream(scrFile);
System.out.println("1016");
//Get the contents of an InputStream as a byte[].
byte[] bytes = IOUtils.toByteArray(inputStream);
System.out.println("1020");
//Adds a picture to the workbook
int pictureIdx = wb.addPicture(bytes, wb.PICTURE_TYPE_PNG);
System.out.println("1024");
//close the input stream
inputStream.close();
//Returns an object that handles instantiating concrete classes
CreationHelper helper = wb.getCreationHelper();
System.out.println("1030");
//Creates the top-level drawing patriarch.
Drawing drawing = readable_sheet.createDrawingPatriarch();
System.out.println("1034");
//Create an anchor that is attached to the worksheet
ClientAnchor anchor = helper.createClientAnchor();
System.out.println("1038");
anchor.setCol1(1); //Column B
anchor.setCol2(2); //Column C
System.out.println("1046");
int lastrow=readable_sheet.getLastRowNum();
System.out.println("lastrow is:" + lastrow );
for(int i=1;i<=lastrow+1;i++)
{
if(readable_sheet.getRow(i)==null)
{
System.out.println("entered");
XSSFRow row1=readable_sheet.createRow(i);
System.out.println("1028");
XSSFCell C1 = row1.createCell(0);
System.out.println("1030");
C1.setCellValue("screen shot "+i);
System.out.println("1026");
anchor.setRow1(i); //Row 3
System.out.println("1028");
anchor.setRow2(i+1);//Row 4
System.out.println("1030");
//Creates a picture
Picture pict = drawing.createPicture(anchor, pictureIdx);
System.out.println("1033");
}
}//Write the Excel file...
FileOutputStream fileOut = null;
fileOut = new FileOutputStream("D:\\Axis SwiftTrade\\Axis_swift\\Axis_swift_update\\Reports\\ScreenshotFile.xlsx");
wb.write(fileOut);
fileOut.close();
trying to add multiple images into excelsheet, succeed in that, while entering multiple screenshots into excel sheet 1st screenshot is getting removed.
any solution or snippet code for adding multiple screenshots in excel sheet???unable to add multiple screenshots into excel sheet and 1st image is removed when adding multiple screenshots into excel sheetunable to add multiple screenshots into excel sheet and 1st image is removed when adding multiple screenshots into excel sheet
i want to write create an excel and download it but when i add image i get following error.
[(The filename, directory name, or volume label syntax is incorrect)]
but the file is loaded if i printout its name and path it shows rite name and path
file NAME: example.png LOCATION file:\D:\blueplustech\plastocartreport\target\plastocartreport-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes!\static\example.png
class:
//FileInputStream obtains input bytes from the image file
InputStream inputStream = new
FileInputStream(getClass().getResource("/static/example.png").getFile());
//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();
//Create an anchor that is attached to the worksheet
ClientAnchor anchor = helper.createClientAnchor();
//set top-left corner for the image
anchor.setCol1(1);
anchor.setRow1(2);
//Creates a picture
Picture pict = drawing.createPicture(anchor, pictureIdx);
//Reset the image to the original size
pict.resize();
any suggestions...?
No need to use file but getResourceAsStream instead
Instead of the
InputStream inputStream = new
FileInputStream(getClass().getResource("/static/example.png").getFile());
use
InputStream inputStream = getClass().getResourceAsStream("/static/example.png");
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.
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.
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);