java xlsx_java 解析xls 或xlsx

/**

*

* @描述:工具类

*

* @作者:建宁

*

* @时间:2012-08-29 下午16:30:40

*/

class WDWUtil

{

/**

*

* @描述:是否是2003的excel,返回true是2003

*

* @作者:建宁

*

* @时间:2012-08-29 下午16:29:11

*

* @参数:@param filePath 文件完整路径

*

* @参数:@return

*

* @返回值:boolean

*/

public static boolean isExcel2003(String filePath)

{

return filePath.matches("^.+\\.(?i)(xls)$");

}

/**

*

* @描述:是否是2007的excel,返回true是2007

*

* @作者:建宁

*

* @时间:2012-08-29 下午16:28:20

*

* @参数:@param filePath 文件完整路径

*

* @参数:@return

*

* @返回值:boolean

*/

public static boolean isExcel2007(String filePath)

{

return filePath.matches("^.+\\.(?i)(xlsx)$");

}

}

/**

* 部分数据更新楼栋信息

* **/

public static String analyzeXlsPart(String path){

String message="";

if(StringUtils.isEmpty(path)){

logger.error("解析文件路径不能为空!");

message="解析文件路径不能为空!";

return message;

}

File file=new File(path);

//File file=new File("D:\\环境\\格美特\\2017项目需求\\恒大\\腾讯脱机门禁\\智慧物业2017-1121\\中原万达住宅_operator_null.xls");

InputStream is = null;

try {

is = new FileInputStream(file);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Workbook  hssfWorkbook = null;

boolean isExcel2003 = true;

if (WDWUtil.isExcel2007(path))

{

isExcel2003 = false;

}

if (isExcel2003)

{

try {

hssfWorkbook = new HSSFWorkbook(is);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

else

{

try {

hssfWorkbook = new XSSFWorkbook(is);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/*try {

hssfWorkbook = new HSSFWorkbook(is);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}*/

// 获取每一个工作薄

for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {

Sheet  hssfSheet =  hssfWorkbook.getSheetAt(numSheet);

if (hssfSheet == null) {

continue;

}

// 获取当前工作薄的每一行

for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {

Row hssfRow = hssfSheet.getRow(rowNum);

if (hssfRow != null) {

Cell one = hssfRow.getCell(0);

//读取第一列数据

Cell two = hssfRow.getCell(1);

//读取第二列数据

Cell three = hssfRow.getCell(2);

//读取第四列数据

Cell four = hssfRow.getCell(3);

//读取第五列数据

// HSSFCell five = hssfRow.getCell(4);

//String sss=getRealData(getValue(one));

/*System.out.println(String.valueOf(sss));

System.out.println((getValue(two)));

System.out.println(getValue(three));

System.out.println(getRealData(getValue(four)));

System.out.println(getRealData(getValue(five)));

System.out.println(getValue(six));*/

String community_id=getRealData(getValue(one));

String bulidName=getValue(two);

String unitName=getValue(three);

String build_code=getValue(four);

String headDes=community_id+",小区批量修改楼栋信息执行完毕";

try {

updatePart(community_id,bulidName,unitName,build_code);

if(rowNum==hssfSheet.getLastRowNum()-1)

message=message+headDes+"!!!!";

} catch (Exception e) {

e.printStackTrace();

message=message+"修改该条【community_id:"+community_id+"】,【bulidName:"+bulidName+"】,【unitName:"+unitName+"】楼栋信息发生异常:"+e.getMessage();

}

}

}

}

logger.info(message);

System.out.println(message);

return message;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值