2011-11-18 4 views
3

내 애플리케이션에서 POI API를 사용하여 Excel 파일을 생성 중입니다. 문서의 왼쪽 상단에있는 셀 중 하나에 이미지가 저장됩니다. 내가 얻고있는 문제는 이미지를 보유하고있는 셀을 채우기 위해 이미지가 늘어나고 있다는 것입니다.이미지를 POI를 사용하여 Excel로 확대

그림 개체에서 사용하는 크기 조정 방법에 따라 이미지의 크기가 다양하지만 이미지의 가로 세로 비율은 항상 내부에있는 셀의 가로 세로 비율에 따라 달라집니다. 비율.

이 내 코드입니다 :

titleRow = sheet.createRow(0); 
      titleRow.setHeightInPoints(25f); 

      titleRow.createCell(0); 

      sheet.getRow(0).getCell(0).setCellStyle(defaultTitleStyle(wb)); 

      WebApplicationContext webCtx = ((WebApplicationContext)AtlasApplicationUtils.getCurrentApplication().getContext()); 
      ServletContext sc = webCtx.getHttpSession().getServletContext(); 
      String contextPath = sc.getContextPath(); 
//   sc.getResourceAsStream(contextPath + "/VAADIN/themes/m2m/../m2m/img/gnd_logo_white.png") 
      String f = new File("").getAbsolutePath(); 
      InputStream is = new FileInputStream(System.getProperty("user.dir") + "/src/main/webapp/VAADIN/themes/m2m/img/gnd_logo_white.png"); 
      byte[] bytes = IOUtils.toByteArray(is); 
      int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG); 
      is.close(); 

      CreationHelper helper = wb.getCreationHelper(); 
      Drawing drawing = sheet.createDrawingPatriarch(); 
      ClientAnchor anchor = helper.createClientAnchor(); 

      anchor.setCol1(0); 
      anchor.setRow1(0); 
      sheet.autoSizeColumn(0); 
      Picture pict = drawing.createPicture(anchor, pictureIdx); 
      pict.getPreferredSize(); 
      pict.resize(0.25); 

스타일링 방법 :

protected CellStyle defaultTitleStyle(final HSSFWorkbook wb) { 

     CellStyle style; 
     final Font titleFont = wb.createFont(); 


     titleFont.setFontHeightInPoints((short) 18); 
     titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD); 
     titleFont.setColor(IndexedColors.GREY_80_PERCENT.getIndex()); 

     style = wb.createCellStyle(); 
     style.setAlignment(CellStyle.ALIGN_CENTER); 
     style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); 
     style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex()); 
     style.setFillPattern(CellStyle.SOLID_FOREGROUND); 
//  style.setFillBackgroundColor(IndexedColors.BLUE_GREY.getIndex()); 
     style.setFont(titleFont); 

     HSSFPalette palette = wb.getCustomPalette(); 
     palette.setColorAtIndex(HSSFColor.BLUE_GREY.index, 
       (byte) 7, 
       (byte) 64, 
       (byte) 127); 

     palette.setColorAtIndex(HSSFColor.GREY_80_PERCENT.index, 
       (byte) 228, 
       (byte) 228, 
       (byte) 228); 


     return style; 

사람이이 문제를 방지하고 이미지가 원래 비율을 유지 할 수있는 방법을 알고 있나요?

답변

5

documentation에는 약간의 경고가 있습니다.

공극 크기 조정()

원래 크기로 이미지를 초기화.

기본 글꼴 크기 (.xls의 경우 10pt, .xlsx의 경우 11pt)의 통합 문서에서만이 방법이 올바르게 작동합니다. 기본 글꼴이 변경된 경우 크기가 조절 된 이미지는 가로 또는 세로로 늘어납니다 ( ).

+1

확인을 추가 할 수 있다고 생각 답변 주셔서 감사합니다. 하나의 질문이지만, 기본 글꼴로 설정하는 방법을 알고 있습니까? 내가 사용했던 모든 글꼴 사양을 제거하려고 시도했지만 어떤 차이도 만들지 못했습니다. – AndroidHustle

0

나는, 당신이

anchor.setAnchorType(ClientAnchor.DONT_MOVE_AND_RESIZE); 
관련 문제