java 获取excel,java读取Excel读取

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 readerExcel(String pathFile) {

Workbook wb = null;

Row row = null;

Sheet sheet = null;

InputStream is = null;

List 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();

// 获取总行数

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;

}

}

原文:http://blog.csdn.net/blogluoqi/article/details/26587889

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值