Use poi3.7 to Operate Excel tables in Java

Source: Internet
Author: User
/*** Create an Excel table ** @ Param filename */public static void createexecl (string filename) {workbook = NULL; // generate a workbook object if (filename. endswith (". xls ") {workbook = new hssfworkbook ();} else {workbook = new xssfworkbook ();} // generate the worksheet object sheet = workbook. createsheet ("sheet one"); // generates a row = sheet. createrow (0); // generate a cell = row. createcell (0); // set the cell content to the string type // cell_type_blank // cell_ty Pe_boolean // cell_type_error/cell_type_formula // cell_type_numeric/cell_type_stringcell.setcelltype (cell. cell_type_string); // set the cell content. setcellvalue ("Serial Number"); cell = row. createcell (1); cell. setcelltype (cell. cell_type_string); cell. setcellvalue ("name"); cell = row. createcell (2); cell. setcelltype (cell. cell_type_string); cell. setcellvalue ("Age"); cell = row. createcell (3); cell. setcelltype (cell. cell_type_stri NG); cell. setcellvalue ("date"); cell = row. createcell (4); cell. setcelltype (cell. cell_type_string); cell. setcellvalue ("Introduction"); For (INT I = 0; I <5; I ++) {ROW = sheet. createrow (I + 1); cell = row. createcell (0); cell. setcelltype (cell. cell_type_string); cell. setcellvalue ("2012080300" + I); cell = row. createcell (1); cell. setcelltype (cell. cell_type_string); cell. setcellvalue ("080300" + I); cell = row. createcell (2); CE Ll. setcelltype (cell. cell_type_string); cell. setcellvalue ("080300" + I); cell = row. createcell (3); cell. setcelltype (cell. cell_type_string); cell. setcellvalue (new date (); cell = row. createcell (4); cell. setcelltype (cell. cell_type_string); cell. setcellvalue ("Haha enemy Oh Ala" + I);} outputstream = NULL; try {outputstream = new fileoutputstream (filename); workbook. write (outputstream); outputstream. flush ();} Catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} finally {If (outputstream! = NULL) {try {outputstream. close ();} catch (ioexception e) {e. printstacktrace () ;}}}/*** read the Excel table ** @ Param filename */public static void readexcel (string filename) {inputstream = NULL; workbook workbook = NULL; sheet = NULL; row = NULL; cell = NULL; stringbuffer = new stringbuffer (); try {inputstream = new fileinputstream (filename ); if (filename. endswith (". xls ")) {Workbook = new hssfworkbook (inputstream);} else {workbook = new xssfworkbook (inputstream);} For (INT sheetindex = 0, sheets = workbook. getnumberofsheets (); sheetindex <sheets; sheetindex ++) {sheet = workbook. getsheetat (sheetindex); If (null! = Sheet) {stringbuffer. append (sheet. getsheetname () + "\ n"); For (INT rowindex = 0, rows = sheet. getlastrownum (); rowindex <= rows; rowindex ++) {ROW = sheet. getrow (rowindex); If (null! = Row) {for (INT cellnum = 0, cells = row. getlastcellnum (); cellnum <cells; cellnum ++) {Cell = row. getcell (cellnum); If (! (Cell. getcelltype () = cell. cell_type_numeric) |! (Dateutil. iscelldateformatted (cell) {Cell. setcelltype (cell. cell_type_string); stringbuffer. append (cell. getstringcellvalue () + "\ t");} else {date = cell. getdatecellvalue (); stringbuffer. append (New simpledateformat ("HH: mm: SS "). format (date) + "\ t") ;}} stringbuffer. append ("\ n") ;}} stringbuffer. append ("\ n") ;}} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} finally {try {inputstream. close ();} catch (ioexception e) {e. printstacktrace () ;}} system. out. println (stringbuffer );}

 

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.