Java POI Import into Excel

Source: Internet
Author: User
Tags rowcount

Required Jar-Pack POI

package example.poi;import java.io.*;import java.text.decimalformat;import  org.apache.poi.hssf.usermodel.*;import org.apache.poi.ss.usermodel.*;import  org.apache.poi.xssf.usermodel.*;p Ublic class importexcel {  private workbook  wb = null; private sheet sheet = null; private row row  = null; private int sheetnum = 0; private int rownum =  0; private fileinputstream fis = null; private file file =  null; private decimalformat df = new decimalformat ("0"); public  Importexcel ()  {  super ();  } public void setsheetnum (Int sheetNum)  {  this.sheetnum = sheetnum; } public void setrownum (int  RowNum)  {  this.rownum = rownum;&nbsP;}  public void setfile (File file)  {  this.file = file; }  /**  *  read Excel file to get Hssfworkbook object   *  @throws  IOException  */  Public void open (String filepath)  throws ioexception {  if ( Validateexcel (FilePath))  {   file = new file (FilePath);    Fis = new fileinputstream (file);    if (isExcel2003 (filePath))  {     wb = new hssfworkbook (FIS);   } else {     wb = new xssfworkbook (FIS);    }   fis.close ();   }  }  /**  *  get sheet number of tables   *  @return  sheet number of tables   */  public int getsheetcount ()  {  int sheetCount = -1;   Sheetcount = wb.getnUmberofsheets ();  return sheetcount; }  /**  *  get the number of records in Sheetnum   *  @return   Record Lines   */ public int getrowcount ()  {   if (wb == null)  {   system.err.println ("----------->workbook is Empty");   }   sheet sheet = wb.getsheetat (this.sheetnum);   int rowcount =  -1;  rowcount = sheet.getlastrownum ();  return rowcount; }   /**  *  gets the number of record rows for the specified sheetnum   *  @param  sheetNum  table number   *   @return   Record number of rows   */ public int getrowcount (int sheetnum)  {   sheet sheet = wb.getsheetat (Sheetnum);  int rowcount = -1;   rowcount = sheet.getlastrownum ();  return rowcount; }  /**   *  get the contents of the specified line &NBsp; *  @param  lineNum  lines   *  @return   content   */ public  String[] readexcelline (Int linenum)  {  return readexcelline (this.sheetNum,  LineNum); }  /**  *  Gets the contents of the specified sheet and number of rows   *  @param  sheetNum  Table numbering   *  @param  lineNum  lines   *  @return   content   */ public  string[] readexcelline (Int sheetnum, int linenum)  {  if (sheetNum  < 0 | |  linenum < 0)  {   return null;  }  string[]  strexcelline = null;  try {   sheet = wb.getsheetat ( Sheetnum);    row = sheet.getrow (linenum);       int  cellcount = row.getlastcellnum ();    strexcelline = new string[ CellcouNt + 1];   for (int i = 0; i <= cellcount; i++)  {    strexcelline[i] = readstringexcelcell (lineNum, i);    }  } catch  (exception e)  {   e.printstacktrace ();   }  return strExcelLine; }  /**  *  gets the contents of the specified column   *   @param  cellNum  column numbering   *  @return   content   */ public string  readstringexcelcell (Int cellnum)  {  return readstringexcelcell (This.rowNum,  cellnum); }  /**  *  Gets the contents of the specified row and column number   *  @param  rownum   Line numbering   *  @param  cellNum  column numbering   *  @return   content   */  Public string readstringexcelcell (Int rownum, int cellnum)  {  return  readstringexcelcell (This.sheetNum, rownum, cellnum); }  /**  *  Gets the contents of the specified sheet, row, column   * @ param sheetnum  table number   *  @param  rowNum  line numbering   *  @param   cellnum  column numbering   *  @return   content   */ public String  Readstringexcelcell (int sheetnum, int rownum, int cellnum)  {  if ( sheetnum < 0 | |  rownum < 0)  {   return  ";  }  string  strexcelcell =  "";   try {   sheet = wb.getsheetat (SheetNum);    row = sheet.getrow (RowNum);       if (Row.getCell ( Cellnum)  != null)  {    switch (Row.getcell (cellnum). GetCellType ())  {     case hssfcell.cell_type_formula:     strexcelcell =   "FORMULA";     break;    case HSSFCell.CELL_TYPE_NUMERIC:      //strexcelcell = string.valueof (Row.getcell (cellnum). GetNumericCellValue ());      //Prevent scientific counting, you can use the last line if you don't need it      strExcelCell =  DecimalFormat (Row.getcell (cellnum). Getnumericcellvalue ());     break;     case HSSFCell.CELL_TYPE_STRING:     strExcelCell =  Row.getcell (Cellnum). Getstringcellvalue ();      break;    case  HSSFCell.CELL_TYPE_BLANK:     strExcelCell =  "";      break;    default:     strExcelCell =  "";      break;    }   }  } catch  (exception e)  {    E.printstacktrace ();   }  return strexcelcell; }  /**  *   Test   *  @param  args  */ public static void main (string[]  args)  {  String filePath =  "c:\\users\\administrator\\desktop\\ Workbook 1.xlsx";   importexcel ie = new importexcel ();  try {    Ie.open (FilePath);  } catch  (ioexception e)  {   e.printstacktrace ();   }  ie.setsheetnum (0);   int count = ie.getrowcount ();   for  (int i = 0; i <= count; i++)  {    String[] rows = ie.readexcelline (i);   for  (int j = 0;  j < rows.length; j++)  {    system.out.print (rows[j] +  "   ");   &nbsP;}    system.out.print ("\ n");  } }  /**  *  Verify that the file exists and that it is an Excel file   *  @param  filePath  *  @return   Results   */  public boolean validateexcel (String filepath)  {  if (filePath ==  null | |  !  (isExcel2003 (filePath)  | |  isexcel2007 (FilePath))  {   system.err.println ("File is not an Excel file!) ");    return false;  }  file file = new file ( FilePath);   if (file == null | |  ! file.exists ())  {   system.err.println ("file does not exist!) ");   return false;  }  return true; }  /**   *  determine if Version 2003 excel  *  @param  filePath  file path   *  @return   Results   */ public boolean isexcel2003 (String filepath)  {&Nbsp; return filepath.matches ("^.+\\. i) (XLS) $ "); }  /**  *  determine if the version is 2007 excel  *  @param  filepath   File path   *  @return   result   */ public boolean isexcel2007 (String  filepath)  {  return filepath.matches ("^.+\\. i) (xlsx) $ "); }  /**  *  convert the obtained data type to string prevent scientific notation   *  @param   decimal  data   *  @return   results   */ public string decimalformat ( Double decimal)  {  return df.format (decimal);  }}

Content excerpt from:

Java read Excel Http://java parse to get data in Excel--compatible with 2003 and 2007

Java POI Import into 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.