POI读取excel2003兼容2007

/**
 * 
 */
package cn.wsn.wxepb.service;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
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 cn.wsn.wxepb.entity.WaterQualityEntity;

/**
 * @author steveguo
 *
 */
public class WaterQualityService {

	private final static String EXCEL_2003_SUFFIX = ".xls";
	private final static String EXCEL_2007_SUFFIX = ".xlsx";
	private final static int STARTROW = 2;
	private final static int STARTCOL = 1;
	private Workbook workBook;
	private Sheet sheet;
	
	
	public WaterQualityService(String filePath) throws FileNotFoundException, IOException {
		init(filePath);
	}
	
	private void init(String filePath) throws FileNotFoundException, IOException {
		File file;
		InputStream is = null;
		BufferedInputStream bis = null;
		try {
			file = new File(filePath);
			is = new FileInputStream(file);
			bis = new BufferedInputStream(is);
			if(filePath.endsWith(EXCEL_2003_SUFFIX)) {
				workBook = new HSSFWorkbook(bis);
			}
			if(filePath.endsWith(EXCEL_2007_SUFFIX)) {
				workBook = new XSSFWorkbook(bis);
			}
			sheet = workBook.getSheetAt(0);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new FileNotFoundException("读取的Excel文件不存在:" + e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			throw new IOException("读取Excel文件出现IO异常:" + e.getMessage());
		} finally {
			if(is != null) {
				is.close();
			}
			if(bis != null) {
				bis.close();
			}
		}
	}
	
	/**
	 * 读取Excel标题内容
	 * @return
	 */
	public String[] readExcelTitle(){
		Row row = sheet.getRow(0);
		//标题列数
		int totalCol = row.getPhysicalNumberOfCells();
		System.out.println("Excel总列数:" + totalCol);
		String[] title = new String[totalCol];
		for (int i = 0; i < totalCol; i++) {
			System.out.println(row.getCell(i).getStringCellValue());
			title[i] = row.getCell(i).getStringCellValue();
		}
		return title;
	}
	
	/**
	 * 读取Excel数据
	 * @return
	 */
	public List<WaterQualityEntity> readExcelData(){
		List<WaterQualityEntity> entityList = new ArrayList<WaterQualityEntity>();
		Row row = sheet.getRow(0);
		int totalRow = sheet.getLastRowNum();
		WaterQualityEntity entity;
		for (int rowIndex = STARTROW; rowIndex < totalRow; rowIndex++) {
			row = sheet.getRow(rowIndex);
			entity = new WaterQualityEntity();
			int colNum = STARTCOL;
			entity.setZdmc(getStringCellValue(row.getCell(colNum ++)));
			String tmp = row.getCell(colNum).getStringCellValue();
			System.out.println(tmp);
			try {
				entity.setJcsj(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(StringUtils.trim(row.getCell(colNum ++).getStringCellValue().trim().replace(" ", ""))));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			entity.setSw(getNumericCellValue(row.getCell(colNum ++)));
			entity.setPh(getNumericCellValue(row.getCell(colNum ++)));
			entity.setRjy(getNumericCellValue(row.getCell(colNum ++)));
			entity.setDd(getNumericCellValue(row.getCell(colNum ++)));
			entity.setZd(getNumericCellValue(row.getCell(colNum ++)));
			entity.setCod(getNumericCellValue(row.getCell(colNum ++)));
			entity.setAd(getNumericCellValue(row.getCell(colNum ++)));
			entity.setZl(getNumericCellValue(row.getCell(colNum ++)));
			entity.setToc(getNumericCellValue(row.getCell(colNum ++)));
			entity.setSwei(getNumericCellValue(row.getCell(colNum ++)));
			//entity.setLs(getNumericCellValue(row.getCell(colNum ++)));
			//entity.setLl(getNumericCellValue(row.getCell(colNum ++)));
			entityList.add(entity);
		}
		return entityList;
	}
	
	private String getStringCellValue(Cell cell) {
		String value = "Excel没有值";
		if(cell == null) {
			return value;
		}
		value = cell.getStringCellValue().trim().replace(" ", "");
		if("".equals(value) || null == value) {
			return "Excel没有值";
		}
		return value;
	}
	
	private BigDecimal getNumericCellValue(Cell cell) {
		if(cell == null) {
			return BigDecimal.ZERO;
		}
		String tmpVal = StringUtils.trim(cell.getStringCellValue().trim()).replaceAll("  ", "");
		if("".equals(tmpVal) || null == tmpVal) {
			return BigDecimal.ZERO;
		}
		return new BigDecimal(tmpVal);
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			WaterQualityService service = new WaterQualityService("d:/水质量数据.xls");
			service.readExcelTitle();
			List<WaterQualityEntity> entityList = service.readExcelData();
			for(WaterQualityEntity entity : entityList) {
				System.out.println(entity.getZdmc());
				System.out.println(entity.getSw());
				System.out.println(entity.getPh());
				System.out.println(entity.getRjy());
				System.out.println(entity.getDd());
				System.out.println(entity.getZd());
				System.out.println(entity.getCod());
				System.out.println(entity.getAd());
				System.out.println(entity.getZl());
				System.out.println(entity.getToc());
				System.out.println(entity.getSwei());
				System.out.println(entity.getLs());
				System.out.println(entity.getLl());
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

需要以下包:


你可能感兴趣的:(POI读取excel2003兼容2007)