import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class ReadExcel {
private POIFSFileSystem pois ;
private HSSFWorkbook workBook;
private HSSFSheet sheet;
private HSSFRow row;
private HSSFCell cell;
List<String> RegionList = new ArrayList<String>();
Map<String, String> unitMap = new HashMap<String, String>(); //存放集合 <region,unit>1:1
Map<String, List<String>> deptMap = new HashMap<String, List<String>>(); //存放集合 <unit,dept>1:n
/**
* sheetName
* @param os
* @param sheetCount
* @return
* @throws Exception
*/
public List readExcelSheetName(InputStream os,int sheetCount) throws Exception{
pois = new POIFSFileSystem(os);
workBook = new HSSFWorkbook(pois);
for(int i=;i<sheetCount;i++){
sheet = workBook.getSheetAt(i);
String sheetName = sheet.getSheetName();
// System.out.println("sheetName:"+sheetName);
RegionList.add(sheetName);
List plist = readCell(sheet,sheetName);
File f = new File("F://person.txt");
OutputStream osa = new FileOutputStream(f);
System.out.println(plist.size() + " 总数");
for(int j = ;j<plist.size();j++){
Person per = (Person) plist.get(j);
StringBuffer sb = new StringBuffer();
sb.append("region:"+per.getCity()+" "+"unit:"+per.getUnit()+" "+"dept:"+per.getDept()+" "+"name:"+per.getName());
sb.append(" "+"tel:"+per.getTel()+" "+"phone:"+per.getPhone());
sb.append("\r\n");
osa.write(sb.toString().getBytes());
}
osa.close();
}
return RegionList;
} /**
* 一个sheet上的相关信心
* @param sheet
* @return
*/
public List readCell(HSSFSheet sheet,String sheetName){
List Personlist = new ArrayList();
String dept = "";
String unit = "";
int rowcount = ;
for (Iterator iterator = sheet.iterator(); iterator.hasNext();) {
Person person = new Person();
HSSFRow rows = (HSSFRow) iterator.next(); //获得每个sheet的行数
if(rowcount == ){ //有特殊的特殊处理,第一行标题
cell = rows.getCell();
String title = cell.getStringCellValue();
// System.out.println("一标题:"+title.trim());
}else if(rowcount == ){ //第二行,单位名称
cell = rows.getCell();
String unitCell = cell.getStringCellValue();
unit = unitCell.replace("单位名称:", "");
unitMap.put(sheetName, unit);//
}else{
for(int i = ;i<rows.getPhysicalNumberOfCells();i++){ //获得每行的cell个数
cell = rows.getCell(i);
if(rowcount == ){ //第三行标题栏
// switch (i){
// case 1 :
// String b1 = cell.getStringCellValue();
// System.out.println("三标题:"+b1);
// break;
// case 2 :
// String b2 = cell.getStringCellValue();
// System.out.println("三标题:"+b2);
// break;
// case 3:
// String b3 = cell.getStringCellValue();
// System.out.println("三标题:"+b3);
// break;
// case 4:
// String b4 = cell.getStringCellValue();
// System.out.println("三标题:"+b4);
// break;
// }
}else{
if(cell != null){
if(i == && formateCell(cell) != null&& !"".equals(formateCell(cell).trim())){ //第一个单元格有合并的部分
System.out.println(cell.getCellType()+"+++++"+cell.getStringCellValue().trim());
dept = cell.getStringCellValue();
dept = dept.replaceAll("\n", "").replace(" ","");
List deplist = deptMap.get("unit");
if(deplist == null ){
deplist = new ArrayList<String>();
deplist.add(dept);
}
deptMap.put(unit, deplist);
}else{
switch (i){
case :
String b1 = cell.getStringCellValue();
person.setName(b1);
break;
case :
String b2 = cell.getStringCellValue();
person.setPosition(b2);
break;
case :
//System.out.println("-------"+cell.getCellType());
double b3 =cell.getNumericCellValue(); //默认的cell类型:0:double,1:string
person.setTel(formatDouble(b3+""));
break;
case :
double b4 = cell.getNumericCellValue();
person.setPhone(formatDouble(b4+""));
break;
}
}
}
}
}
person.setCity(sheetName);
person.setDept(dept);
person.setUnit(unit);
if(rowcount != ){
Personlist.add(person);
}
}
rowcount ++;
}
return Personlist;
} /**
* 格式化cell里面的内容
* @param cell
* @return
*/
private String formateCell(HSSFCell cell){
String strCell = "";
switch (cell.getCellType()){
case HSSFCell.CELL_TYPE_STRING:
strCell = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
strCell = formatDouble(String.valueOf(cell.getNumericCellValue()));
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
strCell = "";
break;
default:
strCell = "";
break;
}
if (strCell.equals("") || strCell == null) {
return "";
}
if (cell == null) {
return "";
}
return strCell;
} private String formatDouble(String str) {
if (str == null || "".equals(str.trim()))
return "";
if (str.indexOf(".") > ) {
if (str.indexOf("E") > ) {
str = str.substring(, str.indexOf("E")).replace(".", "");
} else {
str = str.substring(, str.indexOf("."));
}
}
return str;
} }

