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. Lightoj1013【DP_LCS】

    题意: 给你两个字符串,让你求一个最短字符串,其中存在给出串的种类: 求这个字符串的长度和种类: 思路: //dp[i,j,k]表示前i个字符,包含s1串前j个字母,包含s2串前k个字符时的方案数. ...

  2. unity从模型中抽取动画文件(animation)

    http://www.cnblogs.com/leng-yuye/archive/2013/01/11/2856144.html 由于模型是由第三方的软件制作的,用unity不能直接编辑模型里的动画文 ...

  3. Pro Android学习笔记(一五四):传感器(4):陀螺仪、加速传感器

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处http://blog.csdn.net/flowingflying/以及作者@恺风Wei. 陀螺仪 陀螺仪(Gyr ...

  4. CentOS 6.5 升级gcc到4.8 以及libstdc++

    CentOS 6.5 自带gcc太旧不支持c11, 升级到4.8的步骤: 引用: http://cache.baiducontent.com/c?m=9d78d513d99216f31eb0d5690 ...

  5. 图片美化增强AI接口调用手册

    在调合合AI平台提供的图片美化增强API接口,API平台链接:https://ai.ccint.com/doc/api/crop_enhance_image, 因为有遇到一些问题,写篇博客记录一下 A ...

  6. socket模拟服务器,客户端下载东西(ftp)

    服务端(ftp_server.py) import hashlib,socket,os server = socket.socket() server.bind(("localhost&qu ...

  7. java操作mongodb数据库实现新建数据库,新建集合,新建文档

    *首先明确一点,要通过java代码创建mongodb数据库实例,需要同时创建集合和文档. 代码实现: /* 建立与mongodb数据库的连接,可指定参数,如:MongoClient client = ...

  8. E. 打击判定 判断矩形是否相交

    有一个很明显的做法就是判断PointInPolygon .枚举第二个矩形的点,是否在第一个矩形内,但是有bug 就是那种第二个矩形很大的那种,所以容易想到又枚举第一个矩形的点,看是否在第二个矩形里. ...

  9. oracle 直接连接到数据库 CMD窗口

    打开cmd 在命令行 中输入如下命令  sqlplus sys/zd****!@60.**.203/orcl as sysdba  就直接连上了

  10. datetimepicker 插件位置问题解决经验

    使用dadetimepicker进行时间选择是个很不错的选择,但是美中不足的是该插件在chrome中显示弹框的时候有时会出现位置错位的现象,而在IE中则没有这种现象,视图如图1 图1 查阅了网上的资料 ...