Java uses POI import to export data from Excel

Source: Internet
Author: User
Tags string format

First of all to talk about what happened today, in the spirit of an Android heart I was assigned to the PB group, Cao Ying Han Ah! Well, what we're going to record and share today is that Java uses POI import to export data from Excel. Below the POI package of http://poi.apache.org/download.html, interested friends can also go to see the API.

Let's share the two points that you should be aware of when doing basic operations on a POI:

1.POI for Xlsx/xls is required to create different Workbook instance Hssfworkbook is for XLS Xssfworkbook is for xlsx.

   1:  String fileaddress="e:/blog stats. xlsx"//Read Excel address
   2:  xssfworkbook wb=null;
   3:  new File (fileaddress);  Reading Excel Files
   4:  isnew FileInputStream (f);  Create a file stream
   5:  if(Fileaddress.tolowercase (). EndsWith ("xlsx")) {
   6:       new Xssfworkbook (is);  //If the Xssfworkbook object is created on the xlsx version
   7:  Elseif(Filename.tolowercase (). EndsWith ("xls")) {  
   8:       new Hssfworkbook (is);  //If the XLS version of the Hssfworkbook object is created
   9:  }

2. When importing data into Excel, you need to understand if there is a row object in sheet, if there is a getrow (), Gercell (), Setcellvalue (), if not, you need CreateRow (), Createcell (), Setcellvalue ().

   1:    /  /First sheet
   2:    int lastrow = sheet.getlastrownum () + 1;   
   3:    if(rowindex_<lastrow) {    
   4:      xssfrow row = Sheet.getrow (rowindex_);  Get Row Objects             
   5:      Row.getcell (0). Setcellvalue (bolgname2);      
   6:      Row.getcell (1). Setcellvalue (linktime2);    
   7:      Row.getcell (2). Setcellvalue (Linktitle2);      
   8:      Row.getcell (3). Setcellvalue (LINKRESULT2);     
   9:      Row.getcell (4). Setcellvalue (LINKRED2);      
  Ten:      Row.getcell (5). Setcellvalue (LINKPINGLUN2);      
One   :      Row.getcell (6). Setcellvalue (TUIJIANSTRING2);      
  :      Row.getcell (7). Setcellvalue (FANDUISTRING2);      
  :      }            
  :      else{      
  :       xssfrow row = Sheet.createrow (rowindex_);    
  :       Row.createcell (0). Setcellvalue (bolgname2);    
  :       Row.createcell (1). Setcellvalue (linktime2);   
  :       Row.createcell (2). Setcellvalue (Linktitle2);   
  :       Row.createcell (3). Setcellvalue (LINKRESULT2);   
  :       Row.createcell (4). Setcellvalue (LINKRED2);    
  :       Row.createcell (5). Setcellvalue (LINKPINGLUN2);    
  :       Row.createcell (6). Setcellvalue (TUIJIANSTRING2);    
At   :       Row.createcell (7). Setcellvalue (FANDUISTRING2);    
  :         }
     3. Close the Stream
   1:  new FileOutputStream (fileaddress);
   2:          wb.write (fileout);
   3:  is        . Close ();
   4:          fileout.close ();

Finally today to blog statistics fixed a bug, found that the regular expression in the control of the string format is very strong but the water is deep need to study hard, think of him as the next learning goal, and then the next time to solve the problem, the brain burst out of him.

Java uses POI import to export data from Excel

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.