POI读取Excel

1,新建maven 项目


2,在pom.xml文件中添加jar包

        <dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.10-FINAL</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.10-FINAL</version>
		</dependency>

3,demo

package test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelToXMLUtil {

	public static void main(String[] args) {

		String path = "C:\\Users\\uld\\Desktop\\tmp001.xls";

		try {
			List<List<String>> result = new ReadExcelToXMLUtil().readXls(path);
			System.out.println("<lines>");
			for (int i = 1; i < result.size(); i++) {
				System.out.println("<line>");
				List<String> modeltitle = result.get(0);
				List<String> model = result.get(i);
				for (int k = 0; k < model.size(); k++) {
					System.out.println("<" + modeltitle.get(k) + ">" + model.get(k) + "</" + modeltitle.get(k) + ">");
				}
				System.out.println("</line>");
			}
			System.out.println("</lines>");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private List<List<String>> readXls(String path) throws Exception {
		InputStream is = new FileInputStream(path);
		// HSSFWorkbook 标识整个excel
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
		List<List<String>> result = new ArrayList<List<String>>();
		int size = hssfWorkbook.getNumberOfSheets();
		// 循环每一页,并处理当前循环页
		for (int numSheet = 0; numSheet < size; numSheet++) {
			// HSSFSheet 标识某一页
			HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
			if (hssfSheet == null) {
				continue;
			}
			// 处理当前页,循环读取每一行
			for (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
				// HSSFRow表示行
				HSSFRow hssfRow = hssfSheet.getRow(rowNum);
				int minColIx = hssfRow.getFirstCellNum();
				int maxColIx = hssfRow.getLastCellNum();
				List<String> rowList = new ArrayList<String>();
				// 遍历改行,获取处理每个cell元素
				for (int colIx = minColIx; colIx < maxColIx; colIx++) {
					// HSSFCell 表示单元格
					HSSFCell cell = hssfRow.getCell(colIx);
					if (cell == null) {
						continue;
					}
					rowList.add(getStringVal(cell).trim());
				}
				result.add(rowList);
			}
		}
		return result;
	}

	private List<List<String>> readXlsx(String path) throws Exception {
		InputStream is = new FileInputStream(path);
		XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
		List<List<String>> result = new ArrayList<List<String>>();
		// 循环每一页,并处理当前循环页
		for (XSSFSheet xssfSheet : xssfWorkbook) {
			if (xssfSheet == null) {
				continue;
			}
			// 处理当前页,循环读取每一行
			for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
				XSSFRow xssfRow = xssfSheet.getRow(rowNum);
				int minColIx = xssfRow.getFirstCellNum();
				int maxColIx = xssfRow.getLastCellNum();
				List<String> rowList = new ArrayList<String>();
				for (int colIx = minColIx; colIx < maxColIx; colIx++) {
					XSSFCell cell = xssfRow.getCell(colIx);
					if (cell == null) {
						continue;
					}
					rowList.add(cell.toString().trim());
				}
				result.add(rowList);
			}
		}
		return result;
	}

	public static String getStringVal(HSSFCell cell) {
		switch (cell.getCellType()) {
		case Cell.CELL_TYPE_BOOLEAN:
			return cell.getBooleanCellValue() ? "TRUE" : "FALSE";
		case Cell.CELL_TYPE_FORMULA:
			return cell.getCellFormula();
		case Cell.CELL_TYPE_NUMERIC:
			cell.setCellType(Cell.CELL_TYPE_STRING);
			return cell.getStringCellValue();
		case Cell.CELL_TYPE_STRING:
			return cell.getStringCellValue();
		default:
			return "";
		}
	}

}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值