簡體   English   中英

如何使用Java Apache poi在Excel文件中插入多張圖片?

[英]How can I insert more than one picture in Excel file with Java Apache poi?

我創建一個Java來生成包含圖片jpg的單元格的excel文件。 按行顯示圖片僅顯示最后一行的最后一張圖片

第二部分是我的Java編碼的一部分

你可以幫助庫爾獲得所有照片嗎?

最好的祝福


    import java.net.URL;
    import java.net.URLConnection;

    import org.apache.poi.hssf.usermodel.*;
    import org.apache.poi.hssf.util.*;
    import org.apache.poi.hssf.usermodel.HSSFClientAnchor;

    @SuppressWarnings("serial")
    public class ExcelCaracAssocModelFileBatch extends AbstractBusinessBatch {

    public int rowIdx = 0;
    public int rowId1 = 0;
    public short cellIdx = 0;
    public short cellId2 = 15;  
    public String _item_codref = "";  /
    private HSSFWorkbook wb = new HSSFWorkbook();
    private HSSFSheet sheet1 = wb.createSheet("CONTEXT");
    private HSSFRow hssfHeader; 
    private HSSFRow hssfHead2;
    private HSSFSheet sheet = wb.createSheet("DATA");
    private HSSFCellStyle cellStyle = wb.createCellStyle();
    private HSSFCellStyle cellStyle2 = wb.createCellStyle();
    public  HSSFRow r2;
    private HSSFPicture my_picture;
    private HSSFCell Celldescarac; 
    private HSSFCell Cellcodcarac;

    public ExcelCaracAssocModelFileBatch(ContexteMetier contexteMetier) {
            super(contexteMetier);
        }

    }


        @SuppressWarnings("deprecation")
        public RowSetStateHolder select(ParameterAccess parameterAccess) throws CoreBatchException {

             HSSFFont font1 = wb.createFont();
                   font1.setFontHeightInPoints((short)14);
                   font1.setFontName("Arial");
                   font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                   cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                   cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
                   cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                   cellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
                   cellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
                   cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
                   cellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
                   cellStyle.setFont(font1);

                   cellStyle2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
                   cellStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
                   cellStyle2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
                   cellStyle2.setBorderRight(HSSFCellStyle.BORDER_THIN);
                   cellStyle2.setBorderTop(HSSFCellStyle.BORDER_THIN);
                   cellStyle2.setBorderBottom(HSSFCellStyle.BORDER_THIN);           


            SqlBindingHelper sql = new SqlBindingHelper();

       sql.appendSql("SELECT w_1, w_2, w_3, w_4, w_5, w_6, w_7, w_8, w_9, w_10,");  
                sql.appendSql("w_11, w_12, w_13, w_14, w_15, w_16, w_17, w_18, w_19, w_20,");  
                sql.appendSql("w_21, w_22, w_23, w_24, w_25, w_26, w_27, w_28, w_29, w_30,");  
                sql.appendSql("num01, num02");
                sql.appendSql(" FROM PHX_GDT2_CARAS3 ");  
                sql.appendSql(" WHERE ");
                sql.appendSql(" w_1 = ? "); sql.addParamValue(_w_1); 
                sql.addParamValue( _w_2 );

                  } */

                return createRowSetStateHolderFromSql(sql);
        }


    public long execute(RowSetStateHolder rowSetStateHolder) throws CoreBatchException {

        int cpt = 0;

                   Row row;
                    while(rowSetStateHolder.getRowSet().hasNext())
                    {
                        row = rowSetStateHolder.getRowSet().next();

                   String item_cod = (String)row.getAttribute("w_8");


                   rowIdx = rowId1++;
                   r2 = sheet.createRow(rowIdx);
                   r2.setHeight((short) 1800);


                   HSSFCell r2c0 = r2.createCell((short) 0);
                   r2c0.setCellValue((String)row.getAttribute("w_3"));
                   r2c0.setCellStyle(cellStyle2);
                   HSSFCell r2c1 = r2.createCell((short) 1);
                   r2c1.setCellValue((String)row.getAttribute("w_4"));  
                   r2c1.setCellStyle(cellStyle2);
                   HSSFCell r2c2 = r2.createCell((short) 2);
                   r2c2.setCellValue((String)row.getAttribute("w_5")); 
                   r2c2.setCellStyle(cellStyle2);
                   HSSFCell r2c3 = r2.createCell((short) 3);
                   r2c3.setCellValue((String)row.getAttribute("w_6")); 
                   r2c3.setCellStyle(cellStyle2);
                   HSSFCell r2c4 = r2.createCell((short) 4);
                   r2c4.setCellValue((String) row.getAttribute("w_27"));  
                   r2c4.setCellStyle(cellStyle2);
                   HSSFCell r2c5 = r2.createCell((short) 5);
                   r2c5.setCellValue((String)row.getAttribute("w_18")); 
                   r2c5.setCellStyle(cellStyle2);
                   HSSFCell r2c6 = r2.createCell((short) 6);
                   r2c6.setCellValue((String)row.getAttribute("w_19")); 
                   r2c6.setCellStyle(cellStyle2);
                   HSSFCell r2c7 = r2.createCell((short) 7);
                   r2c7.setCellValue((String)row.getAttribute("w_8")); 
                   r2c7.setCellStyle(cellStyle2);
                   HSSFCell r2c8 = r2.createCell((short) 8);
                   r2c8.setCellValue((String) row.getAttribute("w_9")); 
                   r2c8.setCellStyle(cellStyle2);
                   HSSFCell r2c9 = r2.createCell((short) 9);
                   r2c9.setCellValue((String) row.getAttribute("w_10")); 
                   r2c9.setCellStyle(cellStyle2);
                   HSSFCell r2c10 = r2.createCell((short) 10);
                   r2c10.setCellValue((String) row.getAttribute("w_11")); 
                   r2c10.setCellStyle(cellStyle2);
                   HSSFCell r2c11 = r2.createCell((short) 11);
                   r2c11.setCellValue((String) row.getAttribute("w_12")); 
                   r2c11.setCellStyle(cellStyle2);
                   HSSFCell r2c12 = r2.createCell((short) 12);
                   r2c12.setCellValue((String) row.getAttribute("w_13")); 
                   r2c12.setCellStyle(cellStyle2);



                   String urlmedia = (String) row.getAttribute("w_30"); 
                   try {
                       //test url image 
                       URL url = new URL((String) row.getAttribute("w_30"));
                       URLConnection connection = url.openConnection();
                       int fileLength = connection.getContentLength();

                    // URL ok
                    if (fileLength != -1)
                    {
                        InputStream input = null;
                       input = connection.getInputStream();
                       //Get the contents of an InputStream as a byte[].
                       //byte[] bytes = IOUtils.toByteArray(input);
                       ByteArrayOutputStream img_bytes = new ByteArrayOutputStream();
                        int b;
                        while ((b = input.read()) != -1)
                            img_bytes.write(b);
                        input.close();
                       //Adds a picture to the workbook
                       //int pictureIdx = wb.addPicture(bytes, HSSFWorkbook.PICTURE_TYPE_JPEG);
                       int pictureIdx = wb.addPicture(img_bytes.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG);
                       //close the input stream
                       input.close();

                       /* Create the drawing container */
                       HSSFPatriarch drawing = sheet.createDrawingPatriarch();
                       /* Create an anchor point */

                       /*
                        dx1 - the x coordinate within the first cell.
                            dy1 - the y coordinate within the first cell.
                            dx2 - the x coordinate within the second cell.
                            dy2 - the y coordinate within the second cell.
                            col1 - the column (0 based) of the first cell.
                            row1 - the row (0 based) of the first cell.
                            col2 - the column (0 based) of the second cell.
                            row2 - the row (0 based) of the second cell. 
                        */


                       HSSFClientAnchor my_anchor = new HSSFClientAnchor();
                       my_anchor.setDx1(10);
                       my_anchor.setDx2(10);
                       my_anchor.setDy1(10);
                       my_anchor.setDy2(10);
                       short pictcol1 = (short) rowIdx;
                       short pictcol2 = (short) (pictcol1 + 1);
                       my_anchor.setCol1((short) 13);
                       my_anchor.setCol2((short) 14);
                       my_anchor.setRow1(pictcol1);
                       my_anchor.setRow2(pictcol2);
                       my_anchor.setAnchorType((int) 2);

                       setMy_picture(drawing.createPicture(my_anchor, pictureIdx));

                   }
                   }
                    catch (IOException e)
                    {
                         _log.info("Error while trying to download the file "+ urlmedia, e );
                    }
                         cellIdx = 14;         
                        _item_codref = item_cod;
                   }

                   cellId2 = cellIdx++;
                   HSSFCell r2c4 = r2.createCell(cellId2);
                   r2c4.setCellValue((String)row.getAttribute("w_17")); 
                   r2c4.setCellStyle(cellStyle2);

                   }
                  }

               cpt++;


                    }
            return cpt;
        }

        @Override
        public synchronized void tearDown() throws CoreBatchException {
            try 
            {   
                  //Write the Excel file
                    String nomfic = FILE_NAME_PREFIX + "_" + getCtxMetier().getNumedi() + ".xls";
                    FileOutputStream fileOut = null;
                    fileOut = new FileOutputStream(nomfic);
                    wb.write(fileOut);
                    fileOut.close();                 
            }
                  catch (Exception e) 
                  {
                       _log.error(e);
                      throw new CoreBatchException(e);
                  }

            finally
            {
                super.tearDown();
            }
        }

createDrawingPatriarch方法的作用是刪除Sheet上的所有圖形,包括預先存在的圖像。

請注意,這通常會刪除該圖紙上的所有現有圖紙。

您正在execute方法的while循環內調用createDrawingPatriarch ,因此在每次迭代中都將刪除先前的圖像,只有最后一個圖像可以保留。

為了把所有的圖像完好,一旦創建族長對象時,前while循環,並在你的每一次迭代中重用它while循環。

/* Create the drawing container */
HSSFPatriarch drawing = sheet.createDrawingPatriarch();
while(rowSetStateHolder.getRowSet().hasNext())
{
    // Other code...

    // Use it here as normal.
    setMy_picture(drawing.createPicture(my_anchor, pictureIdx));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM