poi 读取Excel(xls,xlsx)

		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>4.1.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-examples -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-examples</artifactId>
			<version>4.1.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-excelant -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-excelant</artifactId>
			<version>4.1.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>4.1.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml-schemas</artifactId>
			<version>4.1.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-scratchpad</artifactId>
			<version>4.1.0</version>
		</dependency>
package com.example.demo.util;

import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.ss.usermodel.WorkbookFactory;

/**
 * 
 * @Title: Excel操作
 */
public class OperateExcel {

	public static void main(String[] args) {
		List<String[]> value = readExcel(0, "D:\\1.xls");
		for (int i = 0; i < value.size(); i++) {
			System.out.println("第" + i + "行");
			for (String v : value.get(i)) {
				System.out.println(v);
			}
		}
		System.out.println("============================================");
		List<String[]> value_1 = readExcel(0, "D:\\2.xlsx");
		for (int i = 0; i < value_1.size(); i++) {
			System.out.println("第" + i + "行");
			for (String v : value_1.get(i)) {
				System.out.println(v);
			}
		}
	}

	/**
	 * 
	 * {读取 Excel}
	 * 
	 * @param read_line_num
	 *            读取起始索引
	 * @param path
	 *            文件路径
	 * @return
	 */
	public static List<String[]> readExcel(int read_line_num, String path) {
		try {
			File file = new File(path);
			// 判定文件是否存在
			if (!file.exists()) {
				return null;
			}
			// 返回集合
			List<String[]> result = new ArrayList<String[]>();
			FileInputStream fis = new FileInputStream(file);
			Workbook workBook = WorkbookFactory.create(fis);
			// 获取Sheet数 ==> workBook.getNumberOfSheets()
			// 获取第一个Sheet
			Sheet sheet = workBook.getSheetAt(0);
			// 记录列数,用于创建二维数组
			int column_num = 0;
			// 遍历行数据
			// start_row 起始行数
			// num_row 最后一行的索引(索引从0开始)
			for (int start_row = read_line_num, num_row = sheet.getLastRowNum(); start_row <= num_row; start_row++) {
				Row row = sheet.getRow(start_row);
				if (row == null) {
					continue;
				}
				// row.getLastCellNum() 最后一列的列数
				if (column_num < row.getLastCellNum()) {
					column_num = row.getLastCellNum();
				}
				// 遍历列
				String[] row_value = new String[column_num];
				for (int start_cell = 0, sum_cell = row.getLastCellNum(); start_cell < sum_cell; start_cell++) {
					Cell cell = row.getCell(start_cell);
					if (cell == null) {
						continue;
					}
					String cell_value = "";
					// 判断数据的类型
					switch (cell.getCellType()) {
					case NUMERIC: // 数字
						if (HSSFDateUtil.isCellDateFormatted(cell)) {
							Date date = cell.getDateCellValue();
							if (date != null) {
								cell_value = new SimpleDateFormat("yyyy-MM-dd").format(date);
							} else {
								cell_value = "";
							}
						} else {
							cell_value = new DecimalFormat("#").format(cell.getNumericCellValue());
						}
						break;
					case STRING: // 字符串
						cell_value = cell.getStringCellValue();
						break;
					case BOOLEAN: // Boolean
						cell_value = cell.getStringCellValue();
						break;
					case FORMULA: // 公式
						if (!cell.getStringCellValue().equals("")) {
							cell_value = cell.getStringCellValue();
						} else {
							cell_value = cell.getNumericCellValue() + "";
						}
						break;
					case BLANK: // 空值
						cell_value = "";
						break;
					case ERROR: // 故障
						cell_value = "";
						break;
					default:
						cell_value = "";
						break;
					}
					// 添加数据
					row_value[start_cell] = String.valueOf(cell_value).trim();
				}
				// 添加集合
				result.add(row_value);
			}
			System.out.println(3);
			// 关闭
			workBook.close();
			return result;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值