java的poi 读取exc 文件
package lizikj.bigwheel.shop.util.excel; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List; import lizikj.bigwheel.common.vo.agent.Agent; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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; public class ReadExcelUtils {
/** 总行数 */
private int totalRows = 0;
/** 总列数 */
private int totalCells = 0;
/** 错误信息 */
private String errorInfo; /**
* 验证excel文件
*
* @param filePath
* @return
*/
public boolean validateExcel(String filePath) {
/** 检查文件名是否为空或者是否是Excel格式的文件 */
if (filePath == null || !(WDWUtil.isExcel2003(filePath) || WDWUtil.isExcel2007(filePath))) {
errorInfo = "文件名不是excel格式";
return false;
} /** 检查文件是否存在 */
File file = new File(filePath);
if (file == null || !file.exists()) {
errorInfo = "文件不存在";
return false;
}
return true; } /**
* 根据文件名读取excel文件
*
* @param filePath
* @return
*/
public List<Agent> read(String filePath) { List<Agent> dataLst = new ArrayList<Agent>(); InputStream is = null; try {
/** 验证文件是否合法 */
if (!validateExcel(filePath)) {
System.out.println(errorInfo);
return null;
} /** 判断文件的类型,是2003还是2007 */
boolean isExcel2003 = true;
if (WDWUtil.isExcel2007(filePath)) {
isExcel2003 = false;
} /** 调用本类提供的根据流读取的方法 */ File file = new File(filePath); is = new FileInputStream(file); dataLst = read(is, isExcel2003); is.close(); } catch (Exception ex) {
ex.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
is = null;
e.printStackTrace();
}
}
}
/** 返回最后读取的结果 */
return dataLst;
} /**
* 根据流读取Excel文件
*
* @param inputStream
* @param isExcel2003
* @return
*/
public List<Agent> read(InputStream inputStream, boolean isExcel2003) {
List<Agent> dataLst = null;
try { /** 根据版本选择创建Workbook的方式 */
Workbook wb = null;
if (isExcel2003) {
wb = new HSSFWorkbook(inputStream);
} else {
wb = new XSSFWorkbook(inputStream);
}
dataLst = read(wb);
} catch (IOException e) {
e.printStackTrace();
}
return dataLst;
} /**
* 读取数据
*
* @param wb
* @return
*/
private List<Agent> read(Workbook wb) {
List<Agent> dataLst = new ArrayList<Agent>(); /** 得到第一个shell */
Sheet sheet = wb.getSheetAt(0);
/** 得到Excel的行数 */
this.totalRows = sheet.getPhysicalNumberOfRows();
/** 得到Excel的列数 */
if (this.totalRows >= 1 && sheet.getRow(0) != null) {
this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
}
Agent agent = null;
/** 循环Excel的行 */
for (int r = 1; r < this.totalRows; r++) {
Row hssfRow = sheet.getRow(r);
if (hssfRow == null) {
continue;
}
agent = new Agent();
Cell name = hssfRow.getCell(0);
Cell pname = hssfRow.getCell(1);
Cell cname = hssfRow.getCell(2);
Cell rname = hssfRow.getCell(3);
Cell address = hssfRow.getCell(4);
Cell contact = hssfRow.getCell(5);
Cell mobile = hssfRow.getCell(6);
Cell companyName = hssfRow.getCell(7);
agent.setParentAgentId(0);
agent.setAgentName(getValue(name));
agent.setAgentRole("K1");
agent.setProvinceId(getValue(pname));
agent.setCityId(getValue(cname));
agent.setRegionId(getValue(rname));
agent.setContact(getValue(contact));
agent.setAddress(getValue(address));
agent.setMobile(getValue(mobile));
agent.setContactName(getValue(contact));
agent.setCompanyName(getValue(companyName));
dataLst.add(agent);
} return dataLst; } @SuppressWarnings("static-access")
private String getValue(Cell hssfCell) { if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
String result = ""; if (HSSFDateUtil.isCellDateFormatted(hssfCell)) {// 处理日期格式、时间格式
SimpleDateFormat sdf = null;
if (hssfCell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
}
Date date = hssfCell.getDateCellValue();
result = sdf.format(date);
} else if (hssfCell.getCellStyle().getDataFormat() == 58) {
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = hssfCell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
result = sdf.format(date);
} else {
double value = hssfCell.getNumericCellValue();
CellStyle style = hssfCell.getCellStyle();
DecimalFormat format = new DecimalFormat();
String temp = style.getDataFormatString();
// 单元格设置成常规
if (temp.equals("General")) {
format.applyPattern("#");
}
result = format.format(value);
}
return result;
} else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue());
} } class WDWUtil { /**
* 是否是2003的excel,返回true是2003
* @param filePath
* @return
*/
public static boolean isExcel2003(String filePath) {
return filePath.matches("^.+\\.(?i)(xls)$");
} /**
* 是否是2007的excel,返回true是2007
* @param filePath
* @return
*/
public static boolean isExcel2007(String filePath) {
return filePath.matches("^.+\\.(?i)(xlsx)$");
} }
来自:http://blog.csdn.net/maxu12345/article/details/47977811
http://blog.csdn.net/mmm333zzz/article/details/7962377
http://www.cnblogs.com/hongten/p/java_poi_excel.html
java的poi 读取exc 文件的更多相关文章
- java使用poi读取ppt文件和poi读取excel、word示例
java使用poi读取ppt文件和poi读取excel.word示例 http://www.jb51.net/article/48092.htm
- JAVA使用POI读取EXCEL文件的简单model
一.JAVA使用POI读取EXCEL文件的简单model 1.所需要的jar commons-codec-1.10.jarcommons-logging-1.2.jarjunit-4.12.jarlo ...
- Java实现POI读取Excel文件,兼容后缀名xls和xlsx
1.引入所需的jar包: maven管理项目的话直接添加以下坐标即可: <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -- ...
- java使用poi读取ppt文件
package msoffice; import java.io.File; import java.io.FileInputStream; import java.io.IOException; i ...
- java使用POI实现excel文件的读取,兼容后缀名xls和xlsx
需要用的jar包如下: 如果是maven管理的项目,添加依赖如下: <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --&g ...
- java使用poi读取doc和docx文件(maven自动导入依赖包)
java使用poi读取doc和docx文件(maven自动导入依赖包) 于是在网上搜寻了一阵之后才发现原来doc文档和excel一样不能用普通的io流的方法来读取,而是也需要用poi,于是进行了一番尝 ...
- Java使用POI读取和写入Excel指南
Java使用POI读取和写入Excel指南 做项目时经常有通过程序读取Excel数据,或是创建新的Excel并写入数据的需求: 网上很多经验教程里使用的POI版本都比较老了,一些API在新版里已经废弃 ...
- Java使用POI操作Excel文件
1.简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式文件读和写的功能. 2.依赖的jar包 <!-- ex ...
- 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据
1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...
随机推荐
- 涂抹mysql笔记-搭建mysql高可用体系
mysql的高可用体系<>追求更高稳定性的服务体系 可扩展性:横向扩展(增加节点).纵向扩展(增加节点的硬件配置) 高可用性<>Slave+LVS+Keepalived实现高可 ...
- 使用Oracle DBLink进行数据库之间对象的访问操作
Oracle中自带了DBLink功能,它的作用是将多个oracle数据库逻辑上看成一个数据库,也就是说在一个数据库中可以操作另一个数据库中的对象,例如我们新建了一个数据database1,我们需要操作 ...
- node.js打印function
var Person = function(name) { this.name = name; this.gender = ['man', 'woman']; } console.log(Person ...
- redis-单线程架构
单线程模型: redis中的数据结构并不全是简单的kv,还有list.hash等复杂的结构,这些结构很可能会进行细粒度的操作,比如在很长的列表偶棉添加一个元素,在hash当中或者删除一个对象,这样的一 ...
- VS2015密匙--VS2015打开丢失msvcp140.dll--cannot find one or more components ,please reinstall the application
win7旗舰版 64位 + vs2015 专业版 1.安装VS2015过程中可能需要用到的VS2015专业版钥匙:(测试,可用) HMGNV-WCYXV-X7G9W-YCX63-B98R2 2.VS2 ...
- JAVA 没有重载运算符,那么 String 类型的加法是怎么实现的,以及String类型不可变的原因和好处
1, JAVA 不具备 C++ 和 C# 一样的重载运算符 来实现类与类之间相互计算 的功能 这其实一定程度上让编程失去了代码的灵活性, 但是个人认为,这在一定程度上减少了代码异常的概率 ...
- @ResponseBody 与 response.getWriter.write
@responseBody注解的使用 1. @responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通 ...
- get_time
def get_current_time(): #将python的datetime转换为unix时间戳 dtime = datetime.datetime.now() un_time = time.m ...
- myeclise 安装
安装.破解步骤都在gaobo百度云/工具/开发工具 安装后配置环境变量:
- vue获取后台图片验证码,并点击刷新验证码
<--url为需要访问的接口地址--> <span style="display: inline-block;width: 130px;height: 53px;borde ...