Home » excel » java – I have three images I need to insert in three sheets using Poi Apache but all 3 images get inserted in the last sheet

java – I have three images I need to insert in three sheets using Poi Apache but all 3 images get inserted in the last sheet

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have three images I need to insert in three different sheets by using the Poi Apache library but all 3 images get inserted in the last sheet, please advise me what needs to be done to insert the images in different sheets. Help is appreciated. ———————————————————————————————————————

public class Img2xls {

    public static byte[] bytes;
    public static HSSFWorkbook my_workbook;
    public static HSSFSheet my_sheet;
    public static InputStream my_banner_image;
    private static HSSFPatriarch drawing;
    // Initializing employees data to insert into the excel file


    public static void main(String[] args) throws IOException, InvalidFormatException{
        String path1 = "C:\Users\ELCOT\Pictures\screen1.png";
        String path2 = "C:\Users\ELCOT\Pictures\screen2.png"; 
        String path3 = "C:\Users\ELCOT\Pictures\screen1.png";

        /* Create a Workbook and Worksheet */

                 my_workbook = new HSSFWorkbook();
                 my_sheet = my_workbook.createSheet("Vista");   
                 my_sheet = my_workbook.createSheet("Hub2");
                 my_sheet = my_workbook.createSheet("D42");
              HSSFSheet my_sheet1 =   my_sheet;          
              /* Create the drawing container */
              drawing = my_sheet1.createDrawingPatriarch();

                getImage(path1,0);
                getImage(path2,40);
                getImage(path3,80);

                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("excel_insert_image_example.xls"));
                my_workbook.write(out);
                out.close();
        }

    public static void getImage(String img,int i) throws FileNotFoundException, IOException{           

        Row headerRow = my_sheet.createRow(i);
               Cell cell = headerRow.createCell(0);
                cell.setCellValue("Server 1");
                my_sheet.autoSizeColumn(0);
                /* Read the input image into InputStream */
                my_banner_image = new FileInputStream(img);
                /* Convert Image to byte array */
                bytes = IOUtils.toByteArray(my_banner_image);
                /* Add Picture to workbook and get a index for the picture */
               int my_picture_id = my_workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
               //int my_picture_id = my_workbook.addPictur  
              /* Close Input Stream */
                my_banner_image.close();                
                /* Create an anchor point */
                ClientAnchor my_anchor = new HSSFClientAnchor();
                /* Define top left corner, and we can resize picture suitable from there */
                my_anchor.setCol1(2);
                my_anchor.setRow1(i);           
                /* Invoke createPicture and pass the anchor point and ID */
                HSSFPicture  my_picture = drawing.createPicture(my_anchor, my_picture_id);
                /* Call resize method, which resizes the image */
                my_picture.resize();              
         }

    }
How to&Answers:

As Modus already mentioned, you are creating one variable, and without using it, you are updating its value again and again.

Understand it with very basic example;

int x
x = 5;
x = 10;
x = 20;

Now what you are expecting, what will be value of x if we will print it… It will be 20… not 5 and 10 and 20 at once. You are doing the same with my_sheet. You are creating three sheets and assigning them to my_sheet. The newly created instance will replace the previous one and at the end when you will use it, it will be the last instance only.

my_workbook = new HSSFWorkbook();
my_sheet = my_workbook.createSheet("Vista");
my_sheet = my_workbook.createSheet("Hub2");
my_sheet = my_workbook.createSheet("D42");
HSSFSheet my_sheet1 = my_sheet;

Update:
The solution is create one method which takes Three parameters say.

public void insertImageInSheet(String sheetName, String path, int number){
//Now first create new sheet in workbook
Sheet newSheet= my_workbook.createSheet(sheetName);
drawing = newSheet.createDrawingPatriarch();
//not sure what second parameter is right now I have given it a name as number
getImage(path,number);
}

//and then use this method three times
insertImageInSheet("Vista", path1, 0);
insertImageInSheet("Hub2", path2, 40);
insertImageInSheet("D42", path3, 80);

//Now write the wrokbook from output stream
/* Write changes to the workbook */
FileOutputStream out = new FileOutputStream(new File("excel_insert_image_example.xls"));
my_workbook.write(out);
out.close();