java POI导入工具

maven依赖:

	<!-- Apache POI -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10-FINAL</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10-FINAL</version>
        </dependency>

工具类代码:

package com.cfcc.tsms.common.utils;

import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;


public class PoiUtils {
	static Workbook workbook;
	public static void loadExcel(String filepath) {
		FileInputStream inputStream = null;
		try {
			inputStream = new FileInputStream(new File(filepath));
				workbook =  WorkbookFactory.create(inputStream);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 获取cell内的值
	 * @param cell
	 * @return
	 */
	public static String getCellValue(Cell cell) {
		String cellvalue = "";
		DataFormatter formatter = new DataFormatter();
		if (cell != null) {
			switch (cell.getCellType()) {
			case Cell.CELL_TYPE_STRING:
				cellvalue = cell.getStringCellValue();
				break;
			case Cell.CELL_TYPE_NUMERIC:
				if (DateUtil.isCellDateFormatted(cell)) {//日期格式
					cellvalue = formatter.formatCellValue(cell);
				} else {//数值
					double value = cell.getNumericCellValue();
					int intvalue = (int) value;
					cellvalue = value - intvalue==0?String.valueOf(intvalue):String.valueOf(value);
				}
				break;
			case Cell.CELL_TYPE_FORMULA://单元格公式格式
				try {
					cellvalue = String.valueOf(cell.getNumericCellValue());
				} catch (IllegalStateException e) {
					cellvalue = String.valueOf(cell.getRichStringCellValue());
				}
				break;
			case Cell.CELL_TYPE_ERROR:
				cellvalue = "";
				break;
			case Cell.CELL_TYPE_BOOLEAN:
				cell.getBooleanCellValue();
				break;
			case Cell.CELL_TYPE_BLANK:
				cellvalue = "";
				break;

			default:
				cellvalue = cell.toString().trim();
				break;
			}
		}

		return cellvalue.trim();
	}
	
	/**
	 * 获取Sheet --> Row 值
	 * @param workbook
	 * @return
	 */
	public static Map<Integer, LinkedList[]> getRowData(Workbook workbook){
		 Map<Integer, LinkedList[]> mapList = new HashMap<Integer, LinkedList[]>(); // 用于保存对应的行数据对象 //key-->sheetNum ; value-->DataByRow
		for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
			LinkedList[] linkedLists = AnalysisBySheetNum(workbook, mapList, i);
		}
		return mapList;
	}
	/**
	 * 获取Sheet --> Row 值
	 * @param workbook
	 * @return
	 */
	public static Map<Integer, LinkedList[]> getRowData(Workbook workbook,int SheetNo){
		Map<Integer, LinkedList[]> mapList = new HashMap<Integer, LinkedList[]>(); // 用于保存对应的行数据对象 //key-->sheetNum ; value-->DataByRow
		LinkedList[] linkedLists = AnalysisBySheetNum(workbook, mapList, SheetNo);
		return mapList;
	}

	private static LinkedList[] AnalysisBySheetNum(Workbook workbook, Map<Integer, LinkedList[]> mapList, int i) {
		Sheet sheet = workbook.getSheetAt(i);
		int RowNum =  sheet.getLastRowNum()==0?sheet.getLastRowNum():sheet.getLastRowNum()+1;
		if (RowNum<1) {
			return null;
		}
		LinkedList[] linkedLists = new  LinkedList[RowNum];
		for (int j = 0; j < RowNum ; j++) {//行
			Row row = sheet.getRow(j);
			if (row!=null) {
				linkedLists[j] = new LinkedList();//每行数据创建对应的LinkedList
				for (int k = 0; k < row.getLastCellNum(); k++) {
					Cell cell = row.getCell(k);
					String value = getCellValue(cell);
					if ((k==0) &&StringUtils.isBlank(value)) {
						break;
					}
					linkedLists[j].add(value);
				}
			}
		}
		mapList.put(i, linkedLists);//key-->sheetNum ; value-->DataByRow
		return linkedLists;
	}
	
	/**
	 * 
	 * @param filepath
	 * Map<Integer, LinkedList[]> 解释: //key-->sheetNum ; value-->DataByRow
	 * @return
	 */
	public static Map<Integer, LinkedList[]> FileDataList(String filepath){
		loadExcel(filepath);
		return getRowData(workbook);
	}
	public static Map<Integer, LinkedList[]> FileDataListBySheetNo(String filepath,int SheetNo){
		loadExcel(filepath);
		return getRowData(workbook,SheetNo);
	}
	public static void main(String[] args) {
		Map<Integer, LinkedList[]>  map= FileDataList("d:\\财政专户.xls");
		for (int i = 0; i < map.size(); i++) {//sheet
			
			LinkedList[] LinkedList = map.get(i); 
			for (int j = 0; j < LinkedList.length; j++) {//sheet中行数据集合
				System.out.println();
				System.out.println("-------------第["+(j+1)+"]行-------------");
				java.util.LinkedList LinkedList2 =  LinkedList[j];
				for (int k = 0; k < LinkedList2.size(); k++) {//cell数据集合
					System.out.print(LinkedList2.get(k)+" ,");
				}
			}
		}
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独行客-编码爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值