2003版office excel读取

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ReadExcel03 { /**
* 读取excel
* @param is
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static List<List<String>> readExcel(InputStream is)throws FileNotFoundException, IOException {
// 构造 HSSFWorkbook 对象,传入参数为excel文件的io流
HSSFWorkbook wb = new HSSFWorkbook(is);
// 读取第一个sheet的内容
HSSFSheet sheet = wb.getSheetAt(0);
// 获取所有行的迭代对象
Iterator<Row> rowIter = sheet.rowIterator();
// 获取合并单元格对象的相关信息
List<ExcelMergedRegionBean> mergedRegionMapList = new CopyOnWriteArrayList<ExcelMergedRegionBean>(getMergedRegionMapList(sheet));
List<List<String>> contentList = new ArrayList<List<String>>();
// 迭代所有行
while (rowIter.hasNext()) {
List<String> cellList = new ArrayList<String>();
// 获取每一行
Row row = rowIter.next();
// 获取该行的列迭代对象
Iterator<Cell> cellIter = row.cellIterator();
// 迭代该行的每一列
while (cellIter.hasNext()) {
// 获取该行的每一列
Cell cell = cellIter.next();
// 获取该单元格的值
String content = getActualCellValue(sheet, mergedRegionMapList,cell);
cellList.add(content); }
contentList.add(cellList);
}
return contentList;
}
/**
* 获取单元格真实的值
* @param sheet
* @param mergedRegionMapList
* @param myCell
* @return
*/
public static String getActualCellValue(HSSFSheet sheet,List<ExcelMergedRegionBean> mergedRegionMapList, Cell myCell) { Cell actualCell = myCell; // 迭代合并单元格对象,判断myCell该对象是否属于合并单元格
for (ExcelMergedRegionBean mb : mergedRegionMapList) { if (myCell.getRowIndex() > mb.getLastRow()) { mergedRegionMapList.remove(mb); } // 判断myCell该对象是否属于合并单元格,如果是的话,则直接退出循环
if (myCell.getColumnIndex() <= mb.getLastCell()
&& myCell.getColumnIndex() >= mb.getFirstCell()
&& myCell.getRowIndex() <= mb.getLastRow()
&& myCell.getRowIndex() >= mb.getFirstRow()) { Row row = sheet.getRow(mb.getFirstRow()); Cell cell = row.getCell(mb.getFirstCell()); actualCell = cell; break; } } // 返回该单元对应的真实值
return getCellValue(actualCell); } /**
* 处理合并的列
* @param sheet
* @return
*/
public static List<ExcelMergedRegionBean> getMergedRegionMapList(HSSFSheet sheet) {
List<ExcelMergedRegionBean> mergedRegionMapList = new ArrayList<ExcelMergedRegionBean>();
// 获得一个 sheet 中合并单元格的数量
int sheetmergerCount = sheet.getNumMergedRegions();
// 便利合并单元格
for (int i = 0; i < sheetmergerCount; i++) {
// 获得合并单元格
CellRangeAddress ca = sheet.getMergedRegion(i);
// 获得合并单元格的起始行, 结束行, 起始列, 结束列
int firstC = ca.getFirstColumn();
int lastC = ca.getLastColumn();
int firstR = ca.getFirstRow();
int lastR = ca.getLastRow();
ExcelMergedRegionBean mb = new ExcelMergedRegionBean();
mb.setFirstRow(firstR);
mb.setLastRow(lastR);
mb.setFirstCell(firstC);
mb.setLastCell(lastC);
mergedRegionMapList.add(mb); } // 排序,便于后面循环删除
Collections.sort(mergedRegionMapList);
return mergedRegionMapList; }
/**
* 获得单元格的值
* @param cell
* @return
*/
public static String getCellValue(Cell cell) {
String cellValue = "";
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
cellValue = rPadZeroUtil(String.valueOf(cell.getNumericCellValue()));
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
cellValue = String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
cellValue = cell.getStringCellValue();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
cellValue = String.valueOf(cell.getCellFormula());
}
return cellValue;
} public static String rPadZeroUtil(String value) {
if (value != null && !"".equals(value)) {
if (value.endsWith(".0")) {
return value.substring(0, value.indexOf(".0"));
}
}
return value;
}
} 2007版 excel 读取 import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ReadExcel07 { /**
* 读取excel
* @param is
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static List<List<String>> readExcel(InputStream is)throws FileNotFoundException, IOException {
// 构造 XSSFWorkbook 对象,传入参数为excel文件的io流
XSSFWorkbook wb = new XSSFWorkbook(is);
// 读取第一个sheet的内容
XSSFSheet sheet = wb.getSheetAt(0);
// 获取所有行的迭代对象
Iterator<Row> rowIter = sheet.rowIterator();
// 获取合并单元格对象的相关信息
List<ExcelMergedRegionBean> mergedRegionMapList = new CopyOnWriteArrayList<ExcelMergedRegionBean>(getMergedRegionMapList(sheet));
List<List<String>> contentList = new ArrayList<List<String>>();
// 迭代所有行
while (rowIter.hasNext()) {
List<String> cellList = new ArrayList<String>();
// 获取每一行
Row row = rowIter.next();
// 获取该行的列迭代对象
Iterator<Cell> cellIter = row.cellIterator();
// 迭代该行的每一列
while (cellIter.hasNext()) {
// 获取该行的每一列
Cell cell = cellIter.next();
// 获取该单元格的值
String content = getActualCellValue(sheet, mergedRegionMapList,cell);
cellList.add(content); }
contentList.add(cellList);
}
return contentList;
} /**
* 获取单元格真实的值
* @param sheet
* @param mergedRegionMapList
* @param myCell
* @return
*/
public static String getActualCellValue(XSSFSheet sheet,List<ExcelMergedRegionBean> mergedRegionMapList, Cell myCell) {
Cell actualCell = myCell;
// 迭代合并单元格对象,判断myCell该对象是否属于合并单元格
for (ExcelMergedRegionBean mb : mergedRegionMapList) {
if (myCell.getRowIndex() > mb.getLastRow()) {
mergedRegionMapList.remove(mb);
} // 判断myCell该对象是否属于合并单元格,如果是的话,则直接退出循环
if (myCell.getColumnIndex() <= mb.getLastCell()
&& myCell.getColumnIndex() >= mb.getFirstCell()
&& myCell.getRowIndex() <= mb.getLastRow()
&& myCell.getRowIndex() >= mb.getFirstRow()) { Row row = sheet.getRow(mb.getFirstRow());
Cell cell = row.getCell(mb.getFirstCell());
actualCell = cell;
break; } }
// 返回该单元对应的真实值
return getCellValue(actualCell); } /**
* 处理有合并的列
* @param sheet
* @return
*/
public static List<ExcelMergedRegionBean> getMergedRegionMapList(XSSFSheet sheet) { List<ExcelMergedRegionBean> mergedRegionMapList = new ArrayList<ExcelMergedRegionBean>();
// 获得一个 sheet 中合并单元格的数量
int sheetmergerCount = sheet.getNumMergedRegions();
// 便利合并单元格
for (int i = 0; i < sheetmergerCount; i++) {
// 获得合并单元格
CellRangeAddress ca = sheet.getMergedRegion(i);
// 获得合并单元格的起始行, 结束行, 起始列, 结束列
int firstC = ca.getFirstColumn();//第一列
int lastC = ca.getLastColumn();//最后一列
int firstR = ca.getFirstRow();//第一行
int lastR = ca.getLastRow();//第二行
ExcelMergedRegionBean mb = new ExcelMergedRegionBean();
mb.setFirstRow(firstR);
mb.setLastRow(lastR);
mb.setFirstCell(firstC);
mb.setLastCell(lastC);
mergedRegionMapList.add(mb);
} // 排序,便于后面循环删除
Collections.sort(mergedRegionMapList);
return mergedRegionMapList; } /**
* 获得单元格的值
* @param cell
* @return
*/
public static String getCellValue(Cell cell) {
String cellValue = "";
if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC)
cellValue = String.valueOf(cell.getNumericCellValue());
else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN)
cellValue = String.valueOf(cell.getBooleanCellValue());
else if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING)
cellValue = cell.getStringCellValue();
else if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA)
cellValue = String.valueOf(cell.getCellFormula()); return cellValue;
}
} 适配器 import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List; /**
* 读取excel适配器
* @author Administrator
*
*/
public class ReadExcelAdapter { private ReadExcelAdapter(){}
/**
* 适配方法:可读取03版本和07版本的excel
* @return
*/
public static List<List<String>> readExcel(String filePath){
InputStream is = null;
try {
is = new FileInputStream(filePath);
return ReadExcel03.readExcel(is);
} catch (Exception e) {
try {
is = new FileInputStream(filePath);
return ReadExcel07.readExcel(is);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e3) {
e3.printStackTrace();
}
}
return null;
}
}
测试类 import java.util.List; public class ReadExcelTest {
public static void main(String[] args) throws Exception { long startTime = System.currentTimeMillis(); List<List<String>> result = ReadExcelAdapter.readExcel("C:/test.xlsx");
System.out.println(result); System.out.println("use time:"+ (System.currentTimeMillis() - startTime));
}
}

