title: How to Add Image in Excel Sheet from Database using iText in Java
body: <p>i was faceing some problem in creating Excel Report using iText in Java. I want to retrieve some data containing image file from database and export it into Excel Sheet but image file is not appearing in Excel Sheet. And Getting Error Like :</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/codec/digest/DigestUtils
      at org.apache.poi.hssf.usermodel.HSSFWorkbook.addPicture(HSSFWorkbook.java:1705)
      at CompInfoTable.ExcelReport(CompInfoTable.java:301)
      at CompInfoTable$6.actionPerformed(CompInfoTable.java:748)
      at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
      at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
      at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
      at java.awt.Component.processMouseEvent(Unknown Source)
      at javax.swing.JComponent.processMouseEvent(Unknown Source)
      at java.awt.Component.processEvent(Unknown Source)
      at java.awt.Container.processEvent(Unknown Source)
      at java.awt.Component.dispatchEventImpl(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Window.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
      at java.awt.EventQueue.access$500(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue$4.run(Unknown Source)
      at java.awt.EventQueue$4.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)
  Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.digest.DigestUtils
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      ... 40 more
  </code></pre>
  
  <p>I have try many times with different code but is always getting error. This is my code:</p>
  
  <pre><code>public void ExcelReport()
      {
          try
          {
          filename="Company Report.xls";
          workbook=new HSSFWorkbook();
          HSSFSheet sheet=workbook.createSheet("Company Report");
          HSSFRow rowhead;
          HSSFCell cellhead;
  
          /*-------------------------------Adding A Custom Color ------------------------*/
          /* HSSFPalette palette=workbook.getCustomPalette();
             palette.setColorAtIndex(new Byte ((byte)41), new Byte ((byte)82), new Byte ((byte)35), new Byte ((byte)85));
             style.setFillForegroundColor(palette.getColor(41).getIndex());
          /*-------------------------------Adding A Custom Color ------------------------*/
  
          HSSFCellStyle style=workbook.createCellStyle();
          style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
          style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);           
          style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);      
          style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
          style.setAlignment(CellStyle.ALIGN_CENTER);
          style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
          style.setFillPattern(CellStyle.SOLID_FOREGROUND);
          style.setFillForegroundColor(IndexedColors.DARK_RED.getIndex());
  
          HSSFCellStyle style1=workbook.createCellStyle();
          style1.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
          style1.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);          
          style1.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);     
          style1.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
          style1.setAlignment(CellStyle.ALIGN_CENTER);
          style1.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
          style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
          style1.setFillForegroundColor(IndexedColors.DARK_TEAL.getIndex());
  
          HSSFFont font3=workbook.createFont();
          font3.setFontName("Calibri");
          font3.setFontHeightInPoints((short)20);
          font3.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
          font3.setItalic(true);
          font3.setColor(IndexedColors.WHITE.getIndex());
          style.setFont(font3);
  
          HSSFFont font1=workbook.createFont();
          font1.setFontName("Calibri");
          font1.setFontHeightInPoints((short)18);
          font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
          font1.setItalic(true);
          font1.setColor(IndexedColors.WHITE.getIndex());
          style1.setFont(font1);
  
  
  
          HSSFCellStyle style2=workbook.createCellStyle();
          style2.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
          style2.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);          
          style2.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);     
          style2.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
          style2.setAlignment(CellStyle.ALIGN_CENTER);
          style2.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
          style2.setFillPattern(CellStyle.SOLID_FOREGROUND);
          style2.setFillForegroundColor(IndexedColors.SEA_GREEN.getIndex());
  
          HSSFFont font2=workbook.createFont();
          font2.setFontName("Calibri");
          font2.setFontHeightInPoints((short)14);
          font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
          font2.setItalic(true);
          font2.setColor(IndexedColors.BLACK.getIndex());
          style2.setFont(font2);
  
          rowhead=sheet.createRow(4);
          cellhead=rowhead.createCell(0);
          cellhead.getRow().setHeightInPoints(30);
          CellRangeAddress cellrange=new CellRangeAddress(4,5,0,7);
          sheet.addMergedRegion(cellrange);
  
          HSSFRegionUtil.setBorderBottom(CellStyle.BORDER_MEDIUM,cellrange,sheet, workbook);
          HSSFRegionUtil.setBorderLeft(CellStyle.BORDER_MEDIUM,cellrange,sheet, workbook);
          HSSFRegionUtil.setBorderRight(CellStyle.BORDER_MEDIUM,cellrange,sheet, workbook);
          HSSFRegionUtil.setBorderTop(CellStyle.BORDER_MEDIUM,cellrange,sheet, workbook);
  
          cellhead.setCellValue("Company Information");
          cellhead.setCellStyle(style);
  
          rowhead=sheet.createRow(8);
          cellhead=rowhead.createCell(0);
          cellhead.getRow().setHeightInPoints(25);
          cellhead.setCellValue("Comp_Name");
          cellhead.setCellStyle(style1);
  
          cellhead=rowhead.createCell(1);
          cellhead.setCellValue("Comp_Address");
          cellhead.setCellStyle(style1);
  
          cellhead=rowhead.createCell(2);
          cellhead.setCellValue("Comp_Contact");
          cellhead.setCellStyle(style1);
  
          cellhead=rowhead.createCell(3);
          cellhead.setCellValue("Comp_Email");
          cellhead.setCellStyle(style1);
  
          cellhead=rowhead.createCell(4);
          cellhead.setCellValue("Comp_TIN");
          cellhead.setCellStyle(style1);
  
          cellhead=rowhead.createCell(5);
          cellhead.setCellValue("Comp_ST");
          cellhead.setCellStyle(style1);
  
          cellhead=rowhead.createCell(6);
          cellhead.setCellValue("Comp_CIN");
          cellhead.setCellStyle(style1);
  
          cellhead=rowhead.createCell(7);
          cellhead.setCellValue("Comp_Image");
          cellhead.setCellStyle(style1);
  
          for(int i=0;i&lt;=7;i++)
          {
              sheet.setColumnWidth(i,7000);
          }
  
          query="Select * from CompanyInfo";
          PStat=con.prepareStatement(query);
          res=PStat.executeQuery();
  
          int Row=10;
  
          while(res.next())
          {
              rowhead=sheet.createRow(Row);
              cellhead=rowhead.createCell(0);
              cellhead.setCellValue(res.getString("Comp_Name"));
              cellhead.setCellStyle(style2);
              cellhead=rowhead.createCell(1);
              cellhead.setCellValue(res.getString("Comp_Address"));
              cellhead.setCellStyle(style2);
              cellhead=rowhead.createCell(2);
              cellhead.setCellValue(res.getString("Comp_Contact"));
              cellhead.setCellStyle(style2);
              cellhead=rowhead.createCell(3);
              cellhead.setCellValue(res.getString("Comp_Email"));
              cellhead.setCellStyle(style2);
              cellhead=rowhead.createCell(4);
              cellhead.setCellValue(res.getString("Comp_TIN"));
              cellhead.setCellStyle(style2);
              cellhead=rowhead.createCell(5);
              cellhead.setCellValue(res.getString("Comp_ST"));
              cellhead.setCellStyle(style2);
              cellhead=rowhead.createCell(6);
              cellhead.setCellValue(res.getString("Comp_CIN"));
              cellhead.setCellStyle(style2);
  
              //InputStream is=new FileInputStream((res.getBytes("Comp_Images")).toString());
              //byte[] bytes=IOUtils.toByteArray(is);
              byte[] bytes=res.getBytes("Comp_Images");
              int pics=workbook.addPicture(bytes,HSSFWorkbook.PICTURE_TYPE_JPEG);
              //is.close();
              CreationHelper helper=workbook.getCreationHelper();
              Drawing drawing=sheet.createDrawingPatriarch();
              ClientAnchor anchor=helper.createClientAnchor();
              anchor.setCol1(7);
              anchor.setRow1(Row);
              Picture picture=drawing.createPicture(anchor, pics);
              picture.resize();
              Row++;
          }
  FileOutputStream fileout=new FileOutputStream(filename);
                              workbook.write(fileout);
                              fileout.close();
                              workbook.close();
                              JOptionPane.showMessageDialog(null,"Excel file is Generated");
      }
      catch(Exception e)
      {
          JOptionPane.showMessageDialog(null,e);
      }
      }
  </code></pre>