Easy to deal with Excel form in Java

zhaozj2021-02-16  46

Source: www.javaworld.com

Author Elango Sundaram

summary

Many institutions use Microsoft Excel forms as information exchange, many non-professional programmers, business analysts, and project managers can use this technology. The ability to use Java from the Excel documentation, collect, and consolidate the ability of data, prove is very useful. JAKARTA POI (Poor Obfuscation Implementation, no confusion) enables programmers to quickly complete these tasks. POI can be used to perform the exchange of file formats based on Microsoft OLE (Object Links and Embedded) technology in pure Java to composite document formats such as software used by Microsoft Excel, Microsoft Word. This article analyzes POI and explains how to read and write Microsoft Excel documents using Java and provide an interesting instance using the POI API.

text

Whether your information on your hand is an asset stable table, the downloaded account information, bills or salary, which may eventually enter Microsoft Excel spare. Even if it is not a professional IT person, people will also be able to use Excel's data exchange technology. Jakarta Poi API is a wonderful party that makes it easy to access Microsoft document formats for Java programmers. The most mature API in Jakarta Poi is the HSSF (Horrible Spreadsheet Format) API, which is used to access the Excel document.

In this article, I will take you to create and read Excel documents, and using fonts and cell styles - all of this is done in Java.

POI glossary

The following is listed below, and the key terms related to POI are listed below:

l Poifs (Poor Obfuscation Implementation File System): Read, write Ole to the API group in the composite document format.

l HSSF (Horrible Spreadsheet Format): Read and write the API of Microsoft Excel.

l HDF (HORRIBLE Document Format): Read and write the API of Microsoft Word 97.

l HPSF (Horrible Property SET FORMAT): Read the API set by the property.

Create an Excel document

Jakarta OPI can be used to create an Excel document, the main steps are as follows:

l Create a file Workbook: hssfworkbook workbook = new hssfworkbook ();

l Create a worksheet called "Java Excels" in Workbook Worksheet: hssfsheet sheet = workbook.createsheet ("Java Excels");

l Create a new line Row in the table sheet Row: hssfrow rot = Sheet.createrow ((Short) 0);

l Create a single product Cell Cell: hssfcell cell = row.createcell (short);

l Place the content in units: Cell.SetCellValue ("Have a Cup of XL");

l Write Workbook into the file: Workbook.write (FileoutputStream);

Read data from Excel documents

In this example, you will see how to read values ​​from the Excel document.

Let us imagine an Excel form that will be used:

EMPLOYEE NAME

Specialization

Designation

Anbu

Programmingsenior Programmer PROGRAMMER

Jason

Banking industrial

Business Analyst

Ramesh

Databases

DBA

Mackyb

Accounting

Delivery Head

Key Steps to read the Excel table:

l Create a reference to the Excel document: hssfworkbook workbook = new fileinputstream (new fileinputstream (filetoberead);

l Quote: By default, reference to "0" of the first table in the Excel document: hssfsheet sheet = workbook.getsheetat (0); a table can also be referenced by its name. We assume that the name of this form is "sheet1". It can be referenced by this: hssfsheet sheet = workbook.getsheet ("sheet1");

l Quote: hssfrow rot = sheet.Getrow (0);

l A number of cells in the list: hssfcell cell = row.getcell ((Short) 0);

l Number of data values ​​in cells: Cell.getstringcellValue ();

Example:

Now suppose we want to get information about all public methods and data members in a JAR file, all of which can be collected into a table of a separate file. We expect to see the name of the class in the first column of the table, the second column is the field, the third list is a method, and each column is shown in red.

The program is to complete the following:

l Decompress the JAR file

l Read all class file code in the JAR file

l Loading these classes

l By reflex mechanism, get the name of the method and field

l Write information such as the class and fields using Jakarta Poi to the Excel table

Now we focus on using Jakarta Poi's most interesting steps:

l Create a new Excel document: workbook = new hssfworkbook ();

l Create a worksheet in the Excel document and naming for this table: Sheet = Workbook.createsheet ("Java Class Info");

l Set the width of the first three columns: Sheet.SetColumnWidth (Short) 0, (Short) 1000);

l Create a head: hssfrow rot = sheet.createrow ((Short) 0);

l Create a style of fonts and cells:

HSSFFONT FONT = Workbook.createFont ();

Font.SetColor (hssffont.color_red);

Font.setBoldweight (HSSFFont.BoldWeight_Bold);

// Create the Style

HSSfcellStyle CellStyle = workbook.createcellstyle ();

CellStyle.SetFont (font);

l Use the cell style:

HSSfcell Cell = row.createcell ((Short) 0);

Cell.setcellStyle (CellStyle);

Cell.setcellType (hssfcell.cell_type_string);

Cell.SetCellValue ("class name"); l Output file:

FileOutputStream Fout = New FileOutputStream (OutputFile);

// Write the Excel Sheet

Workbook.write (fout);

Fout.flush ();

// done deal. Close IT.

Fout.close ();

to sum up

As shown in this article, Java developers do not need to be critical in the face of the Excel form. We can easily deal with the Excel documentation. Drink a cup of coffee, put Excel after the brain.

About author

Elango Sundaram is a senior Java programmer, dedicated to research distributed computing systems, agents based on technologies and object-oriented methods. He has a master's degree in Virginia University and composes distributed computing using jini and WebSphere Studio Application Developer Tail Plug-in (for WSAD 4.0).

references

This article download: http://www.javaworld.com/javaworld/jw-03-2004/poi/jw-0322-poi.zip

Jakarta Poi Site: http://jakarta.apache.org/poi/

For more information about POI, please refer to "It's Poi-Fect," Tony Sintes (JavaWorld, May 2002): http://www.javaworld.com/javaworld/javaqa/2002-05/01-qa-0503-excel3.html

For more java tools, please visit the JavaWorld Development Tools: http://www.javaworld.com/channel_content/jw-tools-index.shtml

More open source tools

,

Please refer to

Erik Swenson's Open Source Profile

Column:

http://www.javaworld.com/columns/jw-opensource-index.shtml

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

New Post(0)