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