所需jar

官网下载最新jar : http://poi.apache.org/

POI 读取 Excel 文件的更多相关文章

  1. JAVA使用POI读取EXCEL文件的简单model

    一.JAVA使用POI读取EXCEL文件的简单model 1.所需要的jar commons-codec-1.10.jarcommons-logging-1.2.jarjunit-4.12.jarlo ...

  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 文件,读取手机号码 变成 1.3471022771E10

    使用POI 读取 Excel 文件,读取手机号码 变成 1.3471022771E10 [问题点数:40分,结帖人xieyongqiu]             不显示删除回复             ...

  4. 使用jxl,poi读取excel文件

    作用:在java后台添加一个方法,读取导入的excel内容,根据需要返回相应的sql语句,以完成对临时表的插入操作. 使用jxl读取excel文件 package com.sixthf.bi.sapp ...

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

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

  6. jspsmart(保存文件)+poi(读取excel文件)操作excel文件

    写在前面: 项目环境:jdk1.4+weblogic 需求:能上传excel2003+2007 由于项目不仅需要上传excel2003,还要上传excel2007,故我们抛弃了jxl(只能上传exce ...

  7. Java实现POI读取Excel文件,兼容后缀名xls和xlsx

    1.引入所需的jar包: maven管理项目的话直接添加以下坐标即可: <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -- ...

  8. 使用poi读取excel文件 Cannot get a text value from a numeric cell

    我这样转换得到一个excel文本域的值 Cell cell = row.getCell(c); cell.setCellType(Cell.CELL_TYPE_STRING); String park ...

  9. spring boot 使用 POI 读取Excel文件

    内容简介 本文主要介绍使用POI进行Excel文件的相关操作,涉及读取文件,获取sheet表格,对单元格内容进行读写操作,以及合并单元格的操作. Excel文件目录 Excel模板文件存了resour ...

  10. POI读取excel文件。

    1) poi读取现成.xls文件,不需要自己建立.xls ====ReadExcel​类​==== package cust.com.excelToDataTest; import java.io.F ...

随机推荐

  1. hexo新建md文档推送到blog出错

    1. git错误提示 $ hexo g INFO Start processing FATAL Something's wrong. Maybe you can find the solution h ...

  2. Codeforces 174B【模拟构造】

    题意: 给你一个串只有小写字母和点,让你构造前缀是1-8,后缀是1-3长度的文件名: 思路: 那么以"."作为分割点,把字符串都拿出来,然后 首段长度<=8 OK; 中间&l ...

  3. NGUI研究院之UISprite和UITexture浅谈

    NGUI的三大组件,UILabel.UISprite.UITexture,它们三个同时都继承UIWidget.先回到一个很郁闷的话题上,到底是优化DrawCall还是优化内存. UISprite : ...

  4. SpringBoot | 启动异常 | 显示bulid success 无 error信息

    可能原因是没有添加 web 依赖,检查pom里面是否有web <dependency> <groupId>org.springframework.boot</groupI ...

  5. O(nlogn)求逆序数对的个数

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  6. 【aspnetcore】在过滤器(Filter)中使用注入服务(ServiceFilter|TypeFilter)

    在MVC中,AOP是很常用的功能,我们经常会使用如 ActionFilter,IAuthorizeFilter 等描述对Controller和Action进行约束和扩展,一般做法如下: public ...

  7. morhpia(4)-更新

    更新由2部分组成:一个查询和一组更新操作符.本例是跟所有薪水小于等于2000的员工涨工资500. @Test public void update() throws Exception { //第一步 ...

  8. 551 Student Attendance Record I 学生出勤纪录 I

    给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符:    'A' : Absent,缺勤    'L' : Late,迟到    'P' : Present,到场如果一个学生的出勤纪 ...

  9. 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入:   1    \     3    /   2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...

  10. vue项目打包后文本溢出代码消失问题

    补充 https://www.cnblogs.com/richard1015/p/8526988.html vue webpack 打包编译-webkit-box-orient: vertical 后 ...