Use poi to read and write Excel files

Source: Internet
Author: User

A colleague asked me to parse 0.6 million pieces of data to an Excel table, remove the specified data, and write the data in a decisive manner. After half of the data, he didn't need it anymore.

Jar package

 
Commons-codec-1.5.jarcommons-logging-1.1.jardom4j-1.6.1.jarjunit-3.8.1.jarlog4j-1.2.13.jarpoi-3.9-20121203.jarpoi-examples-3.9-20121203.jarpoi-excelant-3.9-20121203.jarpoi-ooxml-3.9-20121203.jarpoi-scratchpad-3.9-20121203.jarstax-api-1.0.1.jarxmlbeans-2.3.0.jar
 Package  Com. jokey;  Import  Java. Io. fileinputstream;  Import  Java. Io. filenotfoundexception;  Import  Java. Io. fileoutputstream; Import  Java. Io. ioexception;  Import  Org. Apache. Poi. poifs. filesystem. poifsfilesystem;  Import  Org. Apache. Poi. xssf. usermodel. xssfcell;  Import  Org. Apache. Poi. xssf. usermodel. xssfrow;  Import  Org. Apache. Poi. xssf. usermodel. xssfsheet;  Import  Org. Apache. Poi. xssf. usermodel. xssfworkbook;  Public   Class Excelreaderandwriter {  Private  Poifsfilesystem FS;  Private  Xssfworkbook WB;  Private  Xssfsheet sheet;  Private  Xssfrow row;  Private  Xssfcell cell;  Public   Static   Void  Main (string [] ARGs ){ Try  {  //  Test the title of an Excel worksheet  Excelreaderandwriter excelreader = New  Excelreaderandwriter (); excelreader. Reader ();  //  Excelreader. Writer (); } Catch  (Filenotfoundexception e) {system. Out. println ( "The specified path file is not found! " ); E. printstacktrace ();} Catch  (Ioexception e ){  //  Todo auto-generated Catch Block  E. printstacktrace ();}}  Private   Void Reader () Throws  Ioexception {fileinputstream is = New Fileinputstream ("F: \ 3.xls X" ); WB = New Xssfworkbook (is); Sheet = WB. getsheetat (0 ); Sheet = WB. getsheetat (0 );  //  Retrieve the total number of rows          Int Totalrownum = Sheet. getlastrownum ();  //  Here I <=          For ( Int I = 0; I <= totalrownum; I ++ ) {Row =Sheet. getrow (I );  For ( Int J = 0; j <row. getlastcellnum (); j ++ ) {System. Out. Print (row. getcell (j);} system. Out. println ( "" );}}  Private   Void Writer () Throws  Ioexception {fileoutputstream OS = New Fileoutputstream ("F: \ 5.xlsx" ); //  Create a worksheet WB = New  Xssfworkbook (); Sheet = WB. createsheet (); row = Sheet. createrow (0 ); Cell = Row. createcell (0 ); Cell. setcellvalue ( "Tested" );  Try  {  //  Write  WB. Write (OS );} Catch  (Ioexception e ){  //  Todo auto-generated Catch Block  E. printstacktrace ();}}} 

 

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.