Poi implements java to generate excel

Source: Internet
Author: User

Package com. WLS. Excel;

Import java. Io. bytearrayinputstream;
Import java. Io. bytearrayoutputstream;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;

Import org. Apache. Poi. hssf. usermodel. hssfcell;
Import org. Apache. Poi. hssf. usermodel. hssfrow;
Import org. Apache. Poi. hssf. usermodel. hssfsheet;
Import org. Apache. Poi. hssf. usermodel. hssfworkbook;

Public class excelpoi {
 
/***
* Returns an inputstream.
* @ Return
*/
Public static inputstream getinput (){
Bytearrayoutputstream baos = NULL;
Bytearrayinputstream BAIS = NULL;
Try {
HSSFWorkbook workbook = new HSSFWorkbook ();

HSSFSheet sheet = workbook. createSheet ("one ");
HSSFRow row = sheet. createRow (0 );

HSSFCell cell = row. createCell (short) 0 );

Cell. setEncoding (HSSFCell. ENCODING_UTF_16 );
Cell. setCellType (HSSFCell. CELL_TYPE_STRING );
Cell. setCellValue ("small test ");

Baos = new ByteArrayOutputStream ();
Workbook. write (baos );
Byte [] B = baos. toByteArray ();
BAIS = new bytearrayinputstream (B );

System. Out. println ("file generated successfully! ");
} Catch (exception e ){
E. printstacktrace ();
}
Return BAIS;
}
 
/**
* Get the stream and generate the local xls file
*/
Public static void test1 (){
Inputstream in = getinput ();
Fileoutputstream Fos = NULL;
Byte [] B = new byte [1024];
Int T = 0;
Try {
Fos = new fileoutputstream ("D:/aa.xls ");

While (-1! = (T = in. read (B, 0, B. length ))){
Fos. write (B, 0, t );
System. out. println (new String (B ));
}
} Catch (IOException e ){
E. printStackTrace ();
}
}

/***
* Generate an excel file in java
* Directly generate a local xls file
*/
Public void test2 (){
Try {
FileOutputStream out = new FileOutputStream ("d:/a.xls ");
HSSFWorkbook workbook = new HSSFWorkbook ();

HSSFSheet sheet = workbook. createSheet ("one sheet ");

HSSFRow row = sheet. createRow (0 );

HSSFCell cell = row. createCell (short) 0 );

Cell. setEncoding (HSSFCell. ENCODING_UTF_16 );
Cell. setCellType (HSSFCell. CELL_TYPE_STRING );
Cell. setCellValue ("small test ");

Workbook. write (out );
System. out. println ("file generated successfully! ");

} Catch (Exception e ){
E. printStackTrace ();
}
 
}
 
Public static void main (String [] args ){

Test1 ();
}
}

Related Article

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.