Java implementation of Excel read-write--poi

Source: Internet
Author: User

The previous article introduces the way to generate Excel by Xls.jar, this article will introduce the way to realize Excel file reading and writing through POI, the content is very simple, the code comment is very clear.

1. Generate Excel Files:

ImportJava.io.File;ImportJava.io.FileOutputStream;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook; Public classPoi_write { Public Static voidMain (string[] args) {FinalString [] str = {"id", "name", "Sex"}; //Create an Excel workbookHssfworkbook Workbook =NewHssfworkbook (); //Create an Excel worksheetHssfsheet sheet =Workbook.createsheet (); //Create first rowHssfrow row = Sheet.createrow (0); Hssfcell Cell=NULL; //Add a table header         for(inti = 0; i < str.length; i++) {cell=Row.createcell (i);        Cell.setcellvalue (Str[i]); }                //Append Data         for(inti = 1; I <= 10; i++) {hssfrow rows=Sheet.createrow (i); Hssfcell cell_id= Rows.createcell (0); Cell_id.setcellvalue (i+""); Hssfcell Cell_name= Rows.createcell (1); Cell_name.setcellvalue ("Name:" +i); Hssfcell Cell_sex= Rows.createcell (2); Cell_sex.setcellvalue (Male); }                //File Save pathFile File =NewFile ("E:/cnblogs/poi_text.xls"); Try{            if(!file.exists ())            {File.createnewfile (); } FileOutputStream fileout=NewFileOutputStream (file);//Create a file output stream objectWorkbook.write (fileout); Fileout.close ();//close the output stream object}Catch(Exception e) {e.printstacktrace (); } System.out.println ("Excel file has been generated"); }    }

2. Read the Excel file:

ImportJava.io.File;Importjava.io.IOException;Importorg.apache.commons.io.FileUtils;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook; Public classpoi_reading { Public Static voidMain (string[] args) {FinalFile File =NewFile ("E:/cnblogs/poi_text.xls"); if(File.exists ()) {Try{Hssfworkbook Workbook=NewHssfworkbook (Fileutils.openinputstream (file)); //Workbook.getsheet ("Sheet0"); get the specified sheet//hssfsheet sheet = workbook.getsheet ("Sheet0"); //workbook.getsheetat (0); Get first sheet by defaultHssfsheet sheet = workbook.getsheetat (0); intFirstrownum = 0; //get the last line number of the current sheet                intLastrownum =Sheet.getlastrownum ();  for(inti = Firstrownum; I <= lastrownum; i++) {Hssfrow row=Sheet.getrow (i); //get the last cell column number of the current line                    intLastcellnum =Row.getlastcellnum ();  for(intj = 0; J < Lastcellnum; J + +) {Hssfcell cell=Row.getcell (j); String value=Cell.getstringcellvalue (); System.out.print (Value+" "); } System.out.println (""); }            } Catch(IOException e) {e.printstacktrace (); }        }Else{System.out.println ("The read file does not exist"); }    }    }

Well, the basic method has been introduced for everyone, the better the effect of the display on everyone's own groping.

I wonder if you find a problem? The excl files we generate are all end of. xls,. xls is the version of office97-2003, and for versions after 2007 ends with. xlsx. How do I generate a higher version of Excel? We can not just modify the file suffix, we need to use Xssfworkbook to create the workbook, and then the rest of the HSSF to remove the beginning. But the point here is that we don't know if the user is using a lower version or a higher version, so we recommend that you use Hssfworkbook to create an Excel file that ends in. xls.

If there is an error, also correct. Thank you

Java implementation of Excel read-write--poi

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.