Basic application of POI output report in Java

Source: Internet
Author: User

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

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.