I want to create an Excel file with a dynamic logo and a list of dynamic images.
The creation of the text part in the Excel file works great with jxls.
But I'm not able to insert one image in each row with jxls.
Please, does anyone have advice for me?
Thank you in advance.
The only solution I found was to create the XLS file with JXLS and after creating the workbook insert the images with native POI functions like :
Workbook resultWorkbook = transformer.transformXLS(is, beans);
Iterator rowIter = resultWorkbook.getSheetAt(0).rowIterator();
while (rowIter.hasNext()){
HSSFRow row = (HSSFRow) rowIter.next();
Iterator cellIter = row.cellIterator();
while (cellIter.hasNext()){
HSSFCell cell = (HSSFCell) cellIter.next();
final String IMG_PREFIX = "#IMG#";
if(cell.toString().startsWith(IMG_PREFIX)){
String cellValue = cell.toString();
cell.setCellValue("");
String imagePath = cellValue.replaceFirst(IMG_PREFIX, "");
File imageFile = new File(imagePath);
if(imageFile.exists()){
FileInputStream isi = new FileInputStream(imageFile);
ImageTools imageTools = new ImageTools();
byte[] imgBytes = imageTools.resizeImage(isi, 100);
int pictureIdx = resultWorkbook.addPicture(imgBytes, Workbook.PICTURE_TYPE_JPEG);
CreationHelper helper = resultWorkbook.getCreationHelper();
Drawing drawing = resultWorkbook.getSheetAt(0).createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(cell.getColumnIndex());
anchor.setRow1(cell.getRowIndex());
Picture pict = drawing.createPicture(anchor, pictureIdx);
pict.resize();
isi.close();
}
}
}
}
I have achieved this problem.Below are the hints:
jx:each(items="persons" var="person" lastCell="G2" direction="DOWN")
jx:image(lastCell="F2" src="person.portrait")
hope it works for you.
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 have an app that imports an excel but the excel in question has black spaces in some of the cells, when I receive it puts those blank spaces in my database. I want to prevent that to happen, i tried to use regex and replace(" ","") and nothing work. Can you help me? This is my code:
File file = new File(inFileName);
Workbook workBook = WorkbookFactory.create(file);
Sheet sheet = workBook.getSheetAt(0);
int rowCtr = 34;
Row myRow = sheet.getRow(rowCtr++);
while (myRow != null) {
Cell nif = myRow.getCell(0);
Cell marcaexploracao = myRow.getCell(1);
Cell numerochip = myRow.getCell(2);
Cell marcaauricular = myRow.getCell(3);
Cell datanascimento = myRow.getCell(4);
//problem I mentioned is is in chipnumber only
bd.addAnimais(chipnumber.ToString().replaceFirst("/[^0-9]/","-"),marcaexploracao.toString(),marcaauricular.toString(),datanascimento.toString(),nif.toString(),0,"","","","",0);
myRow = sheet.getRow(rowCtr++);
}
}catch (Exception e){e.printStackTrace(); }
i have a cell in one excel sheet which contains only a comment. I created it like this:
CreationHelper factory = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
// When the comment box is visible, have it show in a 5x70 space
ClientAnchor anchor = factory.createClientAnchor();
anchor.setCol1(cell.getColumnIndex());
anchor.setCol2(cell.getColumnIndex()+5);
anchor.setRow1(row.getRowNum());
anchor.setRow2(row.getRowNum()+70);
Comment comment = drawing.createCellComment(anchor);
String text = "";
RichTextString str = factory.createRichTextString(text);
comment.setString(str);
row.createCell(0).setCellComment(comment);
Now i have a new project, where I just want to copy that comment to a cell of another sheet, which should just work with that:
XSSFRow row_master_a = null;
XSSFRow row_slave_a = null;
for(int j = 4;j<anz_neu+4;j++){
row_master_a = sheet_master.getRow(4+anz_neu+3-j);
if(row_master_a == null){
row_master_a = sheet_master.createRow(4+anz_neu+3-j);
}
row_master_a.setHeightInPoints(40);
row_slave_a = sheet_slave.getRow(j);
row_master_a.createCell(0).setCellComment(row_slave_a.getCell(0).getCellComment());`
I don't get any errors, but I also don't have a comment in the new sheet.
Can anyone help?
Thanks
You have to create a separate comment object and only copy the string contents as the Comment-object is tied to the Sheet.
I.e. something like
Drawing drawing_master = sheet_master.createDrawingPatriarch();
Comment comment_master = drawing_master.createCellComment(anchor);
comment_master.setString(row_slave_a.getCell(0).getCellComment().getString());
row_master_a.createCell(0).setCellComment(comment_master);
I am using Apache poi to write in to an excel and given download option to that file. But each time when I download, it's overwriting the existing file and even file size is also increasing.
I want to create a new file by same name each time.
ServletContext servletContext = httpSession.getServletContext()
String absolutePathToIndexJSP = servletContext.getRealPath("/") + "File/filename.xlsx
FileInputStream fis = new FileInputStream(new File(absolutePathToIndexJSP));
System.out.println("file path : " + absolutePathToIndexJSP);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
XSSFRow row = sheet.createRow(0);
row.setHeight((short) 2000);
XSSFCell r1c = row.createCell(0);
row.removeCell(r1c);
r1c.setCellValue("Ptoto");
for (int s = 0; s < arrayJson.length(); s++) {
System.out.println(s);
int imageCount = s + 1;
System.out.println(imageCount);
String absolutePathToImage = servletContext.getRealPath("/") + "imgData/" + imageCount + ".jpg";
System.out.println("writing image");
System.out.println("path : " + absolutePathToImage);
InputStream inputStream = new FileInputStream(absolutePathToImage);
byte[] bytes = IOUtils.toByteArray(inputStream);
int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
inputStream.close();
CreationHelper helper = workbook.getCreationHelper();
Drawing drawing = null;
drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
row.removeCell(r1c);
anchor.setCol1(s + 1);
anchor.setRow1(0);
double scale = 0.11;
//Creates a picture
Picture pict = drawing.createPicture(anchor, pictureIdx);
//Reset the image to the original size
pict.resize(scale);
}
fos = new FileOutputStream(absolutePathToIndexJSP);
System.out.println("file written");
workbook.write(fos);
fos.flush();
fos.close();
From what you're asking, you basically just want to delete the old file and create a new one each time, right?
If you're not concerned with possible collisions (two users attempting to download the same source location at the same time) then you could use this delete file method to delete the file and then create a new one. So, where you have
new File(absolutePathToIndexJSP)
You should instantiate that, call the delete method, and then use it.
can any one plz tell me how to read an image from an excel sheet cell through Apache POI .Actually my code works when the cell content is text type but when cell content is an image , it does not read the image to Cell class object and my cell class object takes it as blank data. plz help me guys....
List lst = workbook.getAllPictures();
for (Iterator it = lst.iterator(); it.hasNext(); ) {
PictureData pict = (PictureData)it.next();
String ext = pict.suggestFileExtension();
byte[] data = pict.getData();
if (ext.equals("jpeg")){
FileOutputStream out = new FileOutputStream("pict.jpg");
out.write(data);
out.close();
}
}