poi Create Excel file
@Test public void testcreateworkbook_xls () { HSSFWorkbook hssfWorkbook = new Hssfworkbook (); //Add worksheet (the XLS file generated when sheet is not added will error when opened) hssfsheet sheetone = hssfworkbook.createsheet (); hssfsheet sheettwo = hssfworkbook.createsheet () ; hssfsheet sheetthree = Hssfworkbook.createsheet ("three"); //Save as Excel file, xls suffix FileOutputStream fileOutputStream = null; String filePath = "D:\\test.xls"; try { &Nbsp; fileoutputstream = new fileoutputstream (FilePath); hssfworkbook.write (FileOutputStream); } catch (filenotfoundexception e) { e.printstacktrace (); system.out.println (E.tostring ()); } catch (ioexception e) { e.printstacktrace (); system.out.println (E.tostring ()); } finally { try { &nBsp; fileoutputstream.close (); } catch (ioexception e) { e.printstacktrace (); system.out.println ( E.tostring ()); } } }
//Generate Workbook ooxml form (. xlsx) @Test Public void testcreateworkbook_xlsx () { // Save as Excel file, xlsx suffix String filePath = "d:\\ Test.xlsx "; fileoutputstream fileoutputstream = null; xssfworkbook xssfworkbook = new Xssfworkbook (); xssfsheet xssfsheetone = Xssfworkbook.createsheet (); xssfsheet xssfsheettwo = xssfworkbook.createsheet ("; try { ") fileOutputStream = new FileOutputStream (FilePath); xssfworkbook.write (FileOutputStream); } catch (filenotfoundexception e) { e.printstacktrace (); } catch (ioexception e) { e.printstacktrace (); } finally { try { fileoutputstream.close (); } catch (ioexception e) { e.printstacktrace (); } } }
Use version poi-3.10.1
Relatively simple, not much to say!
Java read and write Excel's POI chapter-002