PowerPoint에 Excel을 Java의 OLE 개체로 포함
12658 단어 excepowerpointembedjava
이 기사에서는 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);
}
}
Reference
이 문제에 관하여(PowerPoint에 Excel을 Java의 OLE 개체로 포함), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/eiceblue/embed-excel-in-powerpoint-as-ole-object-in-java-5675텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)