JAVA uses the COM interface to operate PPT
I. background description
In the Eclipse environment, you can develop a JAVA code operation PPT and modify the PPT template. Including modifying text labels, charts, and tables. To meet the requirements of most software to generate a PPT report, you must first create a template and modify the template data in the program.
2. Development Environment Construction
Download the open-source components of jacob, decompress the package, and add jacob. jar to the project. Copy the corresponding dll file to the Path. The 32-bit system uses X86 dll and the 64-bit system uses X64 dll.
Iii. Merge PPT function 1.1 merge PPT
/*** Merge multiple ppts. The output file and merged file must already exist. No new file is created. * @ Param outPutPPTPath: the output file path after merging. * @ Param mergePPTPathList: append the merged files in sequence. */Public synchronized static void merge (String outPutPPTPath, List
MergePPTPathList) {// start the office PowerPoint program ActiveXComponent pptApp = new ActiveXComponent ("PowerPoint. application "); Dispatch. put (pptApp, "Visible", new Variant (true); Dispatch presentations = pptApp. getProperty ("Presentations "). toDispatch (); // open the output file Dispatch outputPresentation = Dispatch. call (presentations, "Open", outPutPPTPath, false, false, true ). toDispatch (); // cyclically Add the merged file for (String mergeFile: mergePPTPathList) {Dispatch mergePresentation = Dispatch. call (presentations, "Open", mergeFile, false, false, true ). toDispatch (); Dispatch mergeSildes = Dispatch. get (mergePresentation, "Slides "). toDispatch (); int mergePageNum = Dispatch. get (mergeSildes, "Count "). toInt (); // close the Dispatch of the merged file. call (mergePresentation, "Close"); Dispatch outputSlides = Dispatch. call (outputPresentation, "Slides "). toDispatch (); int outputPageNum = Dispatch. get (outputSlides, "Count "). toInt (); // append the content of the file to be merged to the end of the output file. call (outputSlides, "InsertFromFile", mergeFile, outputPageNum, 1, mergePageNum);} // Save the output file and close and exit PowerPonit. dispatch. call (outputPresentation, "Save"); Dispatch. call (outputPresentation, "Close"); Dispatch. call (pptApp, "Quit ");}
1.2 call the test code
public static void main(String[] args) throws OpenXML4JException, IOException, XmlException { String outPutPPTPath = "D:\\TEMP\\template\\1.pptx"; List
mergePPTPathList = new ArrayList
(); mergePPTPathList.add("D:\\TEMP\\template\\2.pptx"); mergePPTPathList.add("D:\\TEMP\\template\\3.pptx"); MergePPT.merge(outPutPPTPath, mergePPTPathList);; }