Recently, I have been dealing with Excel many times, mostly repetitive and cumbersome tasks. I wonder if I can compile a small program to Operate Excel? I searched through the Internet in my free time and found a good thing, Java Excel API. Haha, use it!
The home page is http://www.andykhan.com/jexcelapi/index.html. The full text translation is omitted. Many articles on the Internet are basically translated into English websites.
Java Excel API: http://www.andykhan.com/jexcelapi/download.html. the latest version is jexcelapi v2.6.
My homepage:/files/dilejun/jexcelapi2.6.zip. After decompression, there will be tutorial and Doc. You can find what you want to know, but it is still in English. The most important thing is the jxl. jar file. With it, we can start programming.
The Java programming platform uses eclipse 3.1. First, create a new Java project, and then import jxl. jar. Create a package and then a class.
** How to import a jar file? Right-click the newly created Java project and select a property. In the Property pair
** Select the Java build path on the left side of the dialog box, click library on the right, and then press
** Add external jars (add external jars), find the jxl. jar, and click OK. Then, import the jar package.
First, read an Excel file.
Package readexcel;
Import java. Io. file;
Import java. Io. ioexception;
Import jxl .*;
Import jxl. Read. Biff. biffexception;
Public class readexcel {
/**
* @ Param ARGs
* @ Throws ioexception
* @ Throws biffexception
*/
Public static void main (string [] ARGs) throws biffexception, ioexception {
// Todo auto-generated method stub
Workbook myworkbook = Workbook. getworkbook (new file ("E: \ lejun. Di \ workspace \ excelapilearn \ myfile.xls "));
Sheet mysheet = myworkbook. getsheet (0 );
Cell a1 = mysheet. getcell (0, 0 );
String S = a1.getcontents (). tostring ();
System. Out. println (s );
Workbook. Close ();
}
}
This code snippet prints the content of cell A1 "E: \ lejun. Di \ workspace \ excelapilearn \ myfile.xls. Corresponding to excel, we need to first get a workbook, using workbook myworkbook = workbook. getworkbook (new file () to obtain an Excel Workspace. With this workspace, Sheet mysheet = myworkbook. getsheet (INT) can help us find the expected sheet (sheet starts from 0). Obviously, with sheet, we can perform Cell operations, use cell a1 = mysheet. getcell (0, 0.
Of course, this is a very simple implementation that may not meet our actual work needs. For more complex operations, you can view its help file or its source code.