1. 程式人生 > >JAVA 解析excel檔案 poi方式

JAVA 解析excel檔案 poi方式

package *.utils;


import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


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.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author poke
 * @created 2017-11-25
 */
public class ReadBusinessDataExcel {


public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";
public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";
public static final String EMPTY = "";
public static final String POINT = ".";
public static final String NOT_EXCEL_FILE = " : Not the Excel file!";
public static final String PROCESSING = "Processing...";


private static final Logger LOG = LoggerFactory.getLogger(ReadBusinessDataExcel.class);

public static void main(String args[]) throws IOException{
new ReadBusinessDataExcel().readExcel("e:/1.xlsx");
}

/**
* read the Excel file
* @param path the path of the Excel file
* @return
* @throws IOException
*/
public List<BusinessData> readExcel(String path) throws IOException {
if (path == null || EMPTY.equals(path)) {
return null;
} else {
String postfix = getPostfix(path);
if (!EMPTY.equals(postfix)) {
if (OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {
return readXls(path);
} else if (OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {
return readXlsx(path);
}
} else {
System.out.println(path + NOT_EXCEL_FILE);
}
}
return null;
}


/**
* Read the Excel 2010
* @param path the path of the excel file
* @return
* @throws IOException
*/
public List<BusinessData> readXlsx(String path) throws IOException {
System.out.println(PROCESSING + path);
InputStream is = new FileInputStream(path);
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
BusinessData BusinessData = null;
List<BusinessData> list = new ArrayList<BusinessData>();
// Read the Sheet
for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
if (xssfSheet == null) {
continue;
}
// Read the Row start at the 2nd
for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
try{
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
if (xssfRow != null) {
BusinessData = new BusinessData();
XSSFCell no = xssfRow.getCell(0);
XSSFCell name = xssfRow.getCell(1);
XSSFCell age = xssfRow.getCell(2);
XSSFCell score = xssfRow.getCell(3);

System.out.print(no);
System.out.print(" ");
System.out.print(name);
System.out.print(" ");
System.out.print(age);
System.out.print(" ");
System.out.print(score);
System.out.println("");




list.add(BusinessData);
}
}catch(Exception e){
LOG.error("處理業務資料");
throw new ShellPlatformException(ErrorCode.FILE_UPLOAD_ERROR, e);
}
}
}
return list;
}


/**
* Read the Excel 2003-2007
* @param path the path of the Excel
* @return
* @throws IOException
*/
public List<BusinessData> readXls(String path) throws IOException {
System.out.println(PROCESSING + path);
InputStream is = new FileInputStream(path);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
BusinessData BusinessData = null;
List<BusinessData> list = new ArrayList<BusinessData>();
// Read the Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// Read the Row start at the 2nd
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
BusinessData = new BusinessData();
HSSFCell no = hssfRow.getCell(0);
HSSFCell name = hssfRow.getCell(1);
HSSFCell age = hssfRow.getCell(2);
HSSFCell score = hssfRow.getCell(3);


System.out.print(no);
System.out.print(" ");
System.out.print(name);
System.out.print(" ");
System.out.print(age);
System.out.print(" ");
System.out.print(score);
System.out.println("");

list.add(BusinessData);
}
}
}
return list;
}


@SuppressWarnings("static-access")
private String getValue(XSSFCell xssfRow) {
if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {
return String.valueOf(xssfRow.getBooleanCellValue());
} else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {
return String.valueOf(xssfRow.getNumericCellValue());
} else {
return String.valueOf(xssfRow.getStringCellValue());
}
}


@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
return String.valueOf(hssfCell.getNumericCellValue());
} else {
return String.valueOf(hssfCell.getStringCellValue());
}
}

/**
     * get postfix of the path
     * @param path
     * @return
     */
    public  String getPostfix(String path) {
        if (path == null || EMPTY.equals(path.trim())) {
            return EMPTY;
        }
        if (path.contains(POINT)) {
            return path.substring(path.lastIndexOf(POINT) + 1, path.length());
        }
        return EMPTY;
    }

}

<!-- poi excel解析  -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>