java实现读取excel文件内容

package excel;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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;

public class ReadExcel {
	/**
	 * 读取excel文件
	 * @param args
	 */
 
	public static void main(String[] args) {
		Workbook wb = null;  
		Sheet sheet = null;
		Row row = null;
 
		String filePath = "C:\\Users\\Pei\\Desktop\\2020公司培训课时汇总20201031.xlsx";
		System.out.println("filePath========"+filePath);
		wb = readExcel(filePath);
		if (wb != null) {
			try {
				List<List<List<Object>>> list = new ArrayList<>();
				
				System.err.println("页签数量:" + wb.getNumberOfSheets());
				// 循环页签
				for (int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
					// 指定页签的值
					sheet = wb.getSheetAt(sheetNum);
					// 定义存放一个页签中所有数据的List
					List<List<Object>> sheetList = new ArrayList<>();
				
					System.err.println("行总数:" + sheet.getLastRowNum());
					// 循环行
					for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
						// 指定行的值
						row = sheet.getRow(rowNum);
						//System.out.println("row================"+row);
						// 定义存放一行数据的List
						List<Object> rowList = new ArrayList<>();
						// 循环列
						System.err.println("列总数:" + row.getLastCellNum());
						for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) {
							Cell cell = sheet.getRow(rowNum).getCell(cellNum);
							rowList.add(getStringCellValue(cell));
						}
						sheetList.add(rowList);
					}
					list.add(sheetList);
				}
				System.err.println(list.toString());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	
	   //判断文件格式
    private static Workbook readExcel(String filePath){
        if(filePath==null){
            return null;
        }
        String extString = filePath.substring(filePath.lastIndexOf("."));
        
        try {
            //@SuppressWarnings("resource")
        	System.out.println("filePath====>>>>>"+filePath);
			InputStream is = new FileInputStream(filePath);
            if(".xls".equals(extString)){
                return new HSSFWorkbook(is);
            }else if(".xlsx".equals(extString)){
                return new XSSFWorkbook(is);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    
    
	//@SuppressWarnings("deprecation")
	public static String getStringCellValue(Cell cell) {
		String cellvalue = "";
		if (cell == null) {
			return "";
		}
		switch (cell.getCellType()) {
		case Cell.CELL_TYPE_STRING:
			cellvalue = cell.getStringCellValue();
			break;
		case Cell.CELL_TYPE_NUMERIC:
			if (DateUtil.isCellDateFormatted(cell)) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date = cell.getDateCellValue();
				cellvalue = sdf.format(date);
			} else {
				cellvalue = String.valueOf(cell.getNumericCellValue());
			}
			break;
		case Cell.CELL_TYPE_BOOLEAN:
			cellvalue = String.valueOf(cell.getBooleanCellValue());
			break;
		case Cell.CELL_TYPE_BLANK:
			cellvalue = "";
			break;
		default:
			cellvalue = "";
			break;
		}
		if (cellvalue == "") {
			return "";
		}
		return cellvalue;
	}
	
}

  pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cn</groupId>
  <artifactId>readExcel</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
     <dependency>
	    <groupId>org.apache.directory.studio</groupId>
	    <artifactId>org.apache.commons.codec</artifactId>
	    <version>1.8</version>
	</dependency>
	 <dependency>
		<groupId>net.sourceforge.jexcelapi</groupId>
		<artifactId>jxl</artifactId>
		<version>2.6.12</version>
	</dependency>
	<dependency>
		<groupId>org.apache.poi</groupId>
		<artifactId>poi-ooxml</artifactId>
		<version>3.9</version>
	</dependency>
<dependency>
   <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.54</version>
</dependency>
  </dependencies>
</project>

  

posted @ 2020-11-10 10:52  红尘沙漏  阅读(5125)  评论(0编辑  收藏  举报