Java中使用JCOM操作Office对象_MySQL
通过使用COM技术,我们用微软Office应用程序能够建立很多应用程序扩展,但是Java开发人员却无法享受它带来的便利--除非他们拥有方便的Java访问COM的途径(Java-to-COM桥)。使用JCom的时候,你可以在Java中控制几乎所有的COM对象,而且它还带有一些用于Excel的强大的辅助类。
在你每次编写用HTML表格样式或Java表格对象显示数据的应用程序的时候,通常都需要带有"导出到Excel"功能。那么头疼的问题就出现了。怎么样实现这种功能呢?在HTML中显示的可以在Office 2003中处理吗?没有这么好!你还必须支持Office 97!
你只能去找一个符合当前需求的工具了,但是接着收到更多的要求了。"这能在Word中做到吗?Powerpoint能做到吗?能不能用调制解调器拨号到远程服务器上并发布数据?Java无法实现这些功能是什么意思啊?Java可以实现任何功能。"
感谢作为Java和COM桥梁的框架组件,它使你在遇到这些情况的时候都可以回答"Yes"。Java-COM桥梁使你能够根据自己的需要操作Windows组件--以前这是VB、C++和.NET开发人员的领地。你通过实现一个与DCOM后端(back end)对话的Java前端(front end),可以远离端对端(end-to-end)的COM系统。在本文的末尾,你可以使用其中一个Java-to-COM桥:它可以被命名为JCom。
Excel基础知识
开始之前,你需要首先从Sourceforge网站下载API。它包含了JCom所使用的Java类的所有源代码、C++代码和JCom用于配置Java和COM的编译好的DLL。把这个DLL放到你的Java主目录的/bin/目录下面,否则会出现问题。同时,为了不出现问题,还要正确地设置JAVA_HOME环境变量。JCom的大多数文档目前都是用日语写的,但是翻译工作正在进行中,因此以后会有些改进的。
下载和安装过程完成以后,用列表1中的代码试一试。这段代码会建立到Excel的JCom接口,并把"Hello World"写入第一个单元格中。你可以看到如图1所示的结果。尽管JCom是一个通用的COM类库,但是还是带有很多用于Excel的辅助类,这是因为Excel可能是最常用的自动化COM应用程序。这些辅助类可以为我们节省很多时间,它们使JCOM成为一个更好的类库了。
列表1:开始使用JCOM和Excel
import jp.ne.so_net.ga2.no_ji.jcom.excel8.*;
import jp.ne.so_net.ga2.no_ji.jcom.*;
public class testSimple
{
public static void main(String[] args) throws Exception {
ReleaseManager rm = new ReleaseManager();
try {
System.out.println("EXCEL is Starting...");
ExcelApplication excel = new ExcelApplication(rm);
excel.Visible(true);
ExcelWorkbooks xlBooks = excel.Workbooks();
ExcelWorkbook xlBook = xlBooks.Add();
ExcelWorksheets xlSheets = xlBook.Worksheets();
ExcelWorksheet xlSheet = xlSheets.Item(1);
ExcelRange xlRange = xlSheet.Cells();
xlRange.Item(1,1).Value("Hello, World!" );
}
catch(Exception e) { e.printStackTrace(); }
finally { rm.release(); }
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
