Java POI Import into Excel

Source: Internet
Author: User

Recent projects need to import Excel, online There are many examples, their own integration record, compatible with 2003 and 2007, temporarily did not add image processing function.

Required Jar Pack Http://pan.baidu.com/s/1sjPuWDR

 PackageExample.poi;ImportJava.io.*;ImportJava.text.DecimalFormat;ImportOrg.apache.poi.hssf.usermodel.*;ImportOrg.apache.poi.ss.usermodel.*;ImportOrg.apache.poi.xssf.usermodel.*; Public classImportexcel {PrivateWorkbook WB =NULL; PrivateSheet Sheet =NULL; PrivateRow row =NULL; Private intSheetnum = 0; Private intRowNum = 0; PrivateFileInputStream FIS =NULL; PrivateFile File =NULL; PrivateDecimalFormat DF =NewDecimalFormat ("0");  PublicImportexcel () {Super(); }  Public voidSetsheetnum (intsheetnum) {   This. Sheetnum =Sheetnum;}  Public voidSetrownum (introwNum) {   This. RowNum =RowNum;}  Public voidsetfile (file file) { This. File =file;} /*** Read Excel file to get Hssfworkbook object *@throwsIOException*/  Public voidOpen (String FilePath)throwsIOException {if(Validateexcel (FilePath)) {file=NewFile (FilePath); FIS=Newfileinputstream (file); if(isExcel2003 (FilePath)) {WB=NewHssfworkbook (FIS); } Else{WB=NewXssfworkbook (FIS);  } fis.close (); } }   /*** Get sheet number of tables *@returnsheet number of tables*/  Public intGetsheetcount () {intSheetcount =-1; Sheetcount=wb.getnumberofsheets (); returnSheetcount;} /*** Get the number of lines of records under Sheetnum *@returnnumber of record lines*/  Public intGetRowCount () {if(WB = =NULL) {System.err.println ("----------->workbook is empty"); } Sheet Sheet= Wb.getsheetat ( This. Sheetnum); intRowCount =-1; RowCount=Sheet.getlastrownum (); returnRowCount;} /*** Gets the number of record lines for the specified sheetnum *@paramsheetnum Table number *@returnnumber of record lines*/  Public intGetRowCount (intsheetnum) {Sheet Sheet=Wb.getsheetat (Sheetnum); intRowCount =-1; RowCount=Sheet.getlastrownum (); returnRowCount;} /*** Get the contents of the specified line *@paramlinenum Number of lines *@returncontent*/  PublicString[] Readexcelline (intlinenum) {  returnReadexcelline ( This. Sheetnum, LineNum); }   /*** Gets the contents of the specified sheet and number of rows *@paramsheetnum Table number *@paramlinenum Number of lines *@returncontent*/  PublicString[] Readexcelline (intSheetnum,intlinenum) {  if(Sheetnum < 0 | | LineNum < 0) {   return NULL; } string[] Strexcelline=NULL; Try{Sheet=Wb.getsheetat (Sheetnum); Row=Sheet.getrow (linenum); intCellcount =Row.getlastcellnum (); Strexcelline=NewString[cellcount + 1];  for(inti = 0; I <= Cellcount; i++) {Strexcelline[i]=Readstringexcelcell (LineNum, i); }  } Catch(Exception e) {e.printstacktrace (); }  returnStrexcelline;} /*** Get the contents of the specified column *@paramcellnum Column number *@returncontent*/  PublicString Readstringexcelcell (intcellnum) {  returnReadstringexcelcell ( This. RowNum, Cellnum); }   /*** Get the contents of the specified row and column number *@paramrowNum Line number *@paramcellnum Column number *@returncontent*/  PublicString Readstringexcelcell (intRowNum,intcellnum) {  returnReadstringexcelcell ( This. Sheetnum, RowNum, cellnum); }   /*** Get the contents of the specified sheet, row, column *@paramsheetnum Table number *@paramrowNum Line number *@paramcellnum Column number *@returncontent*/  PublicString Readstringexcelcell (intSheetnum,intRowNum,intcellnum) {  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 ()) { CaseHSSFCell.CELL_TYPE_FORMULA:strExcelCell= "FORMULA";  Break;  CaseHssfcell.cell_type_numeric://Strexcelcell = string.valueof (Row.getcell (cellnum). Getnumericcellvalue ()); //Prevent scientific counting, use a line if you don't need itStrexcelcell =DecimalFormat (Row.getcell (cellnum). Getnumericcellvalue ());  Break;  CaseHSSFCell.CELL_TYPE_STRING:strExcelCell=Row.getcell (cellnum). Getstringcellvalue ();  Break;  CaseHSSFCell.CELL_TYPE_BLANK:strExcelCell= "";  Break; default: Strexcelcell= "";  Break; }   }  } Catch(Exception e) {e.printstacktrace (); }  returnStrexcelcell;} /*** Test *@paramargs*/  Public Static voidMain (string[] args) {String FilePath= "C:\\users\\administrator\\desktop\\ Workbook 1.xlsx"; Importexcel IE=NewImportexcel (); Try{ie.open (FilePath); } Catch(IOException e) {e.printstacktrace (); } ie.setsheetnum (0); intCount =Ie.getrowcount ();  for(inti = 0; I <= count; i++) {string[] rows=Ie.readexcelline (i);  for(intj = 0; J < Rows.length; J + +) {System.out.print (Rows[j]+ " "); } System.out.print ("\ n"); } }   /*** Verify that the file exists and that it is an Excel file *@paramFilePath *@returnResults*/  Public BooleanValidateexcel (String filePath) {if(FilePath = =NULL|| ! (isExcel2003 (filePath) | |isExcel2007 (FilePath))) {System.err.println ("The file is not an Excel file! "); return false; } File File=NewFile (FilePath); if(File = =NULL|| !file.exists ()) {System.err.println ("File does not exist!" "); return false; }  return true; } /*** Determine if the version is 2003 Excel *@paramfilePath File path *@returnResults*/  Public BooleanisExcel2003 (String filePath) {returnFilepath.matches ("^.+\\. i) (XLS) $ "); }   /*** Determine if the version is 2007 Excel *@paramfilePath File path *@returnResults*/  Public BooleanisExcel2007 (String filePath) {returnFilepath.matches ("^.+\\. i) (xlsx) $ "); }   /*** Convert the acquired data type to string prevent scientific notation *@paramDecimal Data *@returnResults*/  PublicString DecimalFormat (Double decimal) {returnDf.format (decimal);}}

Content for reference:

http://download.csdn.net/detail/wangquanzheng/3526296

http://blog.csdn.net/mmm333zzz/article/details/7962377

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.