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. 用spin和edit控件来用spin控制edit里面小数的增减

    1.响应SPIN的消息,就是点SPIN的上键头和下键头的消息,在这个消息里改变值是以0.1步进量增减.2.使用UpdateData(FALSE)来更新EDIT的关联的double型的变量. 创建步骤 ...

  2. poj2133(sg函数)

    关于sg函数:http://www.cnblogs.com/Knuth/archive/2009/09/05/1561007.html 题目链接:http://poj.org/problem?id=2 ...

  3. 基础篇-环境变量 .bash_profile

    千里之行始于足下~~~ PGHOST或者PGHOSTADDR PGPORT PGDATABASE PGUSER PGPASSWORD(不推荐使用这个,推荐使用.pgpass)

  4. sql server 数据库维护

    select 'kill ',s.spid, sd.name, * from sys.sysprocesses sinner join sys.sysdatabases sd on s.dbid=sd ...

  5. 小程序video置顶

    page { overflow-y: none; height: 100vh; } .page__hd_media { position: fixed; width:100vw; top:; heig ...

  6. java数据结构----图

    1.图:.在计算机程序设计中,图是最常用的数据结构之一.对于存储一般的数据问题,一般用不到图.但对于某些(特别是一些有趣的问题),图是必不可少的.图是一种与树有些相像的数据结构,从数学意义上来讲,树是 ...

  7. 根据select中选定option触发不同事件

    做公司项目,要实现一个小需求,根据下拉框选定的数据,绑定不同的事件.我的思路是获取select下面的option值,然后给它们定义不同的点击事件,但是在使用中option不能添加类,同时没有click ...

  8. Codeforces 1142B(倍增)

    1.先预处理出在循环中某数前面的数是谁. 2.读入a数列时贪心选取最晚的父亲. 3.链上倍增预处理二进制祖先. 4.对于每个位置,预处理第n-1个祖先位置最早要从哪里开始,技巧上再顺手与前一位的最早位 ...

  9. CentOS Linux 搭建 SVN(CollabNet Subversion)服务器

    安装CollabNet Subversion之前必须先安装JDK1.6和python2.4 ~ 2.6 groupadd svn useradd -g svn svnuser  passwd svnu ...

  10. Metasploits之ms10_018

    漏洞详情:https://technet.microsoft.com/library/security/ms10-018 一准备: 1:kali Linux系统 192.168.195.129 2:W ...