java POI读取excel 2007/2003的更多相关文章

  1. java poi 读取excel内容

    import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import or ...

  2. Java POI读取Excel数据,将数据写入到Excel表格

    1.准备 首先需要导入poi相应的jar包,包括: 下载地址:http://pan.baidu.com/s/1bpoxdz5 所需要的包的所在位置包括: 2.读取Excel数据代码 package S ...

  3. Java POI 读取Excel数据转换为XML格式

    1.首先要下载poi相关的包:http://poi.apache.org/  ,以下是所需的jar包 2.贴上详细的代码 public class ExcelToXml { /** * 将excel的 ...

  4. POI读取EXCEL(2007以上)

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; im ...

  5. Java——poi读取Excel文件

    1.创建文件流,打开EXCEL文件 FileInputStream excelFile = new FileInputStream(excelPath); XSSFWorkbook workbook ...

  6. java poi读取excel公式,返回计算值(转)

    http://blog.csdn.net/CYZERO/article/details/6573015 经测试,确实可以 1 package hrds.zpf.poi;  2  3  import o ...

  7. java POI创建Excel示例(xslx和xsl区别 )

    Java用来处理office类库有很多,其中POI就是比较出名的一个,它是apache的类库,现在版本到了3.10,也就是2014年2月8号这个版本. 在处理PPT,Excel和Word前,需要导入以 ...

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

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

  9. java用poi读取Excel表格中的数据

    Java读写Excel的包是Apache POI(项目地址:http://poi.apache.org/),因此需要先获取POI的jar包,本实验使用的是POI 3.9稳定版.Apache POI 代 ...

随机推荐

  1. 【HDU1198】Farm Irrigation(回溯+记忆化搜索)

    数据流小,深搜即可.有些暴力.看其他人的题解用二维转换成一维做的并查集很巧妙,马上去研究一下!! #include <iostream> #include <cstring> ...

  2. thinkjs与Fine Uploader的邂逅

        最近在做一个内部系统,需要一个无刷新的上传功能,找了许久,发现了一个好用的上传工具-Fine Uploader,网上也有不少关于它的介绍,对我有不少的启发,结合我的使用场景简单的介绍一下它与t ...

  3. qt model/view 架构基础介绍之QTableWidget

    # -*- coding: utf-8 -*- # python:2.x #说明:QTreeWidget用于展示树型结构,也就是层次结构同前面说的 QListWidget 类似,这个类需要同另外一个辅 ...

  4. BZOJ 2733 HNOI 2012 永无乡 平衡树启示式合并

    题目大意:有一些岛屿,一開始由一些无向边连接. 后来也有不断的无向边增加,每个岛屿有个一独一无二的重要度,问随意时刻的与一个岛屿联通的全部岛中重要度第k大的岛的编号是什么. 思路:首先连通性一定要用并 ...

  5. 《TCP/IP具体解释卷2:实现》笔记--4种不同类型的mbuf

    mbuf的主要用途是保存子进程和网络接口间互相传递的用户数据.但mbuf也用于保存其它各种数据:源于目的地址.插口 选项等等. 以下介绍我们要遇到的四种类型的mbuf,它们根据在成员m_flag中填写 ...

  6. iOS开发CoreAnimation解读之二——对CALayer的分析

    iOS开发CoreAnimation解读之二——对CALayer的分析 一.UIView中的CALayer属性 1.Layer专门负责view的视图渲染 2.自定义view默认layer属性的类 二. ...

  7. 用gitolite新建项目,clone后首次push,可能会出现: git: No refs in common and none specified; doing no

    用gitolite新建项目,clone后首次push,可能会出现:     $ git push No refs in common and none specified; doing nothing ...

  8. 利用Unicorn和Idaemu辅助解决Geekpwn SecretCode

    在前面的些文章里,我提到了怎么交叉编译Unicorn-engine,以及在windows上使用Unicorn python bindings进行分析程序.这一次我介绍下如何使用Unicorn-engi ...

  9. django的Model 模型中常用的字段类型

    常用的字段类型: AutoField:自增长字段,通常不用,如果未在Model中显示指定主键,django会默认建立一个整型的自增长主键字段 BooleanField:布尔型,值为True或False ...

  10. android 注释常用标签

    javadoc: {@link ActivityGroup}   链接到包.类: {@link #setContentView} 用#链接到类成员: @return View The current ...