[Java] detailed explanation of source code java reads excel content

Source: Internet
Author: User

This example mainly describes how to print the content in an excel file through a java program. Paste the source code directly, and then sort out the principles:
[Java]
Package dec;
 
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. IOException;
 
Import org. apache. poi. hssf. usermodel. HSSFCell;
Import org. apache. poi. hssf. usermodel. HSSFRow;
Import org. apache. poi. hssf. usermodel. HSSFSheet;
Import org. apache. poi. hssf. usermodel. HSSFWorkbook;
Import org. apache. poi. poifs. filesystem. POIFSFileSystem;
 
/**
*
* <P>
* Title: JAVA reads the Excel test code
* </P>
*
* <P>
* Description: example business class
* </P>
*
* <P>
* Copyright: Copyright (c) 2012
* </P>
*
*
* @ Author dml @ 2012-12-10
* @ Version 1.0
*/
Public class ExcelReader {
Private HSSFWorkbook wb = null; // book [partition des sheet]
Private HSSFSheet sheet = null;
Private HSSFRow row = null;

Private int sheetNum = 0; // worksheet No. sheetnum
Private int rowNum = 0;
Private FileInputStream FCM = null;
Private File file = null;
 
Public ExcelReader (){
}
 
Public ExcelReader (File file ){
This. file = file;
}
 
Public void setRowNum (int rowNum ){
This. rowNum = rowNum;
}
 
Public void setSheetNum (int sheetNum ){
This. sheetNum = sheetNum;
}
 
Public void setFile (File file ){
This. file = file;
}
 
/**
* Read the excel file to obtain the HSSFWorkbook object
*/
Public void open () throws IOException {
FS = new FileInputStream (file );
Wb = new HSSFWorkbook (new POIFSFileSystem (FCM ));
FCM. close ();
}
 
/**
* Returns the number of sheet tables.
*
* @ Return int
*/
Public int getSheetCount (){
Int sheetCount =-1;
SheetCount = wb. getNumberOfSheets ();
Return sheetCount;
}
 
/**
* Number of records under sheetNum
*
* @ Return int
*/
Public int getRowCount (){
If (wb = null)
System. out. println ("=============== the WorkBook is empty ================== ");
HSSFSheet sheet = wb. getSheetAt (this. sheetNum );
Int rowCount =-1;
RowCount = sheet. getLastRowNum ();
Return rowCount;
}
 
/**
* Read rowCount of the specified sheetNum
*
* @ Param sheetNum
* @ Return int
*/
Public int getRowCount (int sheetNum ){
HSSFSheet sheet = wb. getSheetAt (sheetNum );
Int rowCount =-1;
RowCount = sheet. getLastRowNum ();
Return rowCount;
}
 
/**
* Get the content of the specified row.
*
* @ Param lineNum
* @ Return String []
*/
Public String [] readExcelLine (int lineNum ){
Return readExcelLine (this. sheetNum, lineNum );
}
 
/**
* Content of the specified worksheet and number of rows
*
* @ Param sheetNum
* @ Param lineNum
* @ Return String []
*/
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;
}
 
/**
* Read the content of a specified Column
*
* @ Param cellNum
* @ Return String
*/
Public String readStringExcelCell (int cellNum ){
Return readStringExcelCell (this. rowNum, cellNum );
}
 
/**
* Specify the row and column numbers.
*
* @ Param rowNum
* @ Param cellNum
* @ Return String
*/
Public String readStringExcelCell (int rowNum, int cellNum ){
Return readStringExcelCell (this. sheetNum, rowNum, cellNum );
}
 
/**
* Content in a worksheet, row, or column
*
* @ Param sheetNum
* @ Param rowNum
* @ Param cellNum
* @ Return String
*/
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 (short) cellNum )! = Null) {// add this condition
 
// Judge
 
Switch (row. getCell (short) cellNum). getCellType ()){
Case HSSFCell. CELL_TYPE_FORMULA:
StrExcelCell = "FORMULA ";
Break;
Case HSSFCell. CELL_TYPE_NUMERIC :{
StrExcelCell = String. valueOf (row. getCell (short) cellNum)
. GetNumericCellValue ());
}
Break;
Case HSSFCell. CELL_TYPE_STRING:
StrExcelCell = row. getCell (short) cellNum)
. GetStringCellValue ();
Break;
Case HSSFCell. CELL_TYPE_BLANK:
StrExcelCell = "";
Break;
Default:
StrExcelCell = "";
Break;
}
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return strExcelCell;
}
 
// The main function is used for testing. This function is generally used for operations.
Public static void main (String args []) {
// Mark the file path
File file = new File ("D: \ testExcel.xls ");
ExcelReader readExcel = new ExcelReader (file );
// Open the file
Try {
ReadExcel. open ();
} Catch (IOException e ){
E. printStackTrace ();
}
ReadExcel. setSheetNum (0); // you can specify 0 for a worksheet.
// Total number of rows
Int count = readExcel. getRowCount ();
// Read the content in the Excel file cyclically
 
For (int I = 0; I <= count; I ++ ){
String [] rows = readExcel. readExcelLine (I );
For (int j = 0; j <rows. length; j ++ ){
System. out. print (rows [j] + "");
System. out. print ("| ");
}
System. out. print ("\ n ");
}
}
}
Execution result:
[Plain]
D: \ testExcel.xls prints the content.

Jar package:
Commons-io-1.1.jar
Excelutils. jar
Poi-3.0.1.jar
Tm-extractors-0.4.jar
These items have been packaged and uploaded to poi.rar. You can download them to the project.

Apache POI is used to implement this function. The following is an introduction to Baidu Encyclopedia:
[Plain]
Apache POI is an open-source library of the Apache Software Foundation. POI provides APIs for Java programs to read and write Microsoft Office files.
Structure:
HSSF-provides the ability to read and write Microsoft Excel files.
XSSF-provides the ability to read and write files in Microsoft Excel OOXML format.
HWPF-read and write Microsoft Word files.
HSLF-supports reading and writing Microsoft PowerPoint files.
HDGF-read and write Microsoft Visio files.

The Apache Foundation has many open-source projects. As a java programmer, you should be more or less familiar with them.

Taking the time to understand the content of these projects can quickly improve their software design capabilities.

P.s. Java programmers do not realize the function and think that the task is completed. After the task is completed, a little bit of detours in the development process will be learned a lot.

 

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.