⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mswordexample.java

📁 SWT开发的Java调用COM的工具集
💻 JAVA
字号:
/*
 * Copyright (c) 2007 Software Wizards Pty Ltd, Victoria, Australia.
 * mailto:enquires@swz.com.au. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted. See the GNU General Public License
 * for more details.
 *
 * Redistribution of the SWTtoCOM software is not permitted as part of any
 * commercial product. Licensing terms for such distribution may be
 * obtained from the copyright holder.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package au.com.swz.swttocom.example.msword;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import au.com.swz.swttocom.example.msword.wordom.DocumentComposite;

public class MSWordExample extends Composite {
	private CTabFolder tabFolder;
	private MenuItem[] fileMenus;
	private CTabItem currentSelection = null;
	
	public MSWordExample(Composite parent, int style) {
		super(parent, style);
		Display display = Display.getDefault();
		
		tabFolder = new CTabFolder(this, SWT.TOP);
		tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
		tabFolder.setSimple(false);
		tabFolder.setSelectionBackground(new Color[] {
				display.getSystemColor(SWT.COLOR_TITLE_BACKGROUND),
				display.getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT) },
				new int[] { 90 }, true);
		tabFolder.setSelectionForeground(display.getSystemColor(SWT.COLOR_TITLE_FOREGROUND));

		GridLayout thisLayout = new GridLayout(1, true);
		thisLayout.marginWidth = 0;
		thisLayout.marginHeight = 0;
		thisLayout.numColumns = 1;
		thisLayout.makeColumnsEqualWidth = true;
		thisLayout.horizontalSpacing = 0;
		thisLayout.verticalSpacing = 0;
		this.setLayout(thisLayout);

		GridData layoutData = new GridData();
		layoutData.verticalAlignment = GridData.FILL;
		layoutData.horizontalAlignment = GridData.FILL;
		layoutData.widthHint = -1;
		layoutData.heightHint = -1;
		layoutData.horizontalIndent = 0;
		layoutData.horizontalSpan = 1;
		layoutData.verticalSpan = 1;
		layoutData.grabExcessHorizontalSpace = true;
		layoutData.grabExcessVerticalSpace = true;

		Menu menu = new Menu(getShell(), SWT.BAR);
		MenuItem fileMenuItem = new MenuItem(menu, SWT.CASCADE);
		fileMenuItem.setText("File");
		Menu fileMenu = new Menu(fileMenuItem);
		fileMenus = new MenuItem[4];
		
		fileMenus[0] = new MenuItem(fileMenu, SWT.CASCADE);
		fileMenus[0].setText("New Document");
		fileMenus[0].addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				openDocument(null);
			}
		});
		
		fileMenus[1] = new MenuItem(fileMenu, SWT.CASCADE);
		fileMenus[1].setText("Open Document");
		fileMenus[1].addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				FileDialog dialog = new FileDialog(MSWordExample.this.getShell(), SWT.OPEN);
				dialog.setText("Open MSWord Document");
				dialog.setFilterNames(new String[] { "Word Documents (*.doc;*.dot)", "All Files (*.*)"}); //$NON-NLS-1$ //$NON-NLS-2$
				dialog.setFilterExtensions(new String[] { "*.doc;*.dot", "*.*"}); //$NON-NLS-1$ //$NON-NLS-2$
				String fileName = System.getProperty("user.dir") + File.separator + "example.doc";
				dialog.setFileName(fileName);
				final String filePath = dialog.open();
				if (filePath != null) {
					openDocument(new File(filePath));
				}
			}
		});

		fileMenus[2] = new MenuItem(fileMenu, SWT.CASCADE);
		fileMenus[2].setText("Print");
		fileMenus[2].addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				if (tabFolder.getSelection() != null) {
					DocumentComposite docWin = (DocumentComposite) tabFolder.getSelection().getControl();
					docWin.getOleClientSite().exec(OLE.OLECMDID_PRINT, OLE.OLECMDEXECOPT_PROMPTUSER, null, null);
				}
			}
		});
		
		fileMenus[3] = new MenuItem(fileMenu, SWT.CASCADE);
		fileMenus[3].setText("XML Mail Merge");
		fileMenus[3].addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				if (tabFolder.getSelection() != null) {
					FileDialog dialog = new FileDialog(MSWordExample.this.getShell(), SWT.OPEN);
					dialog.setText("Open XML Data Source");
					dialog.setFilterNames(new String[] { "*.xml", "*.*"}); //$NON-NLS-1$ //$NON-NLS-2$
					dialog.setFilterExtensions(new String[] { "*.xml", "*.*"}); //$NON-NLS-1$ //$NON-NLS-2$
					String fileName = System.getProperty("user.dir") + File.separator + "example.xml";
					dialog.setFileName(fileName);
					final String filePath = dialog.open();
					if (filePath == null) {
						return;
					}
					DocumentComposite docWin = (DocumentComposite) tabFolder.getSelection().getControl();
					MSWordMailMerger merger = new MSWordMailMerger(docWin.getDocumentAutomation());
					try {
						XMLMailMergeDataSource dataSource = new XMLMailMergeDataSource(filePath);
						merger.merge(dataSource);
					} catch (Exception e) {
						MessageBox mb = new MessageBox(MSWordExample.this.getShell(), SWT.OK|SWT.ICON_ERROR);
						mb.setText("Error");
						mb.setMessage(e.toString());
						mb.open();
					}
				}
			}
		});
		
		fileMenuItem.setMenu(fileMenu);
		getShell().setMenuBar(menu);
	}

	private void openDocument(File docFile) {
		CTabItem tabItem = new CTabItem(tabFolder, SWT.CLOSE);
		tabFolder.setSelection(tabItem);
		DocumentComposite docWin;
		if (docFile == null) {
			tabItem.setText("New Document");
			docWin = new DocumentComposite(tabFolder, SWT.NONE);
		} else {
			tabItem.setText(docFile.getName());
			docWin = new DocumentComposite(tabFolder, SWT.NONE, docFile);
		}
		docWin.getDocumentAutomation().getApplication().setDisplayScrollBars(true);
		docWin.getOleFrame().setFileMenus(fileMenus);
		tabFolder.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				CTabItem newSelection = (CTabItem)event.item;
				if ((currentSelection != null) && (!currentSelection.isDisposed())) {
					DocumentComposite docWin = (DocumentComposite) currentSelection.getControl();
					docWin.deactivateInPlaceClient();
				}
				currentSelection = newSelection;
				DocumentComposite docWin = (DocumentComposite) newSelection.getControl();
				docWin.oleShow();
			}
		});
		tabItem.addListener(SWT.Dispose, new Listener() {
			public void handleEvent(Event e) {
				CTabItem tabItem = (CTabItem)e.widget;
				tabItem.getControl().dispose();
			}
		});
		tabItem.setControl(docWin);
		docWin.oleShow();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			shell.setText("Word Automation Example");
			new MSWordExample(shell, SWT.NULL);
			shell.setLayout(new FillLayout());
			Rectangle shellBounds = shell.computeTrim(0, 0, 800, 600);
			shell.setSize(shellBounds.width, shellBounds.height);
			shell.open();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -