PowerPoint에 Excel을 Java의 OLE 개체로 포함

Excel 스프레드시트가 포함된 PowerPoint 프레젠테이션을 동료나 고객에게 배포하려는 경우 Excel 데이터를 프레젠테이션에 포함하는 것이 가장 좋습니다. 원본 Excel 문서에 액세스할 수 있습니다.

이 기사에서는 Spire.Office for Java를 사용하여 Excel을 PowerPoint에 OLE 개체로 포함하는 방법을 소개합니다.

Spire.Office.jar 설치



Maven 프로젝트를 생성하는 경우 다음 구성을 사용하여 애플리케이션에서 jar를 쉽게 가져올 수 있습니다. 비 Maven 프로젝트의 경우 this link에서 jar 파일을 다운로드하고 애플리케이션의 종속성으로 추가합니다.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.office</artifactId>
        <version>4.2.0</version>
    </dependency>
</dependencies>


관련된 주요 단계



1단계. com.spire.xls.Workbook 네임스페이스 아래에 있는 Workbook 클래스의 인스턴스를 생성하여 Excel 문서를 로드합니다. saveToImage(int firstRow, int firstColumn, int lastRow, int lastColumn) 메서드를 호출하여 선택한 셀 범위를 이미지로 변환합니다. 이미지가 파워포인트에 삽입되어 삽입할 엑셀 아이콘으로 표시됩니다. 다른 이미지를 아이콘으로 지정할 수도 있습니다.

2단계. 프레젠테이션 개체를 만듭니다. 새 PowerPoint 문서를 만들거나 기존 PowerPoint 파일을 로드하는 데 사용할 수 있습니다. 그런 다음 프레젠테이션의 ImageCollection에 이미지를 추가합니다.

Step 3. 엑셀 문서를 OLE 객체 생성을 위한 핵심 파라미터 중 하나인 byte[]로 변환한다.

4단계. 슬라이드에 OLE 개체를 추가한 다음 OLE 개체 유형 및 디스플레이 이미지를 지정합니다.

코드 사용




import com.spire.presentation.FileFormat;
import com.spire.presentation.IOleObject;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSizeType;
import com.spire.presentation.drawing.IImageData;
import com.spire.xls.Workbook;

import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;

public class EmbedExcel {

    public static void main(String[] args) throws Exception {

        //Specify excel path
        String excelPath = "C:\\Users\\Administrator\\Desktop\\Product.xlsx";

        //Create a Workbook object to load excel file
        Workbook wb = new Workbook();
        wb.loadFromFile(excelPath);

        //Set the margins to 0 so that only the selected cell range data will display in slide
        wb.getWorksheets().get(0).getPageSetup().setBottomMargin(0);
        wb.getWorksheets().get(0).getPageSetup().setTopMargin(0);
        wb.getWorksheets().get(0).getPageSetup().setLeftMargin(0);
        wb.getWorksheets().get(0).getPageSetup().setRightMargin(0);

        //Convert the selected range to image
        BufferedImage image = wb.getWorksheets().get(0).saveToImage(1,1,5,5);

        //Create a Presentation object
        Presentation ppt = new Presentation();
        ppt.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //Append image to ppt
        IImageData oleImage = ppt.getImages().append(image);

        //Load the excel file and convert it to byte[] object
        File excelFile = new File(excelPath);
        FileInputStream inputStream = new FileInputStream(excelFile);
        byte[] data = new byte[(int) excelFile.length()];
        inputStream.read(data, 0, data.length);

        //Create a Rectangle2D object
        Rectangle2D rect = new Rectangle2D.Float(60, 60, image.getWidth(), image.getHeight());

        //Insert the Excel file as an OLE object to the first slide
        IOleObject oleObject = ppt.getSlides().get(0).getShapes().appendOleObject("excel", data, rect);
        oleObject.getSubstituteImagePictureFillFormat().getPicture().setEmbedImage(oleImage);
        oleObject.setProgId("Excel.Sheet.12");

        //Save to another file
        ppt.saveToFile("InsertOle.pptx", FileFormat.PPTX_2013);
    }
}


좋은 웹페이지 즐겨찾기