Java中读取Excel功能实现_POI

这里使用apache的poi进行读取excel

1,新建javaproject 项目:TestExcel

2,导入包

包下载地址:http://poi.apache.org/download.html#POI-3.10-FINAL

百度网盘下载:http://pan.baidu.com/s/1i365mQT

导入根文件夹下、lib、ooxml-lib下的全部jar

4,操作读取excel

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.ss.usermodel.WorkbookFactory;

public class Test {
	// 得到一个File
	public static void main(String[] args) {
		File file = new File("F:/color.xlsx");
		Workbook workbook = null;
		try {
			workbook = WorkbookFactory.create(file);
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Sheet sheet = workbook.getSheetAt(0);
		Iterator<Row> rows = sheet.rowIterator();
		int count = 0;
		while (rows.hasNext()) {
			count++;
			Row row = rows.next();
			// 假设不是String类型统一设置为String
			if (row.getCell(2).getCellType() == 0
					|| row.getCell(2).getCellType() == 2
					|| row.getCell(2).getCellType() == 3
					|| row.getCell(2).getCellType() == 4) {
				row.getCell(2).setCellType(1);
			}
			System.out.println(row.getCell(2).getStringCellValue());
		}

	}

}


源码和測试文件:

 http://pan.baidu.com/s/1c0nIBiK

原文地址:https://www.cnblogs.com/yjbjingcha/p/6943243.html