这里需要用到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. python 图片压缩存储

    python(PIL)图像处理(等比例压缩.裁剪压缩) 缩略(水印)图 http://outofmemory.cn/code-snippet/12264/python-PIL-image-proces ...

  2. python读写Excel文件的函数--使用xlrd/xlwt

    python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket  The ...

  3. CentOS 6.3 配置FTP

    一.FTP的安装 .检测是否安装了FTP:[root@localhost ~]# rpm -q vsftpd 如果安装了会显示版本信息: [root@localhost ~]# vsftpd-2.0. ...

  4. 如何调整iMindMap打印设置

    打印何尝不是一种保存.导出iMindMap思维导图的一种方法,我们还可以通过调整打印设置来满足我们不同的需求.下面小编就给你翻一翻iMindMap中文版教程,教你怎样调整打印设置. 我们在打开iMin ...

  5. codeforces D.Mashmokh and ACM

    题意:给你n和k,然后找出b1, b2, ..., bl(1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n),并且对所有的bi+1%bi==0,问有多少这样的序列? 思路:dp[i][j] 表示长 ...

  6. JAVA内置的观察者模式样本

    DisplayElement.java public interface DisplayElement { public void display(); } CurrentConditionsDisp ...

  7. 马云专访二:点评阿里雅虎交易、BAT三家、互联网巨头与政府关系

    记者:我们不得不要说到你和雅虎之间的事情了.你知道,雅虎对整个互联网业的意义不只是一家公司,它有它象征的意义,重要的是,雅虎对阿里巴巴的意义更加非同寻常,当你最后决定用76亿美元从雅虎“赎身”的时候, ...

  8. win平台下, 检测网络是否连接最好的办法

    [Delphi]检查URL是否有效的函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 function CheckUr ...

  9. Linux Kernel ‘skbuff.c’本地拒绝服务漏洞

    漏洞名称: Linux Kernel ‘skbuff.c’本地拒绝服务漏洞 CNNVD编号: CNNVD-201307-498 发布时间: 2013-07-24 更新时间: 2013-07-24 危害 ...

  10. 使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩

    使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZ ...