爱编程的欧巴

让我们成长吧~
  博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

java excel 导入

Posted on 2018-12-19 10:27  爱编程的欧巴  阅读(216)  评论(0编辑  收藏  举报

package com.app.wii.util;

import java.io.FileInputStream;
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.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
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;

public class ImportExcelUtil {
private final static String excel2003L = ".xls"; // 2003- 版本的excel
private final static String excel2007U = ".xlsx"; // 2007+ 版本的excel

/**
* 获取IO流中的数据,组装成List<List<Object>>对象
*
* @Description 获取IO流中的数据,组装成List<List<Object>>对象
* @param in
* @param fileName
* @return
* @throws Exception
*/
public List<List<Object>> getListByExcel(FileInputStream in, String fileName) throws Exception {
List<List<Object>> list = null;

// 创建Excel工作薄
Workbook work = this.getWorkbook(in, fileName);
if (null == work) {
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;

list = new ArrayList<List<Object>>();
// 遍历Excel中所有的sheet
for (int i = 0; i < work.getNumberOfSheets(); i++) {
sheet = work.getSheetAt(i);
if (sheet == null) {
continue;
}

// 遍历当前sheet中的所有行
for (int j = sheet.getFirstRowNum(); j < sheet.getLastRowNum() + 1; j++) {
row = sheet.getRow(j);
if (row == null || row.getFirstCellNum() == j) {
continue;
}

// 遍历所有的列
List<Object> li = new ArrayList<Object>();
for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
cell = row.getCell(y);
if (cell != null) {
li.add(this.getCellValue(cell));
}
}
list.add(li);
}
}
// ((FileInputStream) work).close();
return list;
}

/**
* 根据文件后缀,自适应上传文件的版本
*
* @Description 根据文件后缀,自适应上传文件的版本
* @param inStr
* @param fileName
* @return
* @throws Exception
*/
public Workbook getWorkbook(FileInputStream inStr, String fileName) throws Exception {
Workbook wb = null;
String fileType = fileName.substring(fileName.lastIndexOf("."));
if (excel2003L.equals(fileType)) {
wb = new HSSFWorkbook(inStr); // 2003-
} else if (excel2007U.equals(fileType)) {
wb = new XSSFWorkbook(inStr); // 2007+
} else {
throw new Exception("解析的文件格式有误!");
}
return wb;
}

/**
* 对表格中数值进行格式化
*
* @Description 对表格中数值进行格式化
* @param cell
* @return
*/
public Object getCellValue(Cell cell) {
Object value = null;
DecimalFormat df = new DecimalFormat("0"); // 格式化number String字符
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 日期格式化
DecimalFormat df2 = new DecimalFormat("0.00"); // 格式化数字

switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if ("General".equals(cell.getCellStyle().getDataFormatString())) {
value = df.format(cell.getNumericCellValue());
} else if ("yyyy\\-mm\\-dd".equals(cell.getCellStyle().getDataFormatString()) || "m/d/yy".equals(cell.getCellStyle().getDataFormatString())
|| "dd/mm/yyyy".equals(cell.getCellStyle().getDataFormatString()) || "d/m/yyyy".equals(cell.getCellStyle().getDataFormatString())
|| "yyyy/mm/dd".equals(cell.getCellStyle().getDataFormatString())) {
value = sdf.format(cell.getDateCellValue());
} else {
value = df2.format(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK:
value = "";
break;
case Cell.CELL_TYPE_FORMULA:
FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
evaluator.evaluateFormulaCell(cell);
CellValue cellValue = evaluator.evaluate(cell);
switch (cellValue.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
value=cellValue.getBooleanValue();
break;
case Cell.CELL_TYPE_NUMERIC:
value=cellValue.getNumberValue();
break;
case Cell.CELL_TYPE_STRING:
value=cellValue.getStringValue();
break;
case Cell.CELL_TYPE_BLANK:
break;
case Cell.CELL_TYPE_ERROR:
break;

// CELL_TYPE_FORMULA will never happen
case Cell.CELL_TYPE_FORMULA:
break;
}
default:
break;
}
return value;
}

public int getAge(Date birthDate) {
if (birthDate == null)
throw new RuntimeException("出生日期不能为null");

int age = 0;
Date now = new Date();

SimpleDateFormat format_y = new SimpleDateFormat("yyyy");
SimpleDateFormat format_M = new SimpleDateFormat("MM");
SimpleDateFormat format_d = new SimpleDateFormat("dd");

String birth_year = format_y.format(birthDate);
String this_year = format_y.format(now);

String birth_month = format_M.format(birthDate);
String this_month = format_M.format(now);

String birth_day = format_d.format(birthDate);
String this_day = format_d.format(now);

// 初步,估算
age = Integer.parseInt(this_year) - Integer.parseInt(birth_year);

// 如果未到出生月份,则age - 1
if (this_month.compareTo(birth_month) < 0) {
age -= 1;
} else if (this_month.compareTo(birth_month) == 0) {
if (this_day.compareTo(birth_day) < 0) {
age -= 1;
}
}
return age;
}

public String[] getEecelTitle(FileInputStream in, String fileName) throws Exception {
// 创建Excel工作薄
Workbook work = this.getWorkbook(in, fileName);
if (null == work) {
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;

// 遍历Excel中所有的sheet
for (int i = 0; i < work.getNumberOfSheets(); i++) {
sheet = work.getSheetAt(i);
if (sheet == null) {
continue;
}

// 遍历当前sheet中的所有行
for (int j = sheet.getFirstRowNum(); j < sheet.getLastRowNum() + 1; j++) {
row = sheet.getRow(j);
if (row == null) {
continue;
}

// 遍历所有的列
String[] arr = new String[row.getLastCellNum()];
for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
cell = row.getCell(y);
if (cell != null) {
arr[y] = this.getCellValue(cell).toString();
}
}
return arr;
}
}
// ((FileInputStream) work).close();
return null;
}

public static void main(String[] args) {
//1983-10-11
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
new ImportExcelUtil().getAge(sdf.parse("1983-10-11"));
} catch (Exception e) {
}
}
}