首页 > 解决方案 > 我有三张图像需要使用 Poi Apache 插入三张纸,但所有 3 张图像都插入到最后一张纸中

问题描述

我有三张图片需要使用 Poi Apache 库插入到三张不同的工作表中,但是所有 3 张图片都插入到最后一张工作表中,请告诉我需要做什么才能将图像插入到不同的工作表中。帮助表示赞赏。-------------------------------------------------- -------------------------------------------------- -----------------

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();              
         }

    }

标签: javaexcelapache-poijava-ee-8

解决方案


正如 Modus 已经提到的,您正在创建一个变量,并且在不使用它的情况下,您会一次又一次地更新它的值。

用非常基本的例子来理解它;

int x x = 5; x = 10; x = 20; 现在你所期望的是,如果我们打印它,x 的值是多少……它将是 20……而不是一次 5 和 10 和 20。你对my_sheet. 您正在创建三张工作表并将它们分配给 my_sheet。新创建的实例将替换前一个实例,最后当您使用它时,它将只是最后一个实例。

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;

更新: 解决方案是创建一种采用三个参数的方法。

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();


推荐阅读