所需jar 包 :poi-3.6
poi-3.6-20091214.jarpoi-contrib-3.6-20091214.jarpoi-scratch.6-20091214.jargeronimo-stax-api_1.0_spec-1.0.jar
xmlbeans-2.3.0.jar
读取excel 文件的 java 代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcel {
/**
* 对外提供读取excel 的方法
* */
public static List<List<Object>> readExcel(File file) throws IOException{
String fileName = file.getName();
String extension = fileName.lastIndexOf(".")==-1?"":fileName.substring(fileName.lastIndexOf(".")+1);
if("xls".equals(extension)){
return read2003Excel(file);
}else if("xlsx".equals(extension)){
return read2007Excel(file);
}else{
throw new IOException("不支持的文件类型");
}
}
/**
* 读取 office 2003 excel
* @throws IOException
* @throws FileNotFoundException */
private static List<List<Object>> read2003Excel(File file) throws IOException{
List<List<Object>> list = new LinkedList<List<Object>>();
HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet sheet = hwb.getSheetAt(0);
Object value = null;
HSSFRow row = null;
HSSFCell cell = null;
for(int i = sheet.getFirstRowNum();i<= sheet.getPhysicalNumberOfRows();i++){
row = sheet.getRow(i);
if (row == null) {
continue;
}
List<Object> linked = new LinkedList<Object>();
for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
cell = row.getCell(j);
if (cell == null) {
continue;
}
DecimalFormat df = new DecimalFormat("0");// 格式化 number String 字符
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
System.out.println(i+"行"+j+" 列 is String type");
value = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
System.out.println(i+"行"+j+" 列 is Number type ; DateFormt:"+cell.getCellStyle().getDataFormatString());
if("@".equals(cell.getCellStyle().getDataFormatString())){
value = df.format(cell.getNumericCellValue());
} else if("General".equals(cell.getCellStyle().getDataFormatString())){
value = nf.format(cell.getNumericCellValue());
}else{
value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
}
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
System.out.println(i+"行"+j+" 列 is Boolean type");
value = cell.getBooleanCellValue();
break;
case XSSFCell.CELL_TYPE_BLANK:
System.out.println(i+"行"+j+" 列 is Blank type");
value = "";
break;
default:
System.out.println(i+"行"+j+" 列 is default type");
value = cell.toString();
}
if (value == null || "".equals(value)) {
continue;
}
linked.add(value);
}
list.add(linked);
}
return list;
}
/**
* 读取Office 2007 excel
* */
private static List<List<Object>> read2007Excel(File file) throws IOException {
List<List<Object>> list = new LinkedList<List<Object>>();
// 构造 XSSFWorkbook 对象,strPath 传入文件路径
XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
// 读取第一章表格内容
XSSFSheet sheet = xwb.getSheetAt(0);
Object value = null;
XSSFRow row = null;
XSSFCell cell = null;
for (int i = sheet.getFirstRowNum(); i <= sheet
.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
if (row == null) {
continue;
}
List<Object> linked = new LinkedList<Object>();
for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
cell = row.getCell(j);
if (cell == null) {
continue;
}
DecimalFormat df = new DecimalFormat("0");// 格式化 number String 字符
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
System.out.println(i+"行"+j+" 列 is String type");
value = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
System.out.println(i+"行"+j+" 列 is Number type ; DateFormt:"+cell.getCellStyle().getDataFormatString());
if("@".equals(cell.getCellStyle().getDataFormatString())){
value = df.format(cell.getNumericCellValue());
} else if("General".equals(cell.getCellStyle().getDataFormatString())){
value = nf.format(cell.getNumericCellValue());
}else{
value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
}
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
System.out.println(i+"行"+j+" 列 is Boolean type");
value = cell.getBooleanCellValue();
break;
case XSSFCell.CELL_TYPE_BLANK:
System.out.println(i+"行"+j+" 列 is Blank type");
value = "";
break;
default:
System.out.println(i+"行"+j+" 列 is default type");
value = cell.toString();
}
if (value == null || "".equals(value)) {
continue;
}
linked.add(value);
}
list.add(linked);
}
return list;
}
}
说明:该类中共封装了三个方法,对外提供的读取excel文件的方法,两个私有的分别读取excel2003和excel2007的方法。外部使用,只需调用readExcel 方法,传入一个File 参数,程序根据文件扩展名来判断选取那个方法来读取Excel文件。
相关推荐
利用POI取得Excel所用到的jar包
读取Excel的poi包,,读取Excel的poi包,,读取Excel的poi包,读取Excel的poi包读
这是一个POI读取word 2003 和 word 2007的例子 是一个Eclipse工程 下载后直接导入工程 运行 src 目录下的 Test java 类即可 这个 rar 包中包含着 POI 读取word 2003 和 word 2007 所需要的 jar 包 也有需要读取的 ...
JAVA IO操作 POI读取excel的内容
利用POI读取excel写入到word 利用POI读取excel写入到word 利用POI读取excel写入到word,压缩包里包含了jar包
POI读取word文档的文字内容和图片内容
POI读取excel的例子
java读取excel 表格数据。 public static void main(String[] args) { String[][] content=POIExcelUtils.read("E:\\1.xlsx"); for(int i=0;i;i++) { if(content[i]!=null){ for(int j=0;j[i].length;j...
POI,读取,Excel包,jar POI,读取,Excel包,jar POI,读取,Excel包,jar
使用poi读取excel内容 所需jar包
POI生成Excel POI读取Excel java操作Excel Servlet生成Excel web项目,包含含读取Excel与生成Excel方法
POI无法读取到word每一段所在的页码。我给出了代码和所有的解释,用POI读取word中的页码。也算是小方法了,基本很准确,为什么要加一个基本呢?因为,分页标志符号在第一行为空白行的时候没有分页符!!所以你的文档...
POI 读取Excel文件 POI 读取Excel文件
apache poi 读取 Excel 的 jar 包 博文链接:https://wxinpeng.iteye.com/blog/231895
使用Poi读取大数据量excel的方法 支持2003和2007的版本
java.lang.NullPointerException at org.apache.poi.openxml4j.opc....在linux环境下poi读取word文件产生的NullPointerException,此问题找了两天最终解决。希望有同样问题的人能快速解决问题,所以上传此文档
使用poi读取写入复杂excel内容包括样式,工具类
java读写Excel,POI.JAR,Word内容读取
poi读取合并单元格帮助类
实现了JAVA 窗口,读取EXCEL文件,用poi读取EXCEL内容只是一个小例子