[Java POI] 1. Use of Java POI and javapoi

Source: Internet
Author: User

[Java POI] 1. Use of Java POI and javapoi

Most of the time, a software application needs to generate reports in Microsoft Excel file format. Sometimes an application even wants to use an Excel file as input data. For example, an application developed by a company needs to generate its own Excel for all outputs from the finance department.

Any Java programmer is willing to output MS Office files, which can be achieved by using predefined 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 to use Java distributed design or modify Microsoft Office files. It contains classes and methods to decode user input data or files to MS Office documents.

Apache POI Components

Apache POI Contains classes and methods to combine all the OLE 2 Documents of MS Office. The list of this API component is as follows.

  • POIFS (File System implementation with poor obfuscation technology ):This component is the basic factor for all other POI components. It is used to explicitly read different files.

  • HSSF (terrible spreadsheet format ):It is used to read and write MS-Excel files in the xls format.

  • XSSF (XML format ):It is used in the XLSX file format in MS-Excel.

  • HPSF (terrible attribute setting format ):It is used to extract attributes of MS-Office files.

  • HWPF (terrible Word Processor Format ):It is used to read and write the file extension of MS-Word.

  • XWPF (XML Word Processor Format ):It is used to read and write files with the docx extension of MS-Word.

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

  • HDGF (terrible chart format ):It contains binary files whose classes and methods are MS-Visio.

  • HPBF (terrible publisher format ):It is used to read and write MS-Publisher files.

Next we will mainly introduce the use of HSSF components, direct generation (gan) code (huo) I. Common settings

// Generate an Excel table
HSSFWorkbook workBook = new HSSFWorkbook ();

// Create a sheet

HSSFSheet sheet = workBook. createSheet ();

Sheet. autoSizeColumn (1, true );

// Define a style
HSSFCellStyle contentStyle = workBook. createCellStyle ();
HSSFFont contentFont = workBook. createFont (); // define the font
ContentFont. setFontName (""); // set the font
ContentFont. setFontHeightInPoints (short) 10); // set the font size
ContentFont. setBold (true); // you can specify the bold value.
ContentFont. setColor (HSSFColor. WHITE. index); // set the font color.
ContentStyle. setFont (contentFont );
ContentStyle. setAlignment (HorizontalAlignment. CENTER); // CENTER in the left-right corner
ContentStyle. setVerticalAlignment (VerticalAlignment. CENTER); // vertically centered

HSSFCellStyle contentStyleTemp = workBook. createCellStyle ();
ContentStyleTemp. cloneStyleFrom (contentStyle); // clone the style
ContentStyleTemp. setFillForegroundColor (HSSFColor. HSSFColorPredefined. ROYAL_BLUE.getColor (). getIndex (); // set the background color.

Ii. Custom colors

HSSFPalette provides two ways to customize colors:

1. Modify the RGB value of an existing color object.

The Code is as follows. Change the ORANGE color value to (255,204,153)

HSSFPalette customPalette = workbook. getCustomPalette ();

CustomPalette. setColorAtIndex (HSSFColor. ORANGE. index, (byte) 255, (byte) 204, (byte) 153 );

After you change the color to ORANGE, the modified color value is used.

 

2. Add a color using the RGB value (with certain limitations)

The Code is as follows. Add a color (153,204,255) and return an HSSFColor object.

HSSFPalette customPalette = workbook. getCustomPalette ();

HSSFColor newColor = customPalette. addColor (byte) 153, (byte) 204, (byte) 255 );

In this way, the new color object newColor can be used. (The limit of the 2nd method is that the newColor object must be referenced during use, while the limit of the 1st method is not)

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.