Jakata Poi HSSF: Pure Java Excel Solution

xiaoxiao2021-03-06  62

Microsoft's success on the desktop system makes us have to use its office products, such as: Word, Excel. Today, its source code still does not publicize our further application and development. However, in the area of ​​the server, Microsoft itself is not good, and the performance is poor. In our actual development, the performance layer has a variety of solutions, but IE browser has become the browser used by people because everyone uses Windows. In the corporate office system, there is often a customer request: You have to open our reports directly with Excel. Or: We have become accustomed to print with Excel. This is no problem with .NET development, but there is J2EE than. Net more promising open development environment, do you want to write a client control in order to solve the print request? Or use local code on the server side? The problem of the first program is that the processing of key data is sometimes not possible in the client, and the problem of the second solution is to sacrifice the portability and stability of the code. If the client is only responsible for processing a good report, it will be an attractive choice.

Apache's POI subproject, the goal is to process OLE2 objects. Currently compared the HSSF interface, process MS Excel (97-2002) objects. It is not like us only uses CSV's unfained things that can be converted by Excel, but a real Excel object, you can control some properties such as Sheet, Cell, etc. This is a young project, so it is still in the design like HDF. Other pure Java programs that support Word format have IText, but it is still in struggle. But HSSF is already mature and we use it. In addition, the implementation of Office in Wuxi is also a solution of pure Java, but it is also a fully commercial product, not public code project. In fact, from the perspective of development history, the original author of Staroffice in the Mid Year of the 1980s established Staroffice Suite in Germany, and then Summer STAROFFICE was acquired by Sun in Summer 1999, and released to StarOffice 5.2 in June 2000; and from Staroffice6 .0 start, StarOffice is based on the OpenOffice API, and the Office project of this public code has been taken for a long time. Although it is written by C , the POI's code part is also changed by OpenOffice. Therefore, it should be full of confidence in Poi. Some companies have used POI in the Web projects such as their office automation, such as the IOFFICE, Haitai's HTOFFICE, etc.

Java originally set core processing into Unicode, which is the benefit of another code to adapt to the multi-language environment. However, due to the only 26 letters in the foreigners, some programmers use 8 byte processing, and they have dropped the high level of CJK. Or because habits are hard coding in the program, there are also many reasons, making many Java applications are very worried on CJK processing. Fortunately, in POI HSSF, you can set eNCoding as double bytes.

Poi can be downloaded to www.apache.org. Compiled JAR mainly there are 4: POI package, POI Browser package, POI HDF package, POI HSSF routine package. When actually run, you need a POI package. If you compile and run with Jakarta Ant, download the SRC package in the Release of Apache Jakarta Poi, which has been generated for you to build Build files. Just run Ant (the installation of Ant is not said here). If you run with JBuilder, add the POI package in the new project. Take JBuilder6 as an example, select the CONFIG LIBRARIES for Tools menu items to create a new lib. Select the POI package in the pop-up menu, such as this Jakarta-Poi-1.5.1-Final-20020820.jar, add POI to jBuilder. Then, right-click your project, in the Project's Properties menu, Path's Required Libraries, click Add, add the Poi just added to the JBuilder to your current project. If you are just to be familiar with the use of POI HSSF, you can directly see the source code in the Samples package of POI and run it. The various objects of HSSF have a presentation. The routines provided by the HSSF are in the org.apache.poi.hssf.usermodel.examples package, with a total of 14, the generated target XLs are Workbook.xls. If you want to see more routines, you can refer to HSSF's JUnit Test Cases, there is in the source code of the POI package. HSSF has test code. Here is only the implementation of some routines.

HSSF is provided to the user's objects used in org.apache.poi.hssf.userModel packets, the main parts include Excell objects, style, and format, and auxiliary operation. There are several objects:

HSSFWORKBOOK EXCELL document object

HSSFSHEET Excell's form

HSSFrow Excell

HSSFCELL Excell's plaid unit

HSSFFONT Excell font

HSSFNAME name

HSSFDataFormat date format

There are 2 items in POI1.7:

HSSFHEADER Sheet head

HSSFFOOTER Sheet

And this style

HSSFCellStyle Cell style

Auxiliary operations

HSSFDateutil dates

HSSFPrintSetup print

HSSFERRORCONSTANTS error message table

Look carefully the structure of the org.apache.poi.hssf package, it is not difficult to find the MVC model in which HSSF's internal implementation follows

Here I use rose to reverse import the object in the org.apache.poi.hssf.usermodel package and organize according to the interrelationship, see the following two figures:

Figure 1 Basic object

It is not difficult to find that each basic object is associated with a Record object. The Record object is a reference record for the Office format.

