java excel解析类_解析Excel一个工具类!

package com.wukong.cft.common.util;

import java.io.IOException;

import java.io.InputStream;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCell;

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.apache.poi.xssf.usermodel.XSSFWorkbook;

/**

* 解析Excel代码分享

* @author Love

*

*/

public class ExcalRead {

//给这个方法传入一个文件流返回一个list集合

public static ArrayList> readExcel(InputStream inputStream) {

ArrayList> Row =new ArrayList>();

try {

Workbook workBook = null;

try {

workBook = new XSSFWorkbook(inputStream);

} catch (Exception ex) {

workBook = new HSSFWorkbook(inputStream);

}

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

Sheet sheet = workBook.getSheetAt(numSheet);

if (sheet == null) {

continue;

}

// 循环行Row

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

Row row = sheet.getRow(rowNum);

if (row == null) {

continue;

}

// 循环列Cell

ArrayList arrCell =new ArrayList();

for (int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) {

Cell cell = row.getCell(cellNum);

if (cell == null) {

continue;

}

arrCell.add(getValue(cell));

}

Row.add(arrCell);

}

}

} catch (IOException e) {

System.out.println("e:"+e);

}

return Row;

}

//解析你Excel里面的值

private static String getValue(Cell cell) {

if (cell == null)

return "";

switch (cell.getCellType()) {

case HSSFCell.CELL_TYPE_NUMERIC:

if(HSSFDateUtil.isCellDateFormatted(cell)){

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();

}

return cell.getNumericCellValue()+"";

case HSSFCell.CELL_TYPE_STRING:

System.out.println(cell.getStringCellValue());

return cell.getStringCellValue()+"";

case HSSFCell.CELL_TYPE_FORMULA:

return cell.getCellFormula()+"";

case HSSFCell.CELL_TYPE_BLANK:

return "";

case HSSFCell.CELL_TYPE_BOOLEAN:

return cell.getBooleanCellValue() + "";

case HSSFCell.CELL_TYPE_ERROR:

return cell.getErrorCellValue() + "";

}

return "";

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值