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>

  

java实现读取excel文件内容的更多相关文章

  1. 使用POI读取excel文件内容

    1.前言 项目中要求读取excel文件内容,并将其转化为xml格式.常见读取excel文档一般使用POI和JExcelAPI这两个工具.这里我们介绍使用POI实现读取excel文档. 2.代码实例: ...

  2. 五种方式让你在java中读取properties文件内容不再是难题

    一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...

  3. PHP读取Excel文件内容

    PHP读取Excel文件内容   项目需要读取Excel的内容,从百度搜索了下,主要有两个选择,第一个是PHPExcelReader,另外一个是PHPExcel.   PHPExcelReader比较 ...

  4. 关于jquery js读取excel文件内容 xls xlsx格式 纯前端

    附带参考:http://blog.csdn.net/gongzhongnian/article/details/76438555 更详细导入导出:https://www.jianshu.com/p/7 ...

  5. Java 读取Excel 文件内容

    在一个项目中,有一个需求,是把excel文件的内容转换为xml格式展示.在学习如何操作的过程中,首先是如何获取excel文件,其中操作的代码如下: 1.首先是导入需要的 jar, 下载地址:https ...

  6. java读取excel文件内容

    1.导入依赖JAR包 <!-- jxl 操作excel --> <dependency> <groupId>org.jxls</groupId> < ...

  7. Java——poi读取Excel文件

    1.创建文件流,打开EXCEL文件 FileInputStream excelFile = new FileInputStream(excelPath); XSSFWorkbook workbook ...

  8. 读取excel文件内容代码

    最近工作需要批量添加映射excel文件字段的代码  于是通过读取excel2007实现了批量生成代码,记录下代码 需要引入poi的jar包 import java.awt.List; import j ...

  9. java Api 读取HDFS文件内容

    package dao; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java ...

随机推荐

  1. Appium上下文和H5测试(二)

    坚持原创输出,点击蓝字关注我吧 作者:清菡 博客:oschina.云+社区.知乎等各大平台都有. 文章总览图 一.往期回顾 loc='new UiSelector().text("全程班&q ...

  2. CSRF&SSRF-初探准备

    了解CSRF之前的必备知识 1.同源策略 同源策略-三个相同:协议.域名.端口 举例说明: 源URL为:http://www.example.com/dir/page.html 协议为:http 域名 ...

  3. 记一次容器CPU高占用问题排查

    起因:发现docker中有两个容器的CPU持续在百分之95以上运行了一晚上 执行命令:docker stats 发现这个两个大兄弟一点没歇满负荷跑了一晚上,再这么下去怕不是要GG 容器里跑的是JAVA ...

  4. Go语言基础--1.1 变量的声明

    1.标准格式: var name type     (var 关键字 name 变量名 type 类型)   命名规则:建议使用驼峰命名法 例如:var userName string    var ...

  5. 第3.10节 Python强大的字符串格式化新功能:使用format字符串格式化

    一.    引言 前面两节介绍的字符串格式化方法,都有其本身对应的缺陷,老猿不建议大家使用,之所以详细介绍主要是考虑历史代码的兼容性,方便大家理解前人留下的代码.老猿推荐大家新编码时使用format方 ...

  6. 第10.10节 Python使用__init__.py自动加载包下内容

    在前面章节老猿介绍了包下模块及子包的加载的各种方式,并说明包的加载首先是自动加载包下的__init__.py文件.在<第10.6节 Python包的概念>中介绍了__init__.py文件 ...

  7. STL-Vector容量问题:

    1.clear,erase ,pop_back() 函数只删除对象,并没有释放vec中的内存,若对象是指针还需要delete:2.在erase,clear,pop_back()删除对象的后,size改 ...

  8. APIO2012 苦无 Kunai

    这题网上貌似还没有完整的题解呢,我来口胡一下~ Description \(W \times H\) 的二维坐标系,\(W, H \le 10^9\) 给 \(n (n \le 10^5)\) 个点 ...

  9. CF1400F - x-prime Substrings

    1400F - x-prime Substrings 首先发现 \(x\) 很小,所以发现对应的 x-prime 字符串数也很少,最多的情况是 \(x = 19\),有 2399 个,先爆搜出来. 现 ...

  10. Java并发编程的艺术(二)——volatile、原子性

    什么是volatile Java语言允许线程访问共享变量,为了确保共享变量能够被准确一致地更新,如果一个字段被声明为volatile,那么Java内存模型将会确保所有线程看到这个变量时值是一致的.保证 ...