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

📄 controllertest.java

📁 java与windows的com桥,可以用来操作所有的com程序如word或者excel等
💻 JAVA
字号:
package com.jacob.test.excel;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.jacob.test.BaseTestCase;

/**
 * this test verifies that you can call toString() on a Variant extracted from
 * Excel that contains a 2 dimensional array of doubles. 1.14M5 and earlier blew
 * up on this because two objects pointed at the same windows memory space SF 1840487
 */
public class ControllerTest extends BaseTestCase {

	private Controller controller;

	protected void setUp() {
		controller = new Controller();
	}

	public void testGetColumnA() {
		List<String> list = controller.getColumnA(super
				.getWindowsFilePathToPackageResource("teste.xls", this
						.getClass()));
		assertEquals(50, list.size());
	}

	public void testGetColumnB() {
		List<String> list = controller.getColumnB(super
				.getWindowsFilePathToPackageResource("teste.xls", this
						.getClass()));
		assertEquals(40, list.size());
	}

	/**
	 * This class looks bad because it is a compressed version that was
	 * originally in 3 different files as part of a bug submission. I didn't
	 * want to simplify it because it might no longer demonstrate the problem we
	 * were trying to fix
	 */
	public class Controller {

		private List<String> columnA;

		private List<String> columnB;

		public List<String> getColumnA(String pathToTest) {
			load(pathToTest);
			return columnA;
		}

		public List<String> getColumnB(String pathToTest) {
			load(pathToTest);
			return columnB;
		}

		public void load(String pathToTest) {
			if (columnA == null || columnB == null) {
				File excelFile = new File(pathToTest);
				executaExcelCallBack(excelFile.getAbsolutePath(), "password");
			}
		}

		public void executaExcelCallBack(String path, String password) {
			// ComThread.InitSTA();
			ComThread.InitMTA();
			ActiveXComponent excel = new ActiveXComponent("Excel.Application");

			try {

				excel.setProperty("Visible", false);
				Dispatch workbooks = excel.getProperty("Workbooks")
						.toDispatch();

				Dispatch workbook = Dispatch.call(workbooks, "Open", path, // FileName
						3, // UpdateLinks
						false, // Readonly
						5, // Format
						password // Password
						).toDispatch();

				Dispatch sheets = Dispatch.call(workbook, "Worksheets")
						.toDispatch();
				System.out.println("Before executa");
				executa(excel, sheets);
				System.out.println("After executa");

				Dispatch.call(workbook, "Close", new Variant(false));
				Dispatch.call(workbooks, "Close");
				System.out.println("After Close");
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					Thread.sleep(1000);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("Before Quit");
				excel.invoke("Quit", new Variant[] {});
				try {
					Thread.sleep(1000);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("After Quit, Before Release()");
				ComThread.Release();
				System.out.println("After Release()");
			}
		}

		/**
		 * Constante para configurar a planilha em modo "Calculation" autom醫ico
		 */
		public static final int CALC_AUTOMATICO = -4105;

		/**
		 * Constante para configurar a planilha em modo "Calculation" manual
		 */
		public static final int CALC_MANUAL = -4135;

		/**
		 * Escreve um determinado valor em uma c閘ula da pasta em quest鉶. O
		 * valor 

⌨️ 快捷键说明

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