这里需要用到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 unicode&str 转化

    从数据库中取出的值是Unicode编码的 需要转化为str才能正常使用 参考: http://www.mamicode.com/info-detail-308445.html

  2. 【转】app后端如何选择合适的数据库产品

    转自:http://blog.csdn.net/newjueqi/article/details/44003503 app后端的开发中,经常要面临的一个问题是:数据放在哪里? mysql ?redis ...

  3. 利用Xilinx中的ROM构造查找表来计算sin和cos的方法探讨

    1.使用matlab制作.coe文件 查找表的构造 构造256点的正余弦表 exp(-j*2*pi*(0:255)/256),分别得到 cos和sin的查找表 matlab代码: 求sin fid = ...

  4. Keil MDK与h-jtag联调

    keil MDK也是可以借助h-jtag进行单步调试,写出来与大家一起分享一下. keil MDK编译器使用V4.01版本,下载地址:http://www.embedinfo.com/down-lis ...

  5. dns智能解析对网站排名的影响

    网站排名是所有建站者都关系的问题,如何提升网站排名有很多因素,网站是否健康也与网站排名有关,下面智儒科技网站建设为你研究下如何判断自己的网站是否健康. 一般情况下,网站的排名在优化的基础上,怎么也上不 ...

  6. CH Round #51 - Shinrein祭 #1

    A.Phorni 题目:http://www.contesthunter.org/contest/CH%20Round%20%2351%20-%20Shinrein祭%20%231/Phorni 没做 ...

  7. 利用matlab给图像加高斯噪声

    I = imread('DSC_0034.JPG'); J = imnoise(I,'gaussian',0.20); figure, imshow(I), figure, imshow(J)

  8. 网络流(最大流) CQOI 2015 BZOJ 3931 网络吞吐量

    3931: [CQOI2015]网络吞吐量 Description 路由是指通过计算机网络把信息从源地址传输到目的地址的活 动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为路由器 ...

  9. hihoCoder 1391 Countries 【预处理+排序+堆】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

    #1391 : Countries 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There are two antagonistic countries, countr ...

  10. excel时会弹出向程序发送命令时出现问题的提示框

    出现此问题需要做两个操作来解决: 1.在开始所有程序中找到Microsoft Excel 2007的运行程序,右键选择属性,在兼容性标签将“以管理员身份运行此程序”的勾去掉. 2.在打开的Excel程 ...