天天看点

java代码读取excel文件_Java读取Excel文件

使用poi插件解析Excel文件

下载插件poi-bin-3.8

package com;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.nio.charset.Charset;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

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;

import org.springframework.stereotype.Component;

import com.csvreader.CsvReader;

import com.elin.csp.model.shopUser.entity.ShopUser;

@Component

public class InputExcelFileService {

private int totalRows = 0;

private int totalCells = 0;

private String errorInfo;

public InputExcelFileService() {

}

public int getTotalRows() {

return totalRows;

}

public int getTotalCells() {

return totalCells;

}

public String getErrorInfo() {

return errorInfo;

}

public boolean validateExcel(String fileName) {

if (fileName == null

|| !(WDWUtil.isExcel2003(fileName) || WDWUtil

.isExcel2007(fileName))) {

errorInfo = "文件名不是excel格式";

return false;

}

File file = new File(fileName);

if (file == null || !file.exists()) {

errorInfo = "文件不存在";

return false;

}

return true;

}

public List> read(String fileName) {

List> dataLst = new ArrayList>();

InputStream is = null;

try {

if (!validateExcel(fileName)) {

System.out.println(errorInfo);

return null;

}

boolean isExcel2003 = true;

if (WDWUtil.isExcel2007(fileName)) {

isExcel2003 = false;

}

File file = new File(fileName);

is = new FileInputStream(file);

dataLst = read(is, isExcel2003);

is.close();

} catch (Exception ex) {

ex.printStackTrace();

} finally {

if (is != null) {

try {

is.close();

} catch (IOException e) {

is = null;

e.printStackTrace();

}

}

}

return dataLst;

}

public List> read(InputStream inputStream, boolean isExcel2003) {

List> dataLst = null;

try {

Workbook wb = isExcel2003 ? new HSSFWorkbook(inputStream)

: new XSSFWorkbook(inputStream);

dataLst = read(wb);

} catch (IOException e) {

e.printStackTrace();

}

return dataLst;

}

private List> read(Workbook wb) {

List> dataLst = new ArrayList>();

Sheet sheet = wb.getSheetAt(0);

this.totalRows = sheet.getPhysicalNumberOfRows();

if (this.totalRows >= 1 && sheet.getRow(0) != null) {

this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();

}

for (int r = 0; r < this.totalRows; r++) {

Row row = sheet.getRow(r);

if (row == null) {

continue;

}

List rowLst = new ArrayList();

for (short c = 0; c < this.getTotalCells(); c++) {

Cell cell = row.getCell(c);

String cellValue = "";

if (cell == null) {

rowLst.add(cellValue);

continue;

}

// cellValue = cell.getStringCellValue();

// rowLst.add(cellValue);

switch (cell.getCellType()) {

// 假如当前Cell的Type为NUMERIC

case HSSFCell.CELL_TYPE_NUMERIC: {

// 判定当前的cell是否为Date

if (HSSFDateUtil.isCellDateFormatted(cell)) {

// 假如是Date类型则,取得该Cell的Date值

Date date = cell.getDateCellValue();

// 把Date转换本钱地格式的字符串

cellValue = cell.getDateCellValue().toLocaleString();

}

// 假如是纯数字

else {

// 纯数字时,一般为电话号码或手机号,所以以字符串类型保存

cell.setCellType(Cell.CELL_TYPE_STRING);

cellValue = cell.getStringCellValue();

// // 取得当前Cell的数值

// Integer num = new Integer((int)

// cell.getNumericCellValue());

// cellValue = String.valueOf(num);

}

break;

}

// 假如当前Cell的Type为STRIN

case HSSFCell.CELL_TYPE_STRING:

// 取得当前的Cell字符串

cellValue = cell.getStringCellValue().replaceAll("'", "''");

break;

// 默认的Cell值

default:

cellValue = " ";

}

rowLst.add(cellValue);

}

dataLst.add(rowLst);

}

return dataLst;

}

public static void main(String[] args) throws Exception {

InputExcelFileService poi = new InputExcelFileService();

// List> list = poi.read("d:/aaa.xls");

List> list = poi.read("d:/aab.xlsx");

if (list != null) {

for (int i = 0, ilen = list.size(); i < ilen; i++) {

System.out.println("第" + (i + 1) + "行");

List cellList = list.get(i);

for (int j = 0, jlen = cellList.size(); j < jlen; j++) {

System.out.print(" 第" + (j + 1) + "列值:");

System.out.println(cellList.get(j));

}

}

}

}

public List wdwExcelMethod(String path) {

InputExcelFileService poi = new InputExcelFileService();

List> list = poi.read(path);

return list;

}

}

class WDWUtil {

public static boolean isExcel2003(String fileName) {

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

}

public static boolean isExcel2007(String fileName) {

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

}

}