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 ();
}
}