Java poi import excel and poiexcel

Source: Internet
Author: User

Java poi import excel and poiexcel

Recently, the project needs to be imported into excel. There are many examples on the Internet. The integration records are compatible with 2003 and 2007, and the image processing function is not added yet.

Jar package http://pan.baidu.com/s/1sjPuWDR required

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. *; public class ImportExcel {private Workbook wb = null; private Sheet sheet = null; private Row row = null; private int sheetNum = 0; private int rowNum = 0; private FileInputStream FCM = 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;} public void setFile (File file) {this. file = file;}/*** read the excel file to obtain the HSSFWorkbook object * @ throws IOException */public void open (String filePath) throws IOException {if (validateExcel (file Path) {file = new File (filePath); FCM = new FileInputStream (file); if (isExcel2003 (filePath) {wb = new HSSFWorkbook (FCM );} else {wb = new XSSFWorkbook (FCM);} fiis. close () ;}}/*** get the number of sheet tables * @ return the number of sheet tables */public int getSheetCount () {int sheetCount =-1; sheetCount = wb. getNumberOfSheets (); return sheetCount;}/*** get the number of record rows under sheetNum * @ return record rows */public int getRowCount () {if (w B = null) {System. err. println ("-----------> the WorkBook is empty");} Sheet sheet = wb. getSheetAt (this. sheetNum); int rowCount =-1; rowCount = sheet. getLastRowNum (); return rowCount;}/*** get the number of record rows for the specified sheetNum * @ param sheetNum table number * @ return record rows */public int getRowCount (int sheetNum) {Sheet sheet = wb. getSheetAt (sheetNum); int rowCount =-1; rowCount = sheet. getLastRowNum (); return rowCount ;}/*** Fixed Line Content * @ param lineNum number of lines * @ return content */public String [] readExcelLine (int lineNum) {return readExcelLine (this. sheetNum, lineNum );} /*** get the content of the specified worksheet and number of rows * @ param sheetNum table number * @ param lineNum number of rows * @ 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 = shee T. 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;}/*** get the content of the specified column * @ param cellNum column number * @ return content */public String readStringExcelCell (int cellNum) {return readStringExcelCell (this. rowNum, CellNum );} /*** get the content of the specified row and column number * @ param rowNum row number * @ param cellNum column number * @ return content */public String readStringExcelCell (int rowNum, int cellNum) {return readStringExcelCell (this. sheetNum, rowNum, cellNum );} /*** get the content of the specified worksheet, row, and column * @ param sheetNum table number * @ param rowNum row number * @ param cellNum column number * @ 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 (); // prevents scientific counting. If this is not necessary, use a row 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 \ 1.xlsx "; ImportExcel ie = new ImportExcel (); try {ie. open (filePath);} catch (IO Exception 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] + "");} System. out. print ("\ n") ;}}/*** verify whether the file exists and whether it is an excel file * @ param filePath * @ return result */public boolean validateExcel (String filePath) {if (filePath = null |! (IsExcel2003 (filePath) | isExcel2007 (filePath) {System. err. println ("the file is not an excel file! "); Return false;} File file = new File (filePath); if (file = null |! File. exists () {System. err. println ("the file does not exist! "); Return false;} return true;}/*** determine whether the file path of excel version 2003 * @ param filePath * @ return result */public boolean isExcel2003 (String filePath) {return filePath. matches ("^. + \\. (? I) (xls) $ ");}/*** determine whether the excel version is 2007 * @ param filePath file path * @ return result */public boolean isExcel2007 (String filePath) {return filePath. matches ("^. + \\. (? I) (xlsx) $ ");} /*** convert the obtained data type to String to prevent scientific notation * @ param decimal data * @ return result */public String decimalFormat (Double decimal) {return df. format (decimal );}}

References:

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

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

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.