Jacob Operation ppt

Source: Internet
Author: User

Because of business reasons, now to use Java dynamic to generate PPT, all the previous technology has not been able to meet the bureau's technology, research for almost half a month, but also a small achievement bar, take out and share. Just started to find information on the Internet, this is really very scarce, is generally the operation of Word and Excel, PPT is very few. In code specifications, I hope that we have a lot of advice, there are problems can send me messages, communication and communication.

Package com.asiainfo.chn.anareport.util;

Import Java.io.File;
Import com.jacob.activeX.ActiveXComponent;
Import Com.jacob.com.ComThread;
Import Com.jacob.com.Dispatch;

Import com.jacob.com.Variant;

	public class Jacobpptutil {public static final int word_html = 8;

	public static final int word_txt = 7;

	public static final int excel_html = 44;
	
	public static final int ppsaveasjpg = 17;
	
	private static final String Add_chart = "Addchart";
	Private activexcomponent ppt;
	
	Private Activexcomponent presentation; /** * Constructs a new PPT * @param isvisble */Public Jacobpptutil (Boolean isvisble) {if (null = = ppt) {ppt = new ActiveX
			Component ("PowerPoint.Application");
			Ppt.setproperty ("Visible", New Variant (isvisble));
			Activexcomponent presentations = ppt.getpropertyascomponent ("Presentations");
		Presentation =presentations.invokegetcomponent ("Add", New Variant (1)); } public Jacobpptutil (String Filepath,boolean isvisble) throws exception{if (null = = FilePath||
		'. Equals (FilePath)} {throw new Exception ("File path is empty!");
		File File = new file (FilePath);
		if (!file.exists ()) {throw new Exception ("file does not exist!");
		ppt = new Activexcomponent ("PowerPoint.Application");
		Setisvisble (ppt, isvisble);
		Open an existing Presentation object activexcomponent presentations = ppt.getpropertyascomponent ("Presentations");
	Presentation = presentations.invokegetcomponent ("Open", new Variant (FilePath), new Variant (TRUE); /** * Convert ppt to Picture * * @param pptfile * @param savetofolder * @author liwx/public void Ppttojpg (String pp
			Tfile, String savetofolder) {try {saveAs (presentation, Savetofolder, ppsaveasjpg);
			if (presentation!= null) {Dispatch.call (presentation, "Close");
		} catch (Exception e) {comthread.release ();
			finally {if (presentation!= null) {Dispatch.call (presentation, "Close");
			} ppt.invoke ("Quit", new variant[] {});
		Comthread.release (); }/** * Play ppt * * @param pptfile
	 * @date 2009-7-4 * @author yhy/public void Pptshow (String pptfile) {//PowerPoint slide Show Settings Object activexcomponent
		setting = presentation. Getpropertyascomponent ("SlideShowSettings");
		Call the object's run function to achieve full-screen playback of Setting.invoke ("Run");
	Release control thread Comthread.release (); /** * ppt Save As * * @param presentation * @param saveto * @param ppsaveasfiletype * @date 2009-7-4 * @auth or yhy */public void SaveAs (Dispatch presentation, String saveto, int ppsaveasfiletype) throws Exception {DISPATC
	H.call (presentation, "SaveAs", SaveTo, New Variant (PpSaveAsFileType));
			/** * Close ppt and release thread * @throws Exception/public void Closeppt () throws exception{if (null!= presentation) {
		Dispatch.call (presentation, "Close");
		} ppt.invoke ("Quit", New variant[]{});
	Comthread.release (); /** * Run ppt * @throws Exception */public void runppt () throws exception{activexcomponent setting = Presentati
	On.getpropertyascomponent ("SlideShowSettings");	Setting.invoke ("Run"); /** * settings are visible * @param visble * @param obj/private void setisvisble (Dispatch obj,boolean visble) throws Excep 
	tion{dispatch.put (obj, "Visible", New Variant (visble)); /** * Add a new slide to a Slide object * @param slides * @param pptpage Slide number * @param type 4: Title + Table 2: Title + text 3: Title + Left/Right contrast text 5: Title + right-to-left text right-hand  Figure 6: Title + Left chart right text 7: Title +smartart Graphics 8: Title + chart * @return * @throws Exception/private Variant addpptpage (activexcomponent
	Slides,int pptpage,int type) throws exception{return Slides.invoke ("Add", new Variant (Pptpage), new variant (type)); /** * * @param the shapes of the Pageshapes page @param charttype chart type * @param leftdistance distance from the left border * @param top Distance distance from top border * @param width of the chart @param height of the height chart * @return * @throws Exception/Public Dispatch A
		Ddchart (Dispatch pageshapes,int charttype,int leftdistance,int topdistance,int width,int height) throws exception{ Variant chart = Dispatch.invoke (Pageshapes, Add_chart, 1, New object[]{new Integer (ChartType),//Chart type new integer (leftdistance),//distance from the left border of the new Integer (topdistance),//distance from the top frame 
	The new Integer (width),//The width of the chart new integer (height),//Chart height}, new int[1]);//error type return Chart.todispatch (); /** * Get the first few slides * @param index number, starting from 1 * @return * @throws Exception/public Dispatch getpptpage (int page
		Index) throws exception{//Get Slide object Activexcomponent slides = presentation.getpropertyascomponent ("slides");
		Get the first few ppt Dispatch pptpage = dispatch.call (slides, "Item", New Object[]{new Variant (PageIndex)}). Todispatch ();
		Dispatch.call (Pptpage, "select");
	return pptpage; /** * Add Table * @param pageshapes Page Object * @param rows Number * @param columns columns * @param leftdistance distance from the left * @pa Ram Topdistance distance from top * @param width * @param height * @return * @throws Exception/public Dispatch AD Dtable (Dispatch pageshapes, long rows, long columns, int leftdistance, int topdistance, int width, int Height) throws Exception {return Dispatch.invoke (pageshapes, "addtable", 1, new object[] {new Long (R) OWS), new Long (columns), new Integer (leftdistance), new Integer (topdistance), new Integer (width), new integer (
	Height)}, new Int[1]). Todispatch (); /** * Modifies the value of the text object on the Selection object * @param selectionobj * @param value * @throws Exception/public void Addtex TValue (Dispatch selectionobj,string value) throws exception{Dispatch shaperange= (Dispatch) dispatch.get (selectionobj
		, "ShapeRange"). Getdispatch (); 
		Dispatch textframe= (Dispatch) dispatch.get (ShapeRange, "TextFrame"). Getdispatch (); 
		Dispatch textrange= (Dispatch) dispatch.get (TextFrame, "TextRange"). Getdispatch ();
		Dispatch.call (TextRange, "select");
	Dispatch.put (TextRange, "Text", value); /** * Add data to the established cell * @param Cell Object * @param value the data you want to add * @throws Exception/public void Addcellval UE (Dispatch cell,object value) throws exception{Dispatch Cellshape = DiSpatch.get (Cell, "Shape"). Todispatch ();
		Dispatch cellframe = Dispatch.get (Cellshape, "TextFrame"). Todispatch ();
		Dispatch CellRange = Dispatch.get (Cellframe, "TextRange"). Todispatch ();
	Dispatch.put (CellRange, "Text", value);
	 /** * Merge cells, the contents of the original two cells after merging will be placed inside a cell * if you span several cells between the start cell and the end cell, it will be merged together * @param cell start cell * @param cell2 end Cell * @return * @throws Exception * * public void Mergecell (Dispatch cell,dispatch cell2) throws exception{
	Ke (cell, "Merge", 1, New Object[]{cell2}, new int[1]); /** * Get the form cell * @param tableobj Table Object * @param rownum The first few lines, starting from 1 * @param columnrum The first few columns, starting from 1 * @return * @t  Hrows Exception * * Public Dispatch getcelloftable (Dispatch tableobj,int rownum,int columnrum) throws exception{return Dispatch.invoke (Tableobj, "Cell", Dispatch.method, new Object[]{new Long (rownum), New Long (Columnrum)}, new Int[1]).
	Todispatch (); /** * Set cell background color * @param cellobj * @param colorindex * @throws Exception/Public void Setcellbackcolor (Dispatch cellobj,int colorindex) throws exception{Dispatch Cellshape = Dispatch.get (Cellobj, "Sh
		Ape "). Todispatch ();
		Dispatch fillobj = Dispatch.get (Cellshape, "Fill"). Todispatch ();
		Dispatch BackColor = Dispatch.get (Fillobj, "ForeColor"). Todispatch ();
		Dispatch.put (BackColor, "Objectthemecolor", ColorIndex);
	Dispatch.put (Fillobj, "ForeColor", BackColor); /** * Modify the style of the table, the default style is: {5c22544a-7ee6-4342-b048-85bdc9fd1c3a} * @param tableobj Table Object * @param styleid style ID * @thro
			WS Exception */public void Edittablesyle (Dispatch tableobj,string styleid) throws exception{if (null = = Tableobj) {
		throw new Exception ("Invalid Table Object!"); } if (null = = Styleid | |
		"". Equals (Styleid)) {throw new Exception ("Invalid style id!");}
	Dispatch.invoke (Tableobj, "ApplyStyle", Dispatch.method, New Object[]{styleid}, new int[1]); /** * Add columns to the Table object * @param tableobj * @param beforecolumn * @throws Exception/public void Addtablecolumn (Dispatch tableobj,int beForecolumn) throws exception{Dispatch columns = Dispatch.get (tableobj, "columns"). Getdispatch ();
		int count = dispatch.get (columns, "count"). GetInt ();
		if (BeforeColumn > Count | | BeforeColumn < 1) {throw new Exception ("Invalid column index!");
	} dispatch.invoke (Columns, "Add", Dispatch.method, New Object[]{beforecolumn}, new int[1]); /** * Add rows to the Table object * @param tableobj * @param beforecolumn * @throws Exception/public void Addtablerow (Di
		Spatch tableobj,int beforerow) throws exception{Dispatch rows = Dispatch.get (Tableobj, "Rows"). Getdispatch ();
		int count = Dispatch.get (rows, count). GetInt ();
		if (BeforeRow > Count | | beforerow <1) {throw new Exception ("Invalid row index!");
	} dispatch.invoke (Rows, "Add", Dispatch.method, New Object[]{beforerow}, new int[1]); /** * Modify the chart type of individual columns * @param chartobj Chart Object * @param seriindex series index, starting from 1 * @param charttype chart type * @throws excep tion */public void Updatesericharttype (Dispatch chartobj,int seriindex,intChartType) throws exception{Dispatch Seri1 = Dispatch.call (chartobj, "SeriesCollection", New Variant (Seriindex)). 	
        Todispatch ();
	Dispatch.put (Seri1, "ChartType", ChartType); /** * Sets whether to display the data table for the chart, when the new table is not displayed by default * @param chartobj Table Object * @param bvalue boolean value, Ture is displayed, False is not displayed * @throws exce Ption */public void setisdispdatatable (Dispatch chartobj,boolean bvalue) throws exception{if (null = = Chartobj) {T
		Hrow new Exception ("Invalid chart Object!");
	} dispatch.put (Chartobj, "hasdatatable", bvalue); /** * Gets the style ID of the table * @param tableobj * @return * @throws Exception */Public String Gettablestyleid (Dispatch ta
		Bleobj) throws exception{Dispatch TableStyle = Dispatch.get (tableobj, "Style"). Todispatch ();
	Return Dispatch.get (TableStyle, "Id"). ToString (); /** * Set whether the data table is displayed on the chart * @param chartobj * @param value * @throws Exception/public void sethasdatatable (Disp Atch Chartobj,boolean value) throws exception{Dispatch.put (chartobj, "hasdatatable", value);
	public void Getgenerageppt () throws Exception {//Generate a new PPT object Dispatch windows = Presentation.getproperty ("W
		Indows "). Todispatch ();
		Dispatch window = Dispatch.call (Windows, Item, new Variant (1)). Todispatch ();
		Dispatch selection = dispatch.get (window, "Selection"). Todispatch ();

		Gets the Slide object activexcomponent slides = presentation.getpropertyascomponent ("slides");
		
		Add first slide; title + subtitle Addpptpage (slides, 1, 1);
		Dispatch sliderange= (Dispatch) dispatch.get (selection, "SlideRange"). Getdispatch ();
		Dispatch shapes= (Dispatch) dispatch.get (slideRange, "shapes"). Getdispatch ();
		Gets the first element in the slide Dispatch shape1 = dispatch.call (shapes, Item, new Variant (1)). Todispatch ();
		Gets the second element in the slide Dispatch shape2 = dispatch.call (shapes, Item, New Variant (2)). Todispatch ();
		Select the first element Dispatch.call (shape1, "select");
		Add Value Addtextvalue (Selection, "Test main title");
		Operation of the second shape Dispatch.call (shape2, "select") on a PPT page;
		Addtextvalue (Selection, "Test subtitle");
		Add second slide (title + chart)Variant v = addpptpage (slides, 2, 8);
		Gets the second ppt object Dispatch ppttwo = V.getdispatch ();
		Activates the current PPT object Dispatch.call (ppttwo, "select");
		Get the shapes and shapes in ppt = Dispatch.get (ppttwo, "shapes"). Todispatch ();
		Dispatch shapetext = dispatch.call (shapes, "Item", New Variant (1)). Todispatch ();
		Operation title Dispatch.call (Shapetext, "select");
		Addtextvalue (Selection, "Test chart title");
		Add chart Dispatch chartdisp = Addchart (shapes, 2, 10, 130, 700, 200);
		
		Dispatch Chartobj=dispatch.get (Chartdisp, "Chart"). Getdispatch ();
		
		Sethasdatatable (Chartobj, true);
		
		Dispatch Chartdata=dispatch.get (chartobj, "ChartData"). Todispatch ();
		
		Variant Chartstylev=dispatch.get (chartobj, "ChartFont");
		
		Dispatch Workbook=dispatch.get (chartData, "Workbook"). Getdispatch ();
		
		Dispatch worksheets=dispatch.get (Workbook, "worksheets"). Getdispatch ();
		
		Dispatch Worksheetitem = dispatch.call (worksheets, "Item", New Variant (1)). Todispatch (); Dispatch cell = Dispatch.invoke (Worksheetitem, "RanGE ", Dispatch.get, new object[] {" B3 "}, New Int[1]). Todispatch ();
        Dispatch.put (Cell, "Value", 12); 7606//Modify the chart type of a single series//dispatch Seri1 = Dispatch.call (chartobj, "SeriesCollection", New Variant (3)). Todisp 	
        Atch ();
        Dispatch.put (Seri1, "ChartType", 4);
		Updatesericharttype (Chartobj, 3, 4);
		
		
		Dispatch ChartArea = Dispatch.get (chartobj, "ChartArea"). Todispatch ();
		Table/*dispatch tabledisp = addtable (Shapes, 3, 4, 50, 130, 600, 300);
		
		Dispatch Tableobj=dispatch.get (Tabledisp, "Table"). Getdispatch (); Modify Table Style Dispatch.invoke (tableobj, "ApplyStyle", 1, New object[]{"{5c22544a-7ee6-4342-b048-85bdc9fd1c3a}"}, new int[1
		]);
		Get cells Dispatch cell = getcelloftable (Tableobj, 1, 1);
		Dispatch cell2 = getcelloftable (Tableobj, 2, 1);
		Addcellvalue (cell, "Test play");
		Addcellvalue (cell2, "haha");
		Merge cell//mergecell (cell, cell2);
		Addtablecolumn (Tableobj, 4); Addtablerow (Tableobj, 3); *//PowerPoint slide ShowSet Object Activexcomponent setting = Presentation.getpropertyascomponent ("SlideShowSettings"); 

		Setting.invoke ("Run");
		Save ppt Presentation.invoke ("SaveAs", New Variant ("D:/a.ppt")); 
		Release control thread Comthread.release ();
	Closeppt (); /** * Parse existing PPT * @param filePath * @throws Exception * * public void Invokeppttemplate (String filePath) throws E
		xception{Dispatch windows = Presentation.getproperty ("Windows"). Todispatch ();
		Dispatch window = Dispatch.call (Windows, Item, new Variant (1)). Todispatch ();
		Get the first few ppt Dispatch pptpage = getpptpage (1);
		Dispatch selection = dispatch.get (window, "Selection"). Todispatch ();
		Dispatch Sliderange=dispatch.get (Selection, "SlideRange"). Getdispatch ();
		Dispatch Shapes=dispatch.get (slideRange, "shapes"). Getdispatch ();
		Gets the nth element in the slide Dispatch tabledisp = dispatch.call (shapes, Item, New Variant (2)). Todispatch ();
		Convert to Table object Dispatch tableobj = Dispatch.get (Tabledisp, "Table"). Todispatch (); Gets the properties of the object's style DispAtch TableStyle = Dispatch.get (tableobj, "Style"). Todispatch ();
		System.out.println (Dispatch.get (TableStyle, "Id"));
		{91ebbbcc-dad2-459c-be2e-f6de35cf9a28}/*//Select the first element Dispatch.call (shape1, "select");
		
		Dispatch Chartobj=dispatch.get (Shape1, "Chart"). Getdispatch ();
		
		Dispatch Chartdata=dispatch.get (chartobj, "ChartData"). Todispatch ();
		
		Dispatch Workbook=dispatch.get (chartData, "Workbook"). Getdispatch ();
		
		Dispatch worksheets=dispatch.get (Workbook, "worksheets"). Getdispatch (); 		 
		
		Dispatch Worksheetitem = dispatch.call (worksheets, "Item", New Variant (1)). Todispatch (); Dispatch cell = Dispatch.invoke (Shape1, "Range", Dispatch.get, new object[] {"B3"}, New Int[1]). Todis  Patch (); ///Modify the chart type of a single series//dispatch Seri1 = Dispatch.call (chartobj, "SeriesCollection", New Variant (1)). TODISPATC 	
        H ();
		
		Dispatch.put (Seri1, "ChartType", "4");
		Dispatch ChartArea = Dispatch.get (chartobj, "ChartArea"). Todispatch ();
Clear chart Content		Dispatch.call (ChartArea, "clearcontents");
		PowerPoint slide Show Set object activexcomponent setting = Presentation.getpropertyascomponent ("SlideShowSettings");
		Setting.invoke ("Run");
		Save ppt//presentation.invoke ("SaveAs", New Variant (FilePath)); 
	Release control thread Comthread.release ();
		public static void Main (string[] strs) throws exception{String filepath= "D:/a.ppt";
		Jacobpptutil jac = new Jacobpptutil (true);
		Jacobpptutil jac = new Jacobpptutil (filepath,true);
		Jac.getgenerageppt ();
		Jac.invokeppttemplate (FilePath);
		String templatesourcepath= "d:/pptsource/Unified Template _ Market. ppt";
	String ppttargetpath= "d:/ppttarget/Unified Template _ Market _new.ppt";

 }

}

java.lang.UnsatisfiedLinkError:no Jacob in Java.library.path

Download the Jacob.rar, extract the Jacob.dll and Jacob.jar then:
1) put Jacob.dll in C:\Program files\java\jdk1.5.0_08\bin, C:\Program Files\java\jdk1.5.0_08\jre\bin, C:\WINDOWS\system32 Table of Contents.
2 Put the Jacob.jar into the project's LIB package and load the jar package in the Java build path.
3 run the project can be compiled through.

Note:
1 This configuration does not need to configure the environment variables (such as: Path,classpath).



Reference article: http://lwx-1987.iteye.com/blog/1132960

http://blog.163.com/wm_at163/blog/static/13217349020114166107230/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.