Esempio n. 1
0
  /**
   * 获取Excel2003图片
   *
   * @param sheetNum 当前sheet编号
   * @param sheet 当前sheet对象
   * @param workbook 工作簿对象
   * @return Map key:图片单元格索引(0_1_1)String,value:图片流PictureData
   * @throws IOException
   */
  public static Map<String, PictureData> getSheetPictrues03(
      int sheetNum, HSSFSheet sheet, HSSFWorkbook workbook) {

    Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
    List<HSSFPictureData> pictures = workbook.getAllPictures();
    if (pictures.size() != 0) {
      for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
        HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
        if (shape instanceof HSSFPicture) {
          if (null != shape) {
            HSSFPicture pic = (HSSFPicture) shape;
            int pictureIndex = pic.getPictureIndex() - 1;
            HSSFPictureData picData = pictures.get(pictureIndex);
            String picIndex =
                String.valueOf(sheetNum)
                    + UNDER_LINE
                    + String.valueOf(anchor.getRow1())
                    + UNDER_LINE
                    + String.valueOf(anchor.getCol1());
            sheetIndexPicMap.put(picIndex, picData);
          }
        }
      }
      return sheetIndexPicMap;
    } else {
      return null;
    }
  }
Esempio n. 2
0
 public void testDefaultPictureSettings() {
   HSSFPicture picture = new HSSFPicture(null, new HSSFClientAnchor());
   assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT);
   assertEquals(picture.getFillColor(), HSSFShape.FILL__FILLCOLOR_DEFAULT);
   assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_NONE);
   assertEquals(picture.getLineStyleColor(), HSSFShape.LINESTYLE__COLOR_DEFAULT);
   assertEquals(picture.isNoFill(), false);
   assertEquals(picture.getPictureIndex(), -1); // not set yet
 }
Esempio n. 3
0
  public void testReadExistingImage() {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");
    HSSFSheet sheet = wb.getSheet("pictures");
    HSSFPatriarch drawing = sheet.getDrawingPatriarch();
    assertEquals(1, drawing.getChildren().size());
    HSSFPicture picture = (HSSFPicture) drawing.getChildren().get(0);

    assertEquals(picture.getPictureIndex(), 2);
    assertEquals(picture.getLineStyleColor(), HSSFShape.LINESTYLE__COLOR_DEFAULT);
    assertEquals(picture.getFillColor(), 0x5DC943);
    assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT);
    assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_DEFAULT);
    assertEquals(picture.isNoFill(), false);

    picture.setPictureIndex(2);
    assertEquals(picture.getPictureIndex(), 2);
  }
Esempio n. 4
0
  /** No NullPointerException should appear */
  public void testDefaultSettingsWithEmptyContainer() {
    EscherContainerRecord container = new EscherContainerRecord();
    EscherOptRecord opt = new EscherOptRecord();
    opt.setRecordId(EscherOptRecord.RECORD_ID);
    container.addChildRecord(opt);
    ObjRecord obj = new ObjRecord();
    CommonObjectDataSubRecord cod = new CommonObjectDataSubRecord();
    cod.setObjectType(HSSFSimpleShape.OBJECT_TYPE_PICTURE);
    obj.addSubRecord(cod);
    HSSFPicture picture = new HSSFPicture(container, obj);

    assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT);
    assertEquals(picture.getFillColor(), HSSFShape.FILL__FILLCOLOR_DEFAULT);
    assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_DEFAULT);
    assertEquals(picture.getLineStyleColor(), HSSFShape.LINESTYLE__COLOR_DEFAULT);
    assertEquals(picture.isNoFill(), HSSFShape.NO_FILL_DEFAULT);
    assertEquals(picture.getPictureIndex(), -1); // not set yet
  }
  /**
   * 复制来源表格中第1个SHEET中的图片到目标表格中的第1个SHEET中。
   *
   * @param destBook -- 目标表格
   * @param srcBook -- 来源表格
   */
  private static void copySheetImage(HSSFWorkbook destBook, HSSFWorkbook srcBook) {
    // 来源表单
    HSSFSheet srcSheet = srcBook.getSheetAt(0);
    // 目标表单
    HSSFSheet destSheet = destBook.getSheetAt(0);

    // 需要偏移的行数
    int endRowNum = destSheet.getPhysicalNumberOfRows();

    // 取来源表单中的图片对象
    List<HSSFPicture> lsSrcPicture = getAllPicture(srcSheet);
    _log.showDebug("----------source picture size:" + lsSrcPicture.size());

    // 取所有子图形数据,如果是主从报表且明细数据占多页时,则会报空指针错误
    List<HSSFPictureData> lsPicData = null;
    try {
      lsPicData = srcBook.getAllPictures();
    } catch (Exception e) {
      // e.printStackTrace();
      _log.showWarn("由于表单明细有多页,造成临时表的图片数据取不到,只能采用原表第1个图替代!");

      // 原表中也没有图片,则不处理图片复制了
      lsPicData = destBook.getAllPictures();
      if (lsPicData == null || lsPicData.isEmpty()) return;

      // 只取原表中第1个图片信息
      List<HSSFPictureData> destData = FactoryUtil.newList();
      for (int i = 0, n = lsSrcPicture.size(); i < n; i++) {
        destData.add(lsPicData.get(0));
      }
      lsPicData = destData;
    }
    if (lsPicData == null || lsPicData.isEmpty()) return;
    _log.showDebug("----------source data size:" + lsPicData.size());

    // data数量可能大于图片数量
    if (lsSrcPicture.size() > lsPicData.size()) {
      _log.showWarn("图片数量与数据数量不符!");
      return;
    }

    // 取图片管理器
    HSSFPatriarch destDraw = destSheet.getDrawingPatriarch();
    if (destDraw == null) {
      destDraw = destSheet.createDrawingPatriarch();
    }

    // 取原目标表单中的图片对象
    List<HSSFPicture> lsDestPicture = getAllPicture(destSheet);
    int index = lsDestPicture.size();

    for (int i = 0, n = lsSrcPicture.size(); i < n; i++) {
      // 取图片对象
      HSSFPicture picture = lsSrcPicture.get(i);
      // 根据图片序号取图片数据
      HSSFPictureData picdata = lsPicData.get(i);
      // 取图片字节信息
      byte[] datas = picdata.getData();

      // 取图片位置信息
      HSSFClientAnchor anchor = (HSSFClientAnchor) picture.getAnchor();

      // 添加行偏移值
      anchor.setRow1(anchor.getRow1() + endRowNum);
      anchor.setRow2(anchor.getRow2() + endRowNum);

      // 插入新图片,返回的新图片序号无效
      destBook.addPicture(datas, picdata.getFormat());
      // 上面代码中新建图片的序号没有考虑原有图片数量,所以取原图片数量+1作为新图片的序号
      index++;
      _log.showDebug("---------copy new image index=" + index);

      destDraw.createPicture(anchor, index);
    }
  }