Use POI Import to export Excel files in Java and customize date formats

Source: Internet
Author: User

The mission of the Apache POI project is to create and maintain the Java API to manipulate various file formats based on Office Open XML Standard (OOXML) and Microsoft's OLE Composite Document Format (OLE2) 2. In short, you can read and write Excel files using java. In addition, you can read and write MS Word and PowerPoint files using java. Apache POI is a Java Excel solution (Excel 97-2008).

Need Jar:poi-3.9-20121203.jar

Export

 Public Static voidMain (string[] args)throwsIOException {//Create a workbookHssfworkbook WB =NewHssfworkbook (); //Create a styleHssfcellstyle style =Wb.createcellstyle (); //Custom Time FormatStyle.setdataformat (Wb.createdataformat (). GetFormat ("yyyy mm month DD day hh" mm min ss sec ")); //yyyy mm month dd Day hh mm min ss sec//YYYY-MM-DD Hh:mm:ss//Create a pageHssfsheet sheet = wb.createsheet ("Fristsheet"); //Merge CellsSheet.addmergedregion (NewCellrangeaddress (0, 1, 1, 4)); //Create a rowHssfrow row = Sheet.createrow (0); //creates a cell that writes a value to the first cellRow.createcell (0). Setcellvalue ("First cell"); //Second CellRow.createcell (1). Setcellvalue (NewDate ()); //bit cell Add styleRow.getcell (1). Setcellstyle (style); //Create a file stream, specify a file save pathOutputStream OS =NewFileOutputStream ("Export data. xls"); //Writewb.write (OS);        Os.close (); System.out.println (Success); }

Import

 Public Static voidMain (string[] args)throwsIOException {//Create a file input streamFileInputStream FIS =NewFileInputStream ("Export data. xls"); //Create a POI File system objectPoifsfilesystem poi =NewPoifsfilesystem (FIS); //create a workbook to pass in a file system objectHssfworkbook WB =NewHssfworkbook (POI); //Create a sheet pageHssfsheet sheet = wb.getsheetat (0); //empty sentence        if(Sheet = =NULL) {            return; } Hssfrow Row= Sheet.getrow (0); if(Row = =NULL) {            return; } Hssfcell Cell= Row.getcell (0); if(Cell = =NULL) {            return; } Hssfcell cell2= Row.getcell (1); if(Cell2 = =NULL) {            return;        } System.out.println (Cell.getstringcellvalue ()); Date Date=Cell2.getdatecellvalue (); DateFormat Formater=NewSimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");        System.out.println (Formater.format (date)); //Create an Excel, incoming workbook object//excelextractor Excel = new Excelextractor (WB); //set the sheet page name to not print//Excel.setincludesheetnames (FALSE); //output file contents via Excelextractor object//System.out.println (Excel.gettext ());}

Use POI Import to export Excel files in Java and customize date formats

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.