Java 实现后缀xls文件读取
Java 实现后缀xls文件读取
一、开发环境
poi采用的3.9版本 + JDK1.6 + Myeclipse
二,JAR包

三、实现代码
实体类:UserRoleBean
package nc.xyzq.uuib.bean; /**
* ClassName:UserRoleBean
* @author lizm
* @since JDK 1.6
*
*/
public class UserRoleBean { private String userCode ;
private String roleid;
private String isflag; public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getRoleid() {
return roleid;
}
public void setRoleid(String roleid) {
this.roleid = roleid;
}
public String getIsflag() {
return isflag;
}
public void setIsflag(String isflag) {
this.isflag = isflag;
} }
实现读取xls信息类:Client
package nc.xyzq.client; import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger; import nc.xyzq.pub.Pub;
import nc.xyzq.uuib.service.impl.UserRoleBean;
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; /**
* ClassName:Client
* Function: TODO
* Reason: TODO
* Date: 2018年11月20日 下午15:59:31
* @author lizm
* @since JDK 1.6
*
*/
public class Client { private static final Logger logger = (Logger) Logger.getLogger(Client.class.getName()); private List<UserRoleBean> getInfoFromExcle(){
List<UserRoleBean> list = new ArrayList();
//excel文件路径
String excelPath = Pub.getPropertiesValue("relationship", "path");//读取配置文件,例如 D://data/relationship.xls
try {
//String encoding = "GBK";
File excel = new File(excelPath);
if (excel.isFile() && excel.exists()) { //判断文件是否存在
String[] split = excel.getName().split("\\."); //.是特殊字符,需要转义!!!!!
Workbook wb;
//根据文件后缀(xls/xlsx)进行判断
if ( "xls".equals(split[1])){
FileInputStream fis = new FileInputStream(excel); //文件流对象
wb = new HSSFWorkbook(fis);
}
/**
else if ("xlsx".equals(split[1])){
FileInputStream fis = new FileInputStream(excel);
wb = new XSSFWorkbook(fis);
}
*/
else {
//System.out.println("文件类型错误!");
logger.info("文件类型错误!");
return list;
} //开始解析
Sheet sheet = wb.getSheetAt(0); //读取sheet 0
int firstRowIndex = sheet.getFirstRowNum()+1; //第一行是列名,所以不读
int lastRowIndex = sheet.getLastRowNum();
//System.out.println("firstRowIndex: "+firstRowIndex);
logger.info("firstRowIndex: "+firstRowIndex);
//System.out.println("lastRowIndex: "+lastRowIndex);
logger.info("lastRowIndex: "+lastRowIndex);
//System.out.println("执行进行中......");
logger.info("执行进行中......"); //遍历行
for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {
//System.out.println("rIndex: " + rIndex);
Row row = sheet.getRow(rIndex);
if (row != null) {
//行中第一个值所在的列序号
int firstCellIndex = row.getFirstCellNum();
//行中最后一个值所在的列序号
int lastCellIndex = row.getLastCellNum();
UserRoleBean userRoleBean = new UserRoleBean();
//遍历列
//for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {
for (int cIndex = 0; cIndex < 3; cIndex++) {
Cell cell = row.getCell(cIndex);
if (cell != null) {
//将表格内容强制转换为字符型
cell.setCellType(Cell.CELL_TYPE_STRING);
//System.out.println(cell.toString());
if(cIndex == 0){
userRoleBean.setUserCode(cell.toString());
}else if(cIndex == 1){
userRoleBean.setRoleid(cell.toString());
}else if(cIndex == 2){
userRoleBean.setIsflag(cell.toString());
}
}
}
if(!("".equals(userRoleBean.getUserCode())) && !("".equals(userRoleBean.getRoleid()))){
//System.out.println(">>>0000000>>:"+userRoleBean.getRoleid());
list.add(userRoleBean);
}
}
}
} else {
//System.out.println("找不到指定的文件");
logger.info("找不到指定的文件");
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
} public static void main(String[] args) {
List<UserRoleBean> list = new ArrayList();
Client client = new Client();
list = client.getInfoFromExcle();
}
}
Java 实现后缀xls文件读取的更多相关文章
- Java学习-013-文本文件读取实例源代码(两种数据返回格式)
此文源码主要为应用 Java 读取文本文件内容实例的源代码.若有不足之处,敬请大神指正,不胜感激! 1.读取的文本文件内容以一维数组[LinkedList<String>]的形式返回,源代 ...
- java上传xls文件
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System. ...
- java与Excel (.xls文件) ---使用JXL创建,增添表格文件
由于一些原因要搞一下excel文件,个人感觉poi太难,所以用了JXL(感觉比较简单). 1.添加外部归档 jxl.jar 2. /** 生成的xls文件第一次需要手动选择EXCEL打开* * */ ...
- Java中IO流文件读取、写入和复制
//构造文件File类 File f=new File(fileName); //判断是否为目录 f.isDirectory(); //获取目录下的文件名 String[] fileName=f.li ...
- JAVA 中文 unicode 相互转换 文件读取
package com.test; import org.junit.Test; public class JunitTest { @Test public void test(){ String p ...
- Java代码操作properties文件(读取,新增/修改,删除)
项目中需要用到操作properties文件中的数据,记录一下 package com.bonc.savepic.save; import java.io.FileNotFoundException; ...
- Linux 读取 (*.xls)文件读取,使用libxls库
首先下载libxls,项目地址:http://sourceforge.net/projects/libxls/,备用下载:libxls-1.4.0.zip 解压后使用: ./configure --p ...
- Java学习-017-EXCEL 文件读取实例源代码
众所周知,EXCEL 也是软件测试开发过程中,常用的数据文件导入导出时的类型文件之一,此文主要讲述如何通过 EXCEL 文件中 Sheet 的索引(index)或者 Sheet 名称获取文件中对应 S ...
- java poi导入EXCEL xls文件代码
/** * */ package com.bn.car.common.report.excel; import java.io.FileInputStream; import java.io.IOEx ...
随机推荐
- 数据库outer连接
left (此处省略outer) join, 左边连接右边,左边最大,匹配所有的行,不管右边 right join,右边连接左边,右边最大,匹配所有的行,不管左边 条件直接放ON后面,是先筛选右边的表 ...
- POJ3150 Candies【差分约束】
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...
- Gym - 101149K Revenge of the Dragon 脑洞题,样例题
http://codeforces.com/gym/101149/problem/K 题意:题目贼长,但其实是个脑筋急转弯... 题解:题目要求某图形面积.该图形只有一个自由度,就是起点与终点距离x. ...
- linux:查找搜索文件
学习内容来源: 实验楼链接: https://www.shiyanlou.com/ 与搜索相关的命令常用的有 whereis,which,find 和 locate . whereis 简单快速 $w ...
- 解决windows7笔记本下玩游戏的显示问题
笔者是Windows7操作系统的粉丝,我的本本同样也是win7系统,和大家一样喜欢界面豪华漂亮,但包括笔者在内的不少用户总埋怨,在玩游戏时,游戏画面居中屏幕两边是黑条,无法全屏显示游戏画面.对于这个问 ...
- 【python-opencv】19-Canny边缘检测
Canny 边缘提取的目标是找到一个最优的边缘检测算法,最优边缘检测的含义是: 好的检测- 算法能够尽可能多地标识出图像中的实际边缘. 好的定位- 标识出的边缘要尽可能与实际图像中的实际边缘尽可能接近 ...
- 前端 html input标签 placeholder属性 标签上显示内容
前端 html input标签 的placeholder属性 标签上显示内容 <!DOCTYPE html> <html lang="en"> < ...
- sql批量修改wordpress网站的文章发布状态
wordpress批量导入文章的时候,有些文章的状态可能会缺失,例如“mis scheduled”.draft.future等几种状态,如何用sql批量修改wordpress网站的文章发布状态呢? 点 ...
- 为帝国cms模板添加站内搜索小教程
由于客户的需要,最近都在整帝国cms,很多东西还是不熟悉,特别是帝国cms模板,以前用的那些网站模板一般是保存在ftp文件中,而帝国cms模板是直接保存在数据库中,修改是在网站后台的模板管理,得慢慢适 ...
- 005-SpringBoot2.x整合Security5(解决 There is no PasswordEncoder mapped for the id "null")
问题描述 SpringBoot升级到了2.0之后的版本,Security也由原来的版本4升级到了5 使用WebSecurityConfigurerAdapter继承重置方法 protected voi ...