![](/img/trans.png)
[英]Can we embed HTML code in ppt through Java using aspose or POI Api?
[英]Can we embed Excel file in ppt through aspose slide java
我們可以通過 aspose slide java 將 Excel 文件作為鏈接嵌入到 ppt 中嗎? 目前我已經嘗試使用 Aspose slide ,該對象嵌入在 pptx 文件中,但在嘗試打開文件時出現異常。請給我一些功能指南。下面附上示例代碼。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import com.aspose.slides.AutoShape;
import com.aspose.slides.IOleObjectFrame;
import com.aspose.slides.ISlide;
import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.slides.ShapeType;
public class PPTTest1 {
public void setAsposeLicense() {
InputStream inputStream = null;
try {
License license = new License();
inputStream = getClass().getClassLoader().getResourceAsStream("C:\\work\\licence\\aspose.slides.lic");
license.setLicense(inputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (Exception e) {
inputStream = null;
}
}
}
public static void main(String[] args) throws IOException {
PPTTest1 ppt=new PPTTest1();
ppt.setAsposeLicense();
//Instantiate Prseetation class that represents the PPTX
Presentation pres = new Presentation();
//Access the first slide
ISlide sld = pres.getSlides().get_Item(0);
//Load an Excel file to Array of Bytes
File file=new File("C:\\work\\Demo_uploadt.xlsm");
int length=(int)file.length();
FileInputStream fstro = new FileInputStream(file);
byte[] buf = new byte[length];
fstro.read(buf, 0, length);
//Add an Ole Object Frame shape\
byte[] fileContent = Files.readAllBytes(file.toPath());
IOleObjectFrame ooff = sld.getShapes().insertOleObjectFrame(0,(float)0,(float) 0,100,400, "Excel.Sheet.10", fileContent);
ooff.setObjectData(fileContent);
pres.save("C:\\work\\OleEmbed.pptx", SaveFormat.Pptx);
System.out.println("ppt generated.........");
}
}
@Bachan約瑟夫,
我已經觀察到您共享的示例代碼,在 PowerPoint 演示文稿中添加 MS Excel OLE 對象時,示例代碼似乎沒有問題。 源文件可能有問題。 您能否分享您收到異常的源文件以及堆棧跟蹤,以便我們進一步觀察以幫助您。
我在 Aspose 擔任支持開發人員。
下面的代碼工作正常,我可以使用 Aspose 幻燈片將 excel 文件附加到 PPT
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.aspose.slides.IOleEmbeddedDataInfo;
import com.aspose.slides.IOleObjectFrame;
import com.aspose.slides.OleEmbeddedDataInfo;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
public class SetFileTypeForAnEmbeddingObject2 {
public static void main(String[] args) throws IOException {
Presentation pres = new Presentation();
try {
// Add known Ole objects
byte[] fileBytes = Files.readAllBytes(Paths.get("C:\\work\\Demo uploadt.xlsm"));
// Create Ole embedded file info
IOleEmbeddedDataInfo dataInfo = new OleEmbeddedDataInfo(fileBytes, "xls");
// Create OLE object
IOleObjectFrame oleFrame = pres.getSlides().get_Item(0).getShapes().addOleObjectFrame(150, 420, 250, 50,
dataInfo);
oleFrame.setObjectIcon(true);
pres.save("C:\\work\\" + "SetFileTypeForAnEmbeddingObject7.pptx", SaveFormat.Pptx);
} finally {
if (pres != null)
pres.dispose();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.