java poi 读取Excel

阅读更多
java 用poi读取excel并区分2003和2007

2003 需要的jar:poi-x.jar
2007 需要的jar:poi-ooxml-x.jar poi-ooxml-schemas-x.jar,dom4j.jar,xmlbeans-2.6.0.jar

如果项目用了maven的话 pom配置中加入以下就可以了:

    3.9



  org.apache.poi
  poi
  ${poi-version}


  org.apache.poi
  poi-ooxml
  ${poi-version}


  org.apache.poi
  poi-ooxml-schemas
  ${poi-version}


  org.apache.xmlbeans
  xmlbeans
  2.6.0


  dom4j
  dom4j-core
  1.4-dev-8

  


package com.jynine.utils;

import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

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.CellStyle;
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;

/**
 * 
 * @author jynine
 * 
 */
public class ExcelUtil {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 字符
	private static DecimalFormat df = new DecimalFormat("0");// 格式化 number String
	private static DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字

	public static Workbook getWorkbook(File file) throws Exception {
		String fileName = file.getName();
		String extension = fileName.lastIndexOf(".") == -1 ? "" : fileName
				.substring(fileName.lastIndexOf(".") + 1);
		FileInputStream fis = new FileInputStream(file);
		// 根据不同的文件名返回不同类型的WorkBook
		if (extension.equals("xls")) {
			return new HSSFWorkbook(fis);
		} else if (extension.equals("xlsx")) {
			return new XSSFWorkbook(fis);
		} else {
			throw new Exception("不支持该格式的文件!");
		}
	}
	/**
	 * 读取excel 文件
	 * @param file
	 * @param startSheet
	 * @param startRow
	 * @return
	 */
	public static List> readExcel(File file,int startSheet,int startRow) {
		List> list = new LinkedList>();
		Workbook wb = null;
		try {
			wb = getWorkbook(file);
			Sheet sheet = wb.getSheetAt(startSheet);
			Object value = null;
			Row row = null;
			Cell cell = null;
			CellStyle cs = null;
			String csStr = null;
			Double numval = null;
			Iterator rows = sheet.rowIterator();
			while (rows.hasNext()) {
				row = (Row) rows.next();
				if(row.getRowNum() >= startRow){
					List cellList = new LinkedList();;
					Iterator cells = row.cellIterator();
					while (cells.hasNext()) {
						cell = (Cell) cells.next();
						switch (cell.getCellType()) {
						case Cell.CELL_TYPE_NUMERIC:
							cs = cell.getCellStyle();
							csStr = cs.getDataFormatString();
							numval = cell.getNumericCellValue();
							if ("@".equals(csStr)) {
								value = df.format(numval);
							} else if ("General".equals(csStr)) {
								value = nf.format(numval);
							} else {
								value = sdf.format(HSSFDateUtil.getJavaDate(numval));
							}
							break;
						case Cell.CELL_TYPE_STRING:
							value = cell.getStringCellValue();
							break;
						case Cell.CELL_TYPE_FORMULA:
							if (!cell.getStringCellValue().equals("")) {
		                        value = cell.getStringCellValue();
		                     } else {
		                        value = cell.getNumericCellValue() + "";
		                     }
							break;
						case Cell.CELL_TYPE_BLANK:
							value = "";
							break;
						case Cell.CELL_TYPE_BOOLEAN:
							value = cell.getBooleanCellValue();
							break;
						default:
							value = cell.toString();
						}
						cellList.add(value);
					}
					list.add(cellList);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	public static void main(String[] args) {
		File file = new File("D:\\test.xlsx");
		List> list = readExcel(file, 0, 0);
		for (int i = 0; i < list.size(); i++) {
			List objects = list.get(i);
			for (int j = 0; j < objects.size(); j++) {
				System.out.print(objects.get(j)+"=====");
			}
			System.out.println();
		}
	}
}

 
 
  • poi-ooxml-3.9.jar (914.7 KB)
  • 下载次数: 5
  • dom4j-core-1.4-dev-8.jar (283.1 KB)
  • 下载次数: 3
  • poi-3.9.jar (1.8 MB)
  • 下载次数: 3
  • poi-ooxml-schemas-3.9.jar (4.6 MB)
  • 下载次数: 6
  • xmlbeans-2.6.0.jar (2.6 MB)
  • 下载次数: 3

你可能感兴趣的:(excel,java,maven,poi)