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