Familiar with hssf of poi

Source: Internet
Author: User
Document directory
  • Overview
  • Busy developers 'Guide to features

Because of some requirements, you need to use Java to save some data into excel. It is good to hear from friends that the Apache poi Project (hereinafter referred to as POI)

Poi provides a series of interfaces for reading and generating Microsoft's Ole 2 compound document format, which is Excel, Word ..

 

Because you only want to read and generate an Excel file, you can directly jump

Http://poi.apache.org/hssf/index.html super see how to use hssf

 

This is a brief introduction to POI-HSSF

POI-HSSF-Java API to access Microsoft Excel format filesoverview

Hssf is the poi project's pure Java implementation of the Excel '97 (-2002) file format.

Hssf provides a way to read spreadsheets create, modify, read and write XLS spreadsheets it provides:

  • Low level structures for those with special needs
  • An eventmodel API for efficient read-only access
  • A full usermodel API for creating, reading and modifying XLS files

An alternate way of generating a spreadsheet is via the cocoon serializer (yet you'll still be using hssf indirectly ). with cocoon you can serialize any XML datasource (which might be a esql page outputting in SQL for instance) by simply applying the stylesheet and designating the serializer.

If you're merely reading spreadsheet data, then use the eventmodel API in the org. Apache. Poi. hssf. eventusermodel package.

If you're modifying spreadsheet data then use the usermodel API. You can also generate spreadsheets this way.

 

I quickly browsed the hssf page and found a very good page (exactly what I needed)

Busy developers 'Guide to hssf features

Busy developers 'Guide to features

Want to use hssf read and write spreadsheets in a hurry? This guide is for you. If you're after more in-depth coverage of the hssf user-API please consult the howto guide as it contains actual descriptions of how to use this stuff.

This page contains a large number of code samples for programmers who do not have time to view API documentation ~~ (Very good practice, user-friendly: 0)

Http://poi.apache.org/hssf/quick-guide.html

 

This is the test code I copied from the sample code (partial) on the webpage :)

After implementation, an Excel file named workbook.xls is generated in the project file folder.

Package blog.csdn.net. sniljava. hssf;

Import java. Io .*;
Import java. util. date;

Import org. Apache. Poi. hssf. usermodel .*;
Import org. Apache. Poi. hssf. util. hssfcolor;

/**
* <P> title: hssf </P>
*
* <P> Description: </P>
*
* <P> copyright: Copyright (c) 2007 </P>
*
* <P> company: geogro </P>
*
* @ Author snail Il
* @ Version 1.0
*/

Public class busydevtesthssf {
Public busydevtesthssf (){
}

Public void createnewwork (){
Hssfworkbook WB = new hssfworkbook ();
Try {
Fileoutputstream fileout = new fileoutputstream ("workbook.xls ");
WB. Write (fileout );
Fileout. Close ();
} Catch (filenotfoundexception ex ){
} Catch (ioexception ex ){
}
}

Public void createnewsheet (){
Try {
Hssfworkbook WB = new hssfworkbook ();
Hssfsheet sheet1 = WB. createsheet ("new sheet ");
Hssfsheet sheet2 = WB. createsheet ("second sheet ");
Fileoutputstream fileout = new fileoutputstream ("workbook.xls ");
WB. Write (fileout );
Fileout. Close ();

} Catch (exception e ){
}
}

Public void createcells (){
Try {
Hssfworkbook WB = new hssfworkbook ();
Hssfsheet sheet = WB. createsheet ("new sheet ");

// Create a row and put some cells in it. rows are 0 based.
Hssfrow ROW = sheet. createrow (short) 0 );
// Create a cell and put a value in it.
Hssfcell cell = row. createcell (short) 0 );
Cell. setcellvalue (1 );

// Or do it on one line.
Row. createcell (short) 1). setcellvalue (1.2 );
Row. createcell (short) 2). setcellvalue ("this is a string ");
Row. createcell (short) 3). setcellvalue (true );

// Write the output to a file
Fileoutputstream fileout = new fileoutputstream ("workbook.xls ");
WB. Write (fileout );
Fileout. Close ();

} Catch (exception e ){
}
}

Public void creatdatecells (){
Try {
Hssfworkbook WB = new hssfworkbook ();
Hssfsheet sheet = WB. createsheet ("new sheet ");

// Create a row and put some cells in it. rows are 0 based.
Hssfrow ROW = sheet. createrow (short) 0 );

// Create a cell and put a date value in it. The first cell is not styled
// As a date.
Hssfcell cell = row. createcell (short) 0 );
Cell. setcellvalue (new date ());

