这里需要用到poi.jar和poi-ooxml.jar  没有的可以去http://mvnrepository.com/下载

 import org.apache.poi.POIXMLDocument;
 import org.apache.poi.hssf.usermodel.HSSFDateUtil;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 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.xssf.usermodel.XSSFWorkbook;

 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PushbackInputStream;
 import java.text.SimpleDateFormat;
 import java.util.*;

 /**
  * Created by Donge on 2017/1/3.
  */
 public class ReadExcel {

     static private Workbook wb;
     static private Sheet sheet;
     static private Row row;

     /**
      * 读取 Excel 标题
      * @param fileName
      * @return
      */
     public static String[] readExcelTitle(String fileName) {
         try {
             wb = createWorkbook(new FileInputStream(fileName));
         } catch (IOException e) {
             e.printStackTrace();
         } catch (InvalidFormatException e) {
             e.printStackTrace();
         }
         sheet = wb.getSheetAt(0);
         row = sheet.getRow(0);// 获取第一行(约定第一行是标题行)
         int colNum = row.getLastCellNum();// 获取行的列数
         String[] titles = new String[colNum];
         for (int i = 0; i < titles.length; i++) {
             titles[i] = getCellFormatValue(row.getCell(i));
         }
         return titles;
     }

     /**
      * 读取 Excel 内容
      * @param fileName
      * @return
      */
     public static List<Map<String, String>> readExcelContent(String fileName) {
         List<Map<String, String>> list = new ArrayList<>();
         Map<String, String> content;
         try {
             wb = createWorkbook(new FileInputStream(fileName));
         } catch (IOException e) {
             e.printStackTrace();
         } catch (InvalidFormatException e) {
             e.printStackTrace();
         }
         sheet = wb.getSheetAt(0);
         int rowNum = sheet.getLastRowNum()+1;// 得到总行数
         row = sheet.getRow(0);
         int colNum = row.getLastCellNum();// 得到总列数
         String titles[] = readExcelTitle(fileName);
         // 正文内容应该从第二行开始,第一行为表头的标题
         for (int i = 1; i < rowNum; i++) {
             int j = 0;
             row = sheet.getRow(i);
             content = new LinkedHashMap<>();
             do {
                 content.put(titles[j], getCellFormatValue(row.getCell(j)).trim());
                 j++;
             } while (j < colNum);
             list.add(content);
         }
         return list;
     }

     /**
      * 根据Cell类型设置数据
      * @param cell
      * @return
      */
     private static String getCellFormatValue(Cell cell) {
         String cellValue = " ";
         if (cell != null) {
             // 判断当前Cell的Type
             switch (cell.getCellType()) {
                 // 如果当前Cell的Type为NUMERIC
                 case Cell.CELL_TYPE_NUMERIC:
                 case Cell.CELL_TYPE_FORMULA: {
                     // 判断当前的cell是否为Date
                     if (HSSFDateUtil.isCellDateFormatted(cell)) {
                         Date date = cell.getDateCellValue();
                         cellValue = new SimpleDateFormat("yyyy-MM-dd").format(date);// 时间格式化显示:2012-12-31
                     } else {
                         // 如果是纯数字取得当前Cell的数值
                         cellValue = String.valueOf(cell.getNumericCellValue());
                     }
                     break;
                 }
                 // 如果当前Cell的Type为STRIN
                 case Cell.CELL_TYPE_STRING:
                     cellValue = cell.getRichStringCellValue().getString();
                     break;
                 default:
                     // 默认的Cell值
                     cellValue = " ";
             }
         }
         return cellValue;

     }

     /**
      * 创建 Workbook
      * @param is
      * @return
      * @throws IOException
      * @throws InvalidFormatException
      */
     public static Workbook createWorkbook(InputStream is) throws IOException,InvalidFormatException {
         if (!is.markSupported()) {
             is = new PushbackInputStream(is, 8);
         }
         if (POIFSFileSystem.hasPOIFSHeader(is)) {
             return new HSSFWorkbook(is);
         }
         if (POIXMLDocument.hasOOXMLHeader(is)) {
             return new XSSFWorkbook(OPCPackage.open(is));
         }
         throw new IllegalArgumentException("POI解析不了您当前的Excel版本");
     }

     /**
      * 测试
      * @param args
      */
     public static void main(String args[]) {
         String filePath = "D:\\Test.xls";
         List<Map<String, String>> list = readExcelContent(filePath);
         Map<String, String> map;
         for (int i = 0; i < list.size(); i++) {
             map = list.get(i);
             System.out.println("**************THE START OF ROW("+(i+1)+")**************");
             for (String key : map.keySet()) {
                 System.out.println(key + " : " + map.get(key));
             }
         }
     }
 }

