JAVA 解析excel文件 poi方式
2017-11-25 16:30
375 查看
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>
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>
相关文章推荐
- Java通过jxl解析Excel文件入库,及日期格式处理方式 (附源代码)
- Java基础系列19:使用JXL或者POI生成和解析Excel文件
- java后台poi方式导出excel和解析excel
- Java+Flex+POI 解析Excel文件(读取文件中想要的内容)
- java通过poi解析Excel文件,直接读取客户端文件
- Excel大文件解析: Java POI SAX解析Excel 文件
- POI以SAX方式解析Excel2007大文件(包含空单元格的处理) Java生成CSV文件实例详解
- 实现poi方式生成excel文件和web类根据路径生成java实例的反射技术的springmvc方法的方法
- poi读取Excel时日期为数字 的解决方法 转载 2017年12月14日 10:31:14 标签:poi /excel /java 19 在使用poi解析Excel文件时,会发现,表格中的日期解析出
- iMOOC学习笔记:Java的Excel读写操作入门实践-利用POI解析Excel文件(二)
- java 操作 poi 解析、读取 Excel 文件
- Java 使用poi解析Excel文件(兼容2007)
- JAVA解析文件pdf、word、excel
- POI读取EXCEL文件的错误 java.io.IOException: Invalid header signature; read 23
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表
- Java程序员从笨鸟到菜鸟之(一百零五)java操作office和pdf文件(三)利用jxl实现数据导出excel报表以及与POI的区别
- Java用系统方式打开Excel/PDF等文件
- java通过jxl解析Excel文件
- java使用poi解析或处理excel的时候,如何防止数字变成科学计数法的形式
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表