package util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.StringUtils;
public class PoiUtils {
public List<String[]> parseExcel(String fileName,InputStream inputStream,String suffix,int startRow) {
Workbook workbook = null;
if(("xls").equals("suffix")){
//03
workbook = new HSSFWorkbook(inputStream);
}else if(("xls").equals("suffix") ){
// workbook = new XSSFWorkbook(inputStream);
}else {
return null;
}
//获取工作表
Sheet sheet = workbook.getSheetAt(0);
if(sheet ==null) {
return null;
}
int lastRowNum = sheet.getLastRowNum();
if(lastRowNum<=startRow) {
return null;
}
//结果
List<String[]> result = new ArrayList<>();
Row row = null;
Cell cell =null;
for(int rowNum = startRow;rowNum<=lastRowNum;rowNum++) {
row = sheet.getRow(rowNum);
short firstCellNum = row.getFirstCellNum();
short lastCellNum = row.getLastCellNum();
if(lastCellNum!=0) {
String[] rowArray = new String[lastCellNum];
for(int cellNum = firstCellNum;cellNum<lastCellNum;cellNum++) {
cell = row.getCell(cellNum);
//判断单元格是否有数据
if(cell == null) {
rowArray[cellNum] = null;
}else {
rowArray[cellNum] = parseCell(cell);
}
}
result.add(rowArray);
}
}
return result;
}
public String parseCell(Cell cell) {
String cellStr = null;
//判断9单元格类型
switch(cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
cellStr = cell.getRichStringCellValue().toString();
break;
case HSSFCell.CELL_TYPE_BLANK :
cellStr ="";
break;
case HSSFCell.CELL_TYPE_NUMERIC :
//数学类型,包含日期,数字,时间
//判断日期(年月日 2018-08-08)类型
if(HSSFDateUtil.isCellDateFormatted(cell)) {
//判断具体类型,是日期还是时间
SimpleDateFormat sdf =null;
if(cell.getCellType() == HSSFDataFormat.getBuiltinFormat("h:mm")) {
//时间
sdf = new SimpleDateFormat("HH:MM");
}else {
//日期
sdf = new SimpleDateFormat("yyyy-mmm-dd");
}
Date temp = cell.getDateCellValue();
cellStr = sdf.format(temp);
}else {
//数字
double temp = cell.getNumericCellValue();
DecimalFormat format = new DecimalFormat();
//查看单元格具体央视类型
String dataFormatString = cell.getCellStyle().getDataFormatString();
//如果
if(dataFormatString.equals("General")) {
//定义格式化正则,适用具体有效数据进行格式化,且只保留有效数据
format.applyPattern("#.###"); //.3#保留3位,一个#代表不保留具体位数
}
cellStr = format.format(temp
);
}
break;
}
return cellStr;
}
public static void main(String[] args) {
PoiUtils u = new PoiUtils();
InputStream input;
try {
input = new FileInputStream("xx.xsl");
String suffix = "xls";
int startRow = 3;
List<String[]> result = u.parseExcel("xx.sls", input, suffix, startRow);
for(String[] ss : result) {
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.StringUtils;
public class PoiUtils {
public List<String[]> parseExcel(String fileName,InputStream inputStream,String suffix,int startRow) {
Workbook workbook = null;
if(("xls").equals("suffix")){
//03
workbook = new HSSFWorkbook(inputStream);
}else if(("xls").equals("suffix") ){
// workbook = new XSSFWorkbook(inputStream);
}else {
return null;
}
//获取工作表
Sheet sheet = workbook.getSheetAt(0);
if(sheet ==null) {
return null;
}
int lastRowNum = sheet.getLastRowNum();
if(lastRowNum<=startRow) {
return null;
}
//结果
List<String[]> result = new ArrayList<>();
Row row = null;
Cell cell =null;
for(int rowNum = startRow;rowNum<=lastRowNum;rowNum++) {
row = sheet.getRow(rowNum);
short firstCellNum = row.getFirstCellNum();
short lastCellNum = row.getLastCellNum();
if(lastCellNum!=0) {
String[] rowArray = new String[lastCellNum];
for(int cellNum = firstCellNum;cellNum<lastCellNum;cellNum++) {
cell = row.getCell(cellNum);
//判断单元格是否有数据
if(cell == null) {
rowArray[cellNum] = null;
}else {
rowArray[cellNum] = parseCell(cell);
}
}
result.add(rowArray);
}
}
return result;
}
public String parseCell(Cell cell) {
String cellStr = null;
//判断9单元格类型
switch(cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
cellStr = cell.getRichStringCellValue().toString();
break;
case HSSFCell.CELL_TYPE_BLANK :
cellStr ="";
break;
case HSSFCell.CELL_TYPE_NUMERIC :
//数学类型,包含日期,数字,时间
//判断日期(年月日 2018-08-08)类型
if(HSSFDateUtil.isCellDateFormatted(cell)) {
//判断具体类型,是日期还是时间
SimpleDateFormat sdf =null;
if(cell.getCellType() == HSSFDataFormat.getBuiltinFormat("h:mm")) {
//时间
sdf = new SimpleDateFormat("HH:MM");
}else {
//日期
sdf = new SimpleDateFormat("yyyy-mmm-dd");
}
Date temp = cell.getDateCellValue();
cellStr = sdf.format(temp);
}else {
//数字
double temp = cell.getNumericCellValue();
DecimalFormat format = new DecimalFormat();
//查看单元格具体央视类型
String dataFormatString = cell.getCellStyle().getDataFormatString();
//如果
if(dataFormatString.equals("General")) {
//定义格式化正则,适用具体有效数据进行格式化,且只保留有效数据
format.applyPattern("#.###"); //.3#保留3位,一个#代表不保留具体位数
}
cellStr = format.format(temp
);
}
break;
}
return cellStr;
}
public static void main(String[] args) {
PoiUtils u = new PoiUtils();
InputStream input;
try {
input = new FileInputStream("xx.xsl");
String suffix = "xls";
int startRow = 3;
List<String[]> result = u.parseExcel("xx.sls", input, suffix, startRow);
for(String[] ss : result) {
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}