Apache POI解析excel文件的更多相关文章

  1. 使用apache POI解析Excel文件

    1. Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. 2. POI结构 ...

  2. 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据

    1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...

  3. poi解析Excel文件版本问题

    poi解析Excel文件时有两种格式: HSSFWorkbook格式用来解析Excel2003(xls)的文件 XSSFWorkbook格式用来解析Excel2007(xlsx)的文件 如果用HSSF ...

  4. Jquery的一键上传组件OCUpload及POI解析Excel文件

    第一步:将js文件引入页面 <script type="text/javascript" src="${pageContext.request.contextPat ...

  5. java使用jxl,poi解析excel文件

    public interface JavaExcel { /** * 使用jxl写excel文件 */ public void writeJxlExcel(); /** * 使用jxl读excel文件 ...

  6. 关于POI解析Excel文件(03和07版本不同)的问题

    问题描述:在使用poi包进行excel解析时,发现对Excel2003以前(包括2003)的版本没有问题,但读取Excel2007时发生如下异常:org.apache.poi.poifs.filesy ...

  7. 如何用Apache POI操作Excel文件-----如何对一个单元格加注解?

    有的时候,我们需要通过操作Apache POI,在生成Cell数据的同时,能对其生成的Cell,加上注解(comments),类似于下面的. 那么对于这种情况,我们的代码应该如何写呢? 借花献佛,我就 ...

  8. 如何用Apache POI操作Excel文件-----如何在已有的Excel文件中插入一行新的数据?

    在POI的第一节入门中,我们提供了两个简单的例子,一个是如何用Apache POI新建一个工作薄,另外一个例子是,如果用Apache POI新建一个工作表.那么在这个章节里面,我将会给大家演示一下,如 ...

  9. 如何用Apache POI操作Excel文件-----如何用Apache POI 画一个离散图

    有的时候,我们需要Excel中的数据,通过一个图画,可视化的表现出来. 那么这个时候,应该如何做呢?现在就借花献佛,以Apache POI自己提供的一个例子为例,给大家演示一下POI的API 如何画图 ...

随机推荐

  1. [Android1.5]TextView跑马灯效果

    from: http://www.cnblogs.com/over140/archive/2010/08/20/1804770.html 前言 这个效果在两周前搜索过,网上倒是有转载,可恨的是转载之后 ...

  2. linux常用命令(4)rm命令

    rm是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比如在/(根目录)下执行rm * -rf).所以,我们在执行rm之前最好先确认一下在哪个目录,到底要删除什么东西 ...

  3. ios+oc面试题

    ios+oc面试题     浅复制和深复制的区别?//浅拷贝和深拷贝答案:浅层复制(copy):只复制指向对象的指针,而不复制引用对象本身.//通过对象的指针来访问这个对象深层复制(mutableCo ...

  4. Adobe Photoshop CS或者CC卸载不了怎么办?

    有木有没有遇到这个问题的同学?使用Adobe Creative Suite CleanerToo工具下载就好了~ 下载地址:http://pan.baidu.com/s/1pJ3aBsn

  5. Linux SendMail 使用外部SMTP服务发送邮件

    这个今天刚好用到,就测试了一下.OK了..因为,PYTHON模块是可以,但SHELL脚本用SHELL发,还是合拍点.. http://my.oschina.net/duangr/blog/183162 ...

  6. 工程师必知ZigBee技术问答精华汇总

    本文是关于ZigBee技术的一些基础知识.行业应用方面的精华汇总.希望通过本文的分析,能让大家对ZigBee技术的起源.发展.特点.前景及其在通信网络中的相关应用有全面直观的了解. 1.基础知识篇 Q ...

  7. centos 6.5 安装weixin

    下载cpanm wget http://xrl.us/cpanm --no-check-certificate -O /sbin/cpanm && chmod +x /sbin/cpa ...

  8. C# Linq-Aggregate

    The easiest to understand definition of Aggregate is that it performs an operation on each element o ...

  9. Balance(01背包)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9163   Accepted: 5617 Description Gigel ...

  10. POJ2442 Sequence

    题目链接. #include <iostream> #include <cstdio> #include <cstring> #include <cstdli ...