コード例 #1
0
ファイル: GetImgFromExcel.java プロジェクト: wulcome/sports
  /**
   * 获取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;
    }
  }
コード例 #2
0
 /** 用循环历遍excel表 第一层循环历遍sheet工作表 第二层循环历遍每一个表的行 第三层循环历遍每行的每一个单元格 */
 @SuppressWarnings("deprecation")
 private void getDataFromSheet() {
   mList = mworkBook.getAllPictures();
   for (int sheetCount = 0; sheetCount < msheetCount; sheetCount++) {
     HSSFSheet sheet = mworkBook.getSheetAt(sheetCount);
     getTitleFromSheet(sheet);
     for (int rowCount = 1; rowCount <= sheet.getLastRowNum(); rowCount++) {
       HSSFRow row = sheet.getRow(rowCount);
       if (row == null) continue;
       mperson = new Person();
       for (short cellCount = 0; cellCount < row.getLastCellNum(); cellCount++) {
         mcell = row.getCell(cellCount);
         if (mcell == null) continue;
         getCellData(cellCount);
       }
       getHeadPhoto(rowCount);
       mAdd.add(DataManager.RequestCode.SrcCode.SRC_EXCEL, mperson);
     }
   }
 }
コード例 #3
0
  /**
   * 复制来源表格中第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);
    }
  }