Figure 2 HSSFWORKBOOK

HSSFWorkbook is an Excell object. This category is reflected in the relationship between HSSFWORKBOOK and basic objects. It can be seen that in many objects have also established a reference to Workbook. It is also important to establish a log mechanism POILogger in HSSFWorkbook and HSSFSHEET, and POILogger is also implemented using Apache log4j.

First see the easiest examples of the POI's Examples package, create an empty XLS file.

Import org.apache.poi.hssf.usermodel.hssfworkbook;

Import java.io.fileoutputstream;

Import java.io.ioException; Public Class Newworkbook

{

Public static void main (string [] args)

Throws oException

{

HSSFWORKBOOK WB = new hssfworkbook (); // Establish a new HSSFWORKBOOK object

FileOutputStream Fileout = New FileOutputStream ("Workbook.xls");

wb.write (fileout); // outputs the Workbook object to the fileworkbook.xls

Fileout.close ();

}

}

With this example, we build a blank XLS file (not empty file). On this basis, we can further see other examples.

Import org.apache.poi.hssf.usermodel. *;

Import java.io.fileoutputstream;

Import java.io.ioException;

Public Class Createcells

{

Public static void main (string [] args)

Throws oException

{

HSSFWORKBOOK WB = new hssfworkbook (); // Establish a new HSSFWORKBOOK object

HSSFSHEET Sheet = wb.createsheet ("New Sheet"); // Create a new Sheet object

// Create a row and put some cells in it. Rows Are 0 based.

HSSFROW ROW = Sheet.createrow ((Short) 0); // Establish a new line

// CREATE A Cell and Put a value in it.

HSSfcell Cell = row.createcell ((Short) 0); // Establish a new Cell

Cell.setcellValue (1); // Set the value of the integer type of Cell

// or do it on one line.

Row.createcell (Short) 1) .SetCellvalue (1.2); // Set the value of the Cell floating point type

Row.createcell ((Short) 2) .SetCellValue ("test"); // Set the value of the Cell character type

Row.createcell ((Short) 3) .setcellValue (TRUE); // Set the value of the Cell Boolean type

HSSFCellStyle CellStyle = wb.createcellStyle (); // Establish a new Cell style

CellStyle.SetDataFormat ("M / D / YY H: MM"); // Set the Cell Style to Customized Date Format

HSSfcell Dcell = row.createcell ((Short) 4);

Dcell.setcellValue (new date ()); // Set the value of Cell as a date type

Dcell.setcellStyle (CellStyle); // Setting the Cell Date of the display format

HSSFCELL CSCELL = Row.createcell ((Short) 5);

cscell.setencoding (hssfcell.encoding_utf_16); // Set Cell Code Solving Chinese High Byte Truncation

CSCell.setcellValue ("Chinese test _chinese words test"); // Set the Chinese and Western conforming strings

Row.createcell ((Short) 6) .SetCellType (hssfcell.cell_type_error); // Establish an error Cell // Write the Output to a file

FileOutputStream Fileout = New FileOutputStream ("Workbook.xls");

Wb.write (fileout);

Fileout.close ();

}

}

I a little modified the CreateCells class in the original Examples package to write the above functional test class. Through this example, we can clearly see several objects such as the XLS file from the large to small. We can set various types of values ​​in CELL. It is especially important to pay attention to if you want to display non-European and American characters, especially like the language of China, Japan, must set up HSSFCELL.Encoding_UTF_16, which can guarantee the high 8 digits of characters without truncation. Causes coding distortion to form garbled.

Other tests can grasp the detailed usage of POI by referring to the test example in the Examples package, including the setting of the font, CELL size, and low grain settings. It should be noted that POI is a project that is still in perfect public code, so some functions are expanding. If HSSFSHEET's getFooter () GetHeader () and SETFOTER (HSSFFHEADER HSH) are talented in Poi1.7, while Poi1.5 is not. Run Test familiar with code or use it to do a project, please pay attention to the version of POI.

It is also important to note that the HSSF also has its resolution of XLS based on events. You can refer to EventExample.java in the routine. This resolution is used in Org.apache.coCoon.serialization.hssfserializer in Apache Cocoon by implementing HSSFListener. Because cocoon2 is based on an event, POI provides a corresponding event in order to provide fast resolution. Of course, we can also implement this event interface.

Because POI is not a sufficiently mature project, it is necessary to make further development and testing. But it has provided us with a pure Java operation OLE2 object, and overcomes the defect call for OLE object calls, providing the server-side Excel solution

转载请注明原文地址:https://www.9cbs.com/read-88638.html

New Post(0)