Import Java.io.fileoutputstream;import Org.apache.poi.hssf.usermodel.hssfcell;import Org.apache.poi.hssf.usermodel.hssfcellstyle;import Org.apache.poi.hssf.usermodel.hssffont;import Org.apache.poi.hssf.usermodel.hssfrow;import Org.apache.poi.hssf.usermodel.hssfsheet;import Org.apache.poi.hssf.usermodel.hssfworkbook;public class Testexcelpoiprint{public static void Main (string[] args) Throws Exception{testexcelpoiprint EP = new Testexcelpoiprint (); Ep.print ();} /* * POI implements output information to excel file * */public void print () throws exception{string Xlsfile = "D:/testpoi.xls";//step 1: Open Excel file HSSF Workbook wb = new Hssfworkbook ();//create Excel file//hssfworkbook wb = new Hssfworkbook (new FileInputStream (Xlsfile));// Open an existing Excel file//step 2: Open the current workbook hssfsheet sheet = Wb.createsheet ("My first Workbook");//Create a new Sheet object//hssfsheet sheet = Wb.getsheetat (0);//Select the first workbook//wb.setsheetname (0, "My first Workbook");//Set the workbook name//step 3: Create a Row object Hssfrow nrow = Sheet.createrow ( (short) 1);//Line 2nd//step 4: Specify column to create Cell object Hssfcell NCell = Nrow.createcell ((short) (2));3rd Column//step 5: Specify the column to create the Cell object Ncell.setcellvalue ("I am a Cell"),//step 6: Set the style Ncell.setcellstyle (Leftstyle (WB));//step 7: Close the Save Excel file FileOutputStream FOut = new FileOutputStream (xlsfile); Wb.write (FOut); Fout.flush (); Fout.close ();} Set cell style private Hssfcellstyle Leftstyle (Hssfworkbook wb) {Hssfcellstyle curstyle = Wb.createcellstyle (); Hssffont Curfont = Wb.createfont ();//Set the font//curfont.setfontname ("Times New Roman");//Set English font curfont.setfontname ("Microsoft Ya Black ");//Set the English font curfont.setcharset (hssffont.default_charset);//Set the Chinese font, you must also encode the cell settings curfont.setfontheightinpoints ((short) 10);//font size curstyle.setfont (curfont); Curstyle.setbordertop (Hssfcellstyle.border_thick);// Thick Solid line Curstyle.setborderbottom (Hssfcellstyle.border_thin);//Solid line Curstyle.setborderleft (Hssfcellstyle.border_medium) ;//Compare thick solid line curstyle.setborderright (Hssfcellstyle.border_thin);//Solid line Curstyle.setwraptext (true); NewLine curstyle.setalignment (hssfcellstyle.align_right);//horizontal with Right alignment curstyle.setverticalalignment ( Hssfcellstyle.vertical_center);//Cell Vertical center return CurStyle;}}
Basic application of POI output report in Java