xls java_Java 实现后缀xls文件读取

此篇博客介绍了如何使用Java通过Apache POI库从Excel文件中读取并解析用户角色数据,包括文件路径获取、文件类型检查、数据遍历和UserRoleBean对象的创建。重点展示了如何处理Excel内容,并返回一个UserRoleBean列表。
摘要由CSDN通过智能技术生成

packagenc.xyzq.client;importjava.io.File;importjava.io.FileInputStream;importjava.util.ArrayList;importjava.util.List;importjava.util.logging.Logger;importnc.xyzq.pub.Pub;importnc.xyzq.uuib.service.impl.UserRoleBean;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;/*** ClassName:Client

* Function: TODO

* Reason: TODO

* Date: 2018年11月20日 下午15:59:31

*@authorlizm

*@sinceJDK 1.6

**/

public classClient {private static final Logger logger = (Logger) Logger.getLogger(Client.class.getName());private ListgetInfoFromExcle(){

List list = newArrayList();//excel文件路径

String excelPath = Pub.getPropertiesValue("relationship", "path");//读取配置文件,例如 D://data/relationship.xls

try{//String encoding = "GBK";

File excel = newFile(excelPath);if (excel.isFile() && excel.exists()) { //判断文件是否存在

String[] split = excel.getName().split("\\."); //.是特殊字符,需要转义!!!!!

Workbook wb;//根据文件后缀(xls/xlsx)进行判断

if ( "xls".equals(split[1])){

FileInputStream fis= new FileInputStream(excel); //文件流对象

wb = newHSSFWorkbook(fis);

}/**else if ("xlsx".equals(split[1])){

FileInputStream fis = new FileInputStream(excel);

wb = new XSSFWorkbook(fis);

}*/

else{//System.out.println("文件类型错误!");

logger.info("文件类型错误!");returnlist;

}//开始解析

Sheet sheet = wb.getSheetAt(0); //读取sheet 0

int firstRowIndex = sheet.getFirstRowNum()+1; //第一行是列名,所以不读

int lastRowIndex =sheet.getLastRowNum();//System.out.println("firstRowIndex: "+firstRowIndex);

logger.info("firstRowIndex: "+firstRowIndex);//System.out.println("lastRowIndex: "+lastRowIndex);

logger.info("lastRowIndex: "+lastRowIndex);//System.out.println("执行进行中......");

logger.info("执行进行中......");//遍历行

for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {//System.out.println("rIndex: " + rIndex);

Row row =sheet.getRow(rIndex);if (row != null) {//行中第一个值所在的列序号

int firstCellIndex =row.getFirstCellNum();//行中最后一个值所在的列序号

int lastCellIndex =row.getLastCellNum();

UserRoleBean userRoleBean= newUserRoleBean();//遍历列//for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {

for (int cIndex = 0; cIndex < 3; cIndex++) {

Cell cell=row.getCell(cIndex);if (cell != null) {//将表格内容强制转换为字符型

cell.setCellType(Cell.CELL_TYPE_STRING);//System.out.println(cell.toString());

if(cIndex == 0){

userRoleBean.setUserCode(cell.toString());

}else if(cIndex == 1){

userRoleBean.setRoleid(cell.toString());

}else if(cIndex == 2){

userRoleBean.setIsflag(cell.toString());

}

}

}if(!("".equals(userRoleBean.getUserCode())) && !("".equals(userRoleBean.getRoleid()))){//System.out.println(">>>0000000>>:"+userRoleBean.getRoleid());

list.add(userRoleBean);

}

}

}

}else{//System.out.println("找不到指定的文件");

logger.info("找不到指定的文件");

}

}catch(Exception e) {

e.printStackTrace();

}returnlist;

}public static voidmain(String[] args) {

List list = newArrayList();

Client client= newClient();

list=client.getInfoFromExcle();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值