1. 下载poi库
http://poi.apache.org/download.html
2.将poi核心文件取出放入myeclipse的lib中
3.测试代码
package test5;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.ParseException;
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;
/**
* 读取Excel测试
* @author chitianxiang
* @version v1.0 $17th April, 2012 - Tuesday - 4:34 p.m
*/
public class Test1 {
public static void main(String[] args) {
File file = new File("E:/chtx/项目/home.xls");
InputStream fileIn = null;
InputStream in = null;
try {
fileIn = new FileInputStream(file);
in = new BufferedInputStream(fileIn);
HSSFWorkbook wb = new HSSFWorkbook(in);
for (int i = 0, sheetNum = wb.getNumberOfSheets(); i < sheetNum; i++) {
HSSFSheet sheet = wb.getSheetAt(i); //读取页面
if (sheet == null) {
continue;
}
for (int j = 0, rowNum = sheet.getLastRowNum(); j < rowNum; j++) {
HSSFRow row = sheet.getRow(j); //读取行
if (row == null) {
continue;
}
for (int z = 0, cellNum = row.getLastCellNum(); z < cellNum; z++) {
HSSFCell cell = row.getCell(z); //读取列
if (cell == null) {
continue;
}
switch (z) {
case 0:
System.out.println(readCell2String(cell));
break;
case 1:
System.out.println(readCell2String(cell));
break;
default:
//do nothing
break;
}
}
}
}
} catch (FileNotFoundException e) {
System.out.println("文件没找着");
e.printStackTrace();
} catch (IOException e) {
System.out.println("读取流异常");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileIn != null) {
try {
fileIn.close();
} catch (IOException e) {
System.out.println("关闭流异常");
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
System.out.println("关闭流异常");
e.printStackTrace();
}
}
}
}
/**
* 将单元格内容转换成字符串,空白和错误默认为空
* @param cell excel 单元格
* @author chitianxiang $17th April, 2012 - Tuesday - 5:54 p.m
*/
private static String readCell2String(HSSFCell cell) {
String cellContent = "";
switch(cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: // 数字
cellContent = cell.getNumericCellValue() + "";
break;
case HSSFCell.CELL_TYPE_STRING: // 字符串
cellContent = cell.getStringCellValue() + "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // boolean
cellContent = cell.getBooleanCellValue() + "";
break;
case HSSFCell.CELL_TYPE_FORMULA: // 公式
cellContent = cell.getCellFormula() + "";
break;
case HSSFCell.CELL_TYPE_BLANK: // 空白
case HSSFCell.CELL_TYPE_ERROR: // 错误
default:
cellContent = "";
break;
}
return normalNumber(cellContent);
}
/**
* 将特殊数字格式转换成普通格式
* @param cellContent 数字
* @author chitianxiang $17th April, 2012 - Tuesday - 6:09 p.m
*/
private static String normalNumber (String cellContent) {
String tempCellContent = cellContent;
//如果读取的是科学计数法的格式,则转换为普通格式
if (null != tempCellContent
&& tempCellContent.indexOf(".") != -1
&& tempCellContent.indexOf("E") != -1) {
DecimalFormat df = new DecimalFormat();
try {
tempCellContent = df.parse(tempCellContent).toString();
} catch (ParseException e) {
//do nothing
e.printStackTrace();
}
//如果读取的是数字格式,并且以".0"结尾格式,则转换为普通格式
} else if (null != tempCellContent
&& tempCellContent.endsWith(".0")) {
int size = tempCellContent.length();
tempCellContent = tempCellContent.substring(0, size - 2);
}
return tempCellContent;
}
}
分享到:
相关推荐
Java读取Excel内容 v Java读取Excel内容 Java读取Excel内容
JAVA读取 excelJAVA读取 excel
java读取excel数据导入数据库源码 java读取excel数据导入数据库源码
完美解决java读取excel内存溢出问题,希望可以帮到大家
java 读取Excel文件中的内容 java 读取Excel文件中的内容
java读取excel表格的数据,并将其保存!已经调试过了,可以使用!
java 读取excel内容 创建excel 插入文字及图片 可以直接运行
通过Java读取Excel文件中的数据时,需要自动判断当前Cell中的数据格式是否日期格式,如果是就要做特殊处理,以便读出数据的格式与预想的相符。
JAVA读取excel
Java读取excel
JAVA读取Excel,建库建表,并生成java实体
java读取excel所有页的数据,可能会有bug 毕竟我测试是没有,小白一个,大佬勿喷啊,网上搜的资源然后改写的读取所有页,可能会有不足的地方。01年的弟弟,不喜勿喷
java读取Excel表中的内容,可以读取多个sheet,生成自定义格式的xml文件
使用Java读取Excel文件的内容,主要是使用POI框架,可以读取最新的Excel文件xlsx后缀文件,即2007版本之后的。
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...
java读取xls中的数据,每个对象对应多个属性值,根据每个对象的属性值来生成一个二维矩阵
java读取excel的工具类型,可同时支持excel2003和2007版本
java读取excel文件内容,可读取2003和2007版本
poi.jar java读取excel 的 jar包