Java POI 1, use of Java POI

Source: Internet
Author: User

Many times, a software application needs to generate a report in Microsoft Excel file format. Sometimes, an application might even want to use an Excel file as input data. For example, an application developed by a company will need all the output from the Finance department to generate its own Excel.

Any Java programmer willing to use the output of MS Office files can be done using pre-defined and read-only APIs.

What is Apache POI?

Apache POI is a popular API that allows programmers to create, modify, and display MS Office files using Java programs. This is an open source library developed by the Apache Software Foundation that uses Java distributed design or modifies Microsoft Office files. It contains classes and methods to decode user input data or files into MS Office documents.

Apache POI Component

Apache POI contains classes and methods to compound all of the MS Office OLE 2 documents. The list of this API component is as follows.

    • poifs (poor obfuscation technology for file systems): This component is a fundamental factor for all other POI components. It is used to explicitly read different files.

    • HSSF (horrible spreadsheet format): It is used to read and write the XLS format of the Ms-excel file.

    • XSSF (XML format): It is used for ms-excel in the xlsx file format.

    • HPSF (horrible property formatting): It is used to extract Ms-office file property settings.

    • HWPF (terrible word processor format): It is a file used to read and write Ms-word's document extension.

    • XWPF (XML word Processor format): It is a file used to read and write Ms-word docx extensions.

    • HSLF (Scary Slide layout format): It is used to read, create, and edit PowerPoint presentations.

    • HDGF (Scary Chart format): It contains classes and methods for Ms-visio binary files.

    • HPBF (horrible publisher format): It is used to read and write Ms-publisher files.

Next, the main introduction of the use of HSSF components, direct generation (GAN) code (HUO) One, commonly used settings

Generate Excel Table
Hssfworkbook WorkBook = new Hssfworkbook ();

New sheet

Hssfsheet sheet = Workbook.createsheet ();

Sheet.autosizecolumn (1, true);

Defining styles
Hssfcellstyle Contentstyle = Workbook.createcellstyle ();
Hssffont Contentfont = Workbook.createfont (); Defining fonts
Contentfont.setfontname ("Microsoft Jas Black");//Set Font
Contentfont.setfontheightinpoints ((short) 10);//Set Font size
Contentfont.setbold (TRUE);//Set Bold
Contentfont.setcolor (HSSFColor.WHITE.index);//Set Font color
Contentstyle.setfont (Contentfont);
Contentstyle.setalignment (HorizontalAlignment.Center);//center Around
Contentstyle.setverticalalignment (verticalalignment.center);//Vertical Center

Hssfcellstyle contentstyletemp = Workbook.createcellstyle ();
Contentstyletemp.clonestylefrom (Contentstyle);//Clone style
Contentstyletemp.setfillforegroundcolor (HSSFColor.HSSFColorPredefined.ROYAL_BLUE.getColor (). GetIndex ());// Background color settings

Second, custom color

Hssfpalette provides two ways to customize colors:

1. Modify the RGB value of an existing Color object

The code is as follows to change the color value of orange to (255,204,153)

Hssfpalette Custompalette = Workbook.getcustompalette ();

Custompalette.setcoloratindex (HSSFColor.ORANGE.index, (byte) 255, (byte) 204, (byte) 153);

When you change the color to orange, the modified color value is used.

2, add a color with RGB value (with certain restrictions)

The code is as follows, adding a color (153,204,255) that returns a Hssfcolor object.

Hssfpalette Custompalette = workbook.getcustompalette ();

Hssfcolor Newcolor = custompalette.addcolor ((byte) 153, (byte) 204, (byte) 255);

This will get the new color object Newcolor, which can be used. (The limitation of the 2nd method is that the reference to the Newcolor object must be in use, and the 1th method does not.)

Java POI 1, use of Java POI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.