poi读

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();
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值