// We style the second cell as a date (and time). It is important
// Create a new cell style from the workbook otherwise you can end up
// Modifying the built in style and other ting not only this cell but other cells.
Hssfcellstyle cellstyle = WB. createcellstyle ();
Cellstyle. setdataformat (hssfdataformat. getbuiltinformat (
"M/D/yy H: mm "));
Cell = row. createcell (short) 1 );
Cell. setcellvalue (new date ());
Cell. setcellstyle (cellstyle );

// Write the output to a file
Fileoutputstream fileout = new fileoutputstream ("workbook.xls ");
WB. Write (fileout );
Fileout. Close ();
} Catch (filenotfoundexception ex ){
} Catch (ioexception ex ){
}
}

Public void createborders (){
Try {
Hssfworkbook WB = new hssfworkbook ();
Hssfsheet sheet = WB. createsheet ("new sheet ");

// Create a row and put some cells in it. rows are 0 based.
Hssfrow ROW = sheet. createrow (short) 1 );

// Create a cell and put a value in it.
Hssfcell cell = row. createcell (short) 1 );
Cell. setcellvalue (4 );

// Style the cell with borders all around.
Hssfcellstyle style = WB. createcellstyle ();
Style. setborderbottom (hssfcellstyle. border_thin );
Style. setbottombordercolor (hssfcolor. Black. Index );
Style. setborderleft (hssfcellstyle. border_thin );
Style. setleftbordercolor (hssfcolor. Green. Index );
Style. setborderright (hssfcellstyle. border_thin );
Style. setrightbordercolor (hssfcolor. Blue. Index );
Style. setbordertop (hssfcellstyle. border_medium_dashed );
Style. settopbordercolor (hssfcolor. Black. Index );
Cell. setcellstyle (style );

// Write the output to a file
Fileoutputstream fileout = new fileoutputstream ("workbook.xls ");
WB. Write (fileout );
Fileout. Close ();
} Catch (filenotfoundexception ex ){
} Catch (ioexception ex ){
}

}

Public void createdifferenttypesofcells (){
Try {
Hssfworkbook WB = new hssfworkbook ();
Hssfsheet sheet = WB. createsheet ("new sheet ");
Hssfrow ROW = sheet. createrow (short) 2 );
Row. createcell (short) 0). setcellvalue (1.1 );
Row. createcell (short) 1). setcellvalue (new date ());
Row. createcell (short) 2). setcellvalue ("A string ");
Row. createcell (short) 3). setcellvalue (true );
Row. createcell (short) 4). setcelltype (hssfcell. cell_type_error );

// Write the output to a file
Fileoutputstream fileout = new fileoutputstream ("workbook.xls ");
WB. Write (fileout );
Fileout. Close ();
} Catch (filenotfoundexception ex ){
} Catch (ioexception ex ){
}
}

Public void createfillwithcolor (){

Try {
Hssfworkbook WB = new hssfworkbook ();
Hssfsheet sheet = WB. createsheet ("new sheet ");

// Create a row and put some cells in it. rows are 0 based.
Hssfrow ROW = sheet. createrow (short) 1 );

// Aqua background
Hssfcellstyle style = WB. createcellstyle ();
Style. setfillbackgroundcolor (hssfcolor. Aqua. Index );
Style. setfillpattern (hssfcellstyle. big_spots );
Hssfcell cell = row. createcell (short) 1 );
Cell. setcellvalue ("X ");
Cell. setcellstyle (style );

// Orange "foreground", foreground being the fill foreground not the font color.
Style = WB. createcellstyle ();
Style. setfillforegroundcolor (hssfcolor. Orange. Index );
Style. setfillpattern (hssfcellstyle. solid_foreground );
Cell = row. createcell (short) 2 );
Cell. setcellvalue ("X ");
Cell. setcellstyle (style );

// Write the output to a file
Fileoutputstream fileout = new fileoutputstream ("workbook.xls ");
WB. Write (fileout );
Fileout. Close ();
} Catch (filenotfoundexception ex ){
} Catch (ioexception ex ){
}

}

Public static void main (string [] ARGs ){
Busydevtesthssf = new busydevtesthssf ();
System. Out. println ("testhssf! ");
// Test any method of the busydevtesthssf object
Busydevtesthssf. createfillwithcolor ();
System. Out. println ("--------------");

}
}

 

Old pig: Because open-source projects on Apache basically serve the people for free, the author does not have much time and energy to do detailed teaching and documentation instructions 【Busy developers' guide to hssf features]This document is simple, intuitive, and convenient. It is a good idea and can be used for reference in future code documents.

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.