Poi 导入

packagecom.huishang.utils;

 

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.IOException;

importjava.text.DecimalFormat;

importjava.text.SimpleDateFormat;

importjava.util.LinkedList;

importjava.util.List;

 

importorg.apache.poi.hssf.usermodel.HSSFCell;

importorg.apache.poi.hssf.usermodel.HSSFDateUtil;

importorg.apache.poi.hssf.usermodel.HSSFRow;

importorg.apache.poi.hssf.usermodel.HSSFSheet;

importorg.apache.poi.hssf.usermodel.HSSFWorkbook;

importorg.apache.poi.xssf.usermodel.XSSFCell;

importorg.apache.poi.xssf.usermodel.XSSFRow;

importorg.apache.poi.xssf.usermodel.XSSFSheet;

importorg.apache.poi.xssf.usermodel.XSSFWorkbook;

 

publicclassImportUtil{

 

publicstaticList<List<Object>>readExcel(Filefile)throwsIOException{

StringfileName=file.getName();

Stringextension=fileName.lastIndexOf(".")==-1?"":fileName.substring(fileName.lastIndexOf(".")+1);

if("xls".equals(extension)){

returnread2003Excel(file);

}elseif("xlsx".equals(extension)){

returnread2007Excel(file);

}else{

thrownewIOException("不支持的文件类型");

}

}

 

/**

*读取Office2007excel

*/

privatestaticList<List<Object>>read2007Excel(Filefile)throwsIOException{

List<List<Object>>list=newLinkedList<List<Object>>();

//构造XSSFWorkbook对象,strPath传入文件路径

XSSFWorkbookxwb=newXSSFWorkbook(newFileInputStream(file));

//读取第一章表格内容

XSSFSheetsheet=xwb.getSheetAt(0);

Objectvalue=null;

XSSFRowrow=null;

XSSFCellcell=null;

for(inti=sheet.getFirstRowNum();i<=sheet.getPhysicalNumberOfRows();i++){

row=sheet.getRow(i);

if(row==null){

continue;

}

List<Object>linked=newLinkedList<Object>();

for(intj=row.getFirstCellNum();j<=row.getLastCellNum();j++){

cell=row.getCell(j);

if(cell==null){

continue;

}

DecimalFormatdf=newDecimalFormat("0");//格式化numberString

//字符

SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");//格式化日期字符串

DecimalFormatnf=newDecimalFormat("0");//格式化数字

switch(cell.getCellType()){

caseXSSFCell.CELL_TYPE_STRING:

//System.out.println(i+"行"+j+"列isStringtype");

value=cell.getStringCellValue();

break;

caseXSSFCell.CELL_TYPE_NUMERIC:

//System.out.println(i+"行"+j+"列isNumbertype;

//DateFormt:"+cell.getCellStyle().getDataFormatString());

if("@".equals(cell.getCellStyle().getDataFormatString())){

value=df.format(cell.getNumericCellValue());

}elseif("General".equals(cell.getCellStyle().getDataFormatString())){

value=nf.format(cell.getNumericCellValue());

}else{

value=sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));

}

break;

caseXSSFCell.CELL_TYPE_BOOLEAN:

//System.out.println(i+"行"+j+"列isBooleantype");

value=cell.getBooleanCellValue();

break;

caseXSSFCell.CELL_TYPE_BLANK:

//System.out.println(i+"行"+j+"列isBlanktype");

value="";

break;

default:

//System.out.println(i+"行"+j+"列isdefaulttype");

value=cell.toString();

}

if(value==null||"".equals(value)){

continue;

}

linked.add(value);

}

list.add(linked);

}

returnlist;

}

 

/**

*读取office2003excel

*

*@throwsIOException

*@throwsFileNotFoundException

*/

privatestaticList<List<Object>>read2003Excel(Filefile)throwsIOException{

List<List<Object>>list=newLinkedList<List<Object>>();

HSSFWorkbookhwb=newHSSFWorkbook(newFileInputStream(file));

HSSFSheetsheet=hwb.getSheetAt(0);

Objectvalue=null;

HSSFRowrow=null;

HSSFCellcell=null;

for(inti=sheet.getFirstRowNum();i<=sheet.getPhysicalNumberOfRows();i++){

row=sheet.getRow(i);

if(row==null){

continue;

}

List<Object>linked=newLinkedList<Object>();

for(intj=row.getFirstCellNum();j<=row.getLastCellNum();j++){

cell=row.getCell(j);

if(cell==null){

continue;

}

DecimalFormatdf=newDecimalFormat("0");//格式化numberString

//字符

SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");//格式化日期字符串

DecimalFormatnf=newDecimalFormat("0");//格式化数字

switch(cell.getCellType()){

caseXSSFCell.CELL_TYPE_STRING:

//System.out.println(i+"行"+j+"列isStringtype");

value=cell.getStringCellValue();

break;

caseXSSFCell.CELL_TYPE_NUMERIC:

//System.out.println(i+"行"+j+"列isNumbertype;

//DateFormt:"+cell.getCellStyle().getDataFormatString());

if("@".equals(cell.getCellStyle().getDataFormatString())){

value=df.format(cell.getNumericCellValue());

}elseif("General".equals(cell.getCellStyle().getDataFormatString())){

value=nf.format(cell.getNumericCellValue());

}else{

value=sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));

}

break;

caseXSSFCell.CELL_TYPE_BOOLEAN:

//System.out.println(i+"行"+j+"列isBooleantype");

value=cell.getBooleanCellValue();

break;

caseXSSFCell.CELL_TYPE_BLANK:

//System.out.println(i+"行"+j+"列isBlanktype");

value="";

break;

default:

//System.out.println(i+"行"+j+"列isdefaulttype");

value=cell.toString();

}

if(value==null||"".equals(value)){

continue;

}

linked.add(value);

}

list.add(linked);

}

returnlist;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值