java中读取Excel数据
package com.pcm.chni.equipment.frame;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
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 com.pcm.chni.equipment.model.HardwareEquipmentModel;
import com.pcm.chni.excel.util.ExcelConstants;
public class BatchEquipmentReaderExcel {
public static String error;
public static List<HardwareEquipmentModel> readerExcel(String pathFile) {
Workbook wb = null;
Row row = null;
Sheet sheet = null;
InputStream is = null;
List<HardwareEquipmentModel> list;
try {
File file = new File(pathFile);
// 设置要读取的文件路径
is = new FileInputStream(file);
// 如果不支持标记、重置, 则包装它
if (!is.markSupported()) {
is = new PushbackInputStream(is, 8);
}
// //创建工作薄,读取2003Excel和2007Excel
// HSSFWorkbook相当于一个excel文件,HSSFWorkbook是解析excel2007之前的版本(xls)
// 之后版本使用XSSFWorkbook(xlsx)
if (POIFSFileSystem.hasPOIFSHeader(is)) {
wb = new HSSFWorkbook(is);
} else if (POIXMLDocument.hasOOXMLHeader(is)) {
wb = new XSSFWorkbook(OPCPackage.open(is));
}
is.close();
} catch (IOException e) {
error = "文件读取错误请检查文件格式是否正确";
e.printStackTrace();
return null;
} catch (InvalidFormatException e2) {
error = "文件读取错误请检查文件格式是否正确";
e2.printStackTrace();
return null;
} catch (Exception e3) {
error = "文件读取错误请检查文件格式是否正确";
e3.printStackTrace();
return null;
}
// 获取第一个工作薄
sheet = wb.getSheetAt(0);
list = new ArrayList<HardwareEquipmentModel>();
// 获取总行数
int sumRow = sheet.getLastRowNum();
// 检查所有的行
int maxRow = ExcelConstants.MAX_ROWS;
// System.out.println("总共有"+sumRow+"行、、、、、、、、、、、、、、、、、、、、、");
//
// System.out.println("最大"+maxRow+"行、、、、、、、、、、、、、、、、、、、、、");
if (sumRow + 1 > maxRow) {
error = "工作表的行数不能大于" + maxRow + ",请重新设定导入数据的行数,分到几张表中再次导入!";
return null;
} else {
// 获取第一列 格式是否正确
row = sheet.getRow(0);// 获取第一列
if ((row.getCell(0) == null || !row.getCell(0).getStringCellValue()
.equals("设备编号"))
|| (row.getCell(1) == null || !row.getCell(1)
.getStringCellValue().equals("设备类型"))
|| (row.getCell(2) == null || !row.getCell(2)
.getStringCellValue().equals("生产日期"))) {
error = "表数据格式不对请检查";
return null;
}
}
SimpleDateFormat pattFormat = new SimpleDateFormat("yyyy-MM-dd");
// 获得行(默认重 0开始)
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
HardwareEquipmentModel model = new HardwareEquipmentModel();
// 获取行
row = sheet.getRow(i);
// 如果整行不能为空 执行下列代码
if (row.getCell(0) != null && row.getCell(1) != null) {
// 判断类型是否符合
if (row.getCell(0).getCellType() == row.getCell(0).CELL_TYPE_STRING
&& row.getCell(1).getCellType() == row.getCell(1).CELL_TYPE_STRING) {
// 判断编号是否符合规格 类型是否符合
model.setEquipment_id(row.getCell(0).toString().trim());
model.setEquipment_name(row.getCell(1).toString().trim());
if (row.getCell(2) != null
&& row.getCell(2).getCellType() == row.getCell(2).CELL_TYPE_NUMERIC) {
model.setProduction_date(pattFormat.format(row.getCell(
2).getDateCellValue()));
} else {
model.setProduction_date("");
}
// 去掉重复项
if (list != null) {
boolean flog = false;
for (int j = 0; i < list.size(); j++) {
if (list.get(j).getEquipment_id().equals(
model.getEquipment_id())
&& list.get(j).getEquipment_name().equals(
model.getEquipment_name())) {
flog = true;
break;
}
}
if (flog == false) {
list.add(model);
}
} else {
list.add(model);
}
}
}
}
return list;
}
}