Java-poi Creating a template

Source: Internet
Author: User

 PackageCom.jy.demo.web;ImportJava.io.FileOutputStream;ImportOrg.apache.poi.ss.usermodel.Cell;ImportOrg.apache.poi.ss.usermodel.Row;ImportOrg.apache.poi.ss.usermodel.Sheet;ImportOrg.apache.poi.ss.usermodel.Workbook;Importorg.apache.poi.ss.util.CellRangeAddress;ImportOrg.apache.poi.xssf.usermodel.XSSFCellStyle;ImportOrg.apache.poi.xssf.usermodel.XSSFWorkbook; Public classTestexcel { Public Static voidMain (string[] args)throwsException {Workbook WB=NewXssfworkbook ();//or New Xssfworkbook ();Sheet Sheet= Wb.createsheet ("Sheet1"); Row rowt= Sheet.createrow (0); Cell Cellt= Rowt.createcell (0); Cellt.setcellvalue ("School Number"); Cell cell1t= Rowt.createcell (1); Cell1t.setcellvalue (Name); Cell cell2t11= Rowt.createcell (2); Cell2t11.setcellvalue (Gender); Cell cell2t= Rowt.createcell (3); Cell2t.setcellvalue (Account); Cell cell2t1= Rowt.createcell (4); Cell2t1.setcellvalue (Results); //Create a cell style objectXssfcellstyle Alignstyle =(Xssfcellstyle) Wb.createcellstyle (); Alignstyle.setalignment (Xssfcellstyle.align_center); //Center Alignmentalignstyle.setverticalalignment (Xssfcellstyle.vertical_center);                                      Cellt.setcellstyle (Alignstyle); inty = 2; intnum = 0;  for(inti = 1; I <= 30; i = i + 3) {num++; Row Row1=Sheet.createrow (i); Cell Cell1= Row1.createcell (0); Sheet.addmergedregion (Cellrangeaddress.valueof ("$A $" + y + ": $A $" + (Y + 2))); Cell1.setcellvalue ("00" +num);            Cell1.setcellstyle (Alignstyle); Cell cell2= Row1.createcell (1); Sheet.addmergedregion (Cellrangeaddress.valueof ("$B $" + y + ": $B $" + (Y + 2))); Cell2.setcellvalue ("Zhang San");            Cell2.setcellstyle (Alignstyle); Cell cell21= Row1.createcell (2); Sheet.addmergedregion (Cellrangeaddress.valueof ("$C $" + y + ": $C $" + (Y + 2))); Cell21.setcellvalue (Male);            Cell21.setcellstyle (Alignstyle); Y= y + 3; Cell Cell3= Row1.createcell (3); Cell3.setcellvalue (Language); Row Row2= Sheet.createrow (i + 1); Cell CELLR2= Row2.createcell (3); Cellr2.setcellvalue (Mathematical); Row row3= Sheet.createrow (i + 2); Cell CELLR3= Row3.createcell (3); Cellr3.setcellvalue (English); Cell CELL4= Row1.createcell (4); Cell4.setcellvalue ("1222"); Cell cell41= Row2.createcell (4); Cell41.setcellvalue ("98"); Cell cell411= Row3.createcell (4); Cell411.setcellvalue ("981"); } FileOutputStream fileout=NewFileOutputStream ("E:\\workbook.xls");        Wb.write (fileout);     Fileout.close (); }        }

Java-poi Creating a template

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.