本ExcelUtils工具类是用poi写的,仅用于线下从excel文件中读取数据。如果生产环境要用的话,建议切换到阿里的easyexcel。

引入poi.jar:

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

ExcelUtils:

import java.io.FileInputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
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 ExcelUtils {
    public static void readExcel(String sourceFilePath) {
        Workbook workbook = null;
        StringBuilder sb = new StringBuilder();
        try {
            workbook = getReadWorkBookType(sourceFilePath);
            //获取第一个sheet
            Sheet sheet = workbook.getSheetAt(0);
            //第一行是表头,从第二行开始读取
            for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                Row row = sheet.getRow(rowNum);
                Cell cell = row.getCell(0);
                sb.append(getCellStringVal(cell).trim());
            }
        } finally {
            IOUtils.closeQuietly(workbook);
        }
        System.out.println(sb.toString());
    }

    private static String getCellStringVal(Cell cell) {
        CellType cellType = cell.getCellTypeEnum();
        switch (cellType) {
            case NUMERIC:
                return cell.getStringCellValue();
            case STRING:
                return cell.getStringCellValue();
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            case BLANK:
                return "";
            case ERROR:
                return String.valueOf(cell.getErrorCellValue());
            default:
                return StringUtils.EMPTY;
        }
    }

    private static Workbook getReadWorkBookType(String filePath) {
        FileInputStream is = null;
        try {
            is = new FileInputStream(filePath);
            if (filePath.toLowerCase().endsWith("xlsx")) {
                return new XSSFWorkbook(is);
            } else {
                return new HSSFWorkbook(is);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }
        return null;
    }

    public static void main(String[] args) {
        readExcel("/Users/koushengrui/Desktop/ksPhotoId.xlsx");
    }
}

ExcelUtils的更多相关文章

  1. ExcelUtils 导表实例

    @RequestMapping("/dealer/chargebook/exportv.htm")    public void getChargeBookList(int epm ...

  2. 一个操作EXCEL的C#类ExcelUtils

    近期在公司里一直从事服务类的工作,涉及到非常多excel的处理.部分工作内容是每天反复的,仅仅是每天的数据不同而已.我遇到的一个问题是客户每天发送的几种数据有些excel中的字段顺序是一致的,有些是不 ...

  3. J2EE项目开发中常用到的公共方法

    在项目IDCM中涉及到多种工单,包括有:服务器|网络设备上下架工单.服务器|网络设备重启工单.服务器光纤网线更换工单.网络设备撤线布线工单.服务器|网络设备替换工单.服务器|网络设备RMA工单.通用原 ...

  4. 基于Spring Mvc实现的Excel文件上传下载

    最近工作遇到一个需求,需要下载excel模板,编辑后上传解析存储到数据库.因此为了更好的理解公司框架,我就自己先用spring mvc实现了一个样例. 基础框架 之前曾经介绍过一个最简单的spring ...

  5. excel解析二维数组结构的excel

    public void fileImport(Ufile ufile) throws Exception { String filePath = ufile.getFilePath(); List&l ...

  6. poi管道流的导入导出

    /** * 导入学生信息 * * @param classid * @param uploadFilePath * @return */ public boolean uploadStudentFil ...

  7. Java操作Excel(读、写、搜索关键字、插入图片)

    import java.io.File; import java.io.IOException; import jxl.Cell; import jxl.Sheet; import jxl.Workb ...

  8. 用poi框架进行批量导入导出实例

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能.我们这里使用poi对数据库中的数据进行批量导出,以及 ...

  9. 利用java读写Excel文件

    一.读取Excel文件内容 java 代码 public static String readExcel(File file){ StringBuffer sb = new StringBuffer( ...

随机推荐

  1. Angular5 *ngIf 和 hidden 的区别

    问题 项目中遇到一个问题,有一个过滤查询的面板,需要通过一个展开折叠的button,来控制它的show 和 hide.这个面板中,有一个Select 组件,一个 input 查询输入框. 原来代码是: ...

  2. poj3761(反序表)

    题目链接:https://vjudge.net/problem/POJ-3761 题意:给出n和k,求通过k趟冒泡排序得到长为n的有序排列(元素为n个不同的数)的原排列有多少个. 思路: 先给出反序表 ...

  3. [转帖]Ubuntu 对应内核版本

    带有相应Linux内核版本的Ubuntu版本列表 https://www.helplib.com/ubuntu/article_155943   问题: 是否有带有默认对应的Linux内核版本的Ubu ...

  4. CodeForces-431D Random Task

    题目描述 求一个\(n\),使得\(n+1\)到\(2n\)这些数的二进制中恰好有\(k\)个\(1\)的数有\(m\)个. Input 输入包含两个正整数\(m,k\).$(0<=m<= ...

  5. AT2294 Eternal Average

    题目 题目给我们的这个东西可以转化为一棵\(k\)叉树,有\(n+m\)个叶子节点,其中\(m\)个权值为\(1\),\(n\)个权值为\(0\),每个非叶子节点的权值为其儿子的平均值,现在问你根节点 ...

  6. div距离左边设置

    margin-right:不加负号, margin-left:必须加负号,理解为倒数 margin-left:-10px;

  7. vue video.js使用

    插件github地址:https://github.com/videojs/video.js 这边做一个切换视频vue url的解决记录 this.$nextTick(() => { let u ...

  8. RGB颜色值转换成十六进制

    function transferRgbToStr(color) { if (typeof color !== 'string' && !(color instanceof Strin ...

  9. FMDB-FMDatabaseQueue

    FMDB封装了SQLite3的方法,操作数据库变得很简单. 增删改查变简单之后,那么问题来了,如何使用多线程优化对数据库的操作? 这是我们的第一反应估计是dispatch_async(). 那么问题又 ...

  10. Core Graphics Paths

    Paths中的几个重要元素 Points void CGContextMoveToPoint (    CGContextRef c,    CGFloat x,    CGFloat y ); 指定 ...