POI读取excel


import java.io.FileInputStream;
import java.util.ArrayList;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

public class TestPOIreadExcel {
public static void main(String[] args) throws Exception {
String base="C:/Documents and Settings/Administrator/桌面/";
String e2007 = "1.xlsx";
String e2003 = "1.xls";
String path =base+e2003;
Sheet esheet =null;
if(path.indexOf(".xlsx")!=-1) {
org.apache.poi.xssf.usermodel.XSSFWorkbook wb = new org.apache.poi.xssf.usermodel.XSSFWorkbook(new FileInputStream(path));
org.apache.poi.xssf.usermodel.XSSFSheet sheet = wb.getSheetAt(0); //获取2007excel第一页
esheet = sheet;
}else {
org.apache.poi.hssf.usermodel.HSSFWorkbook wb = new org.apache.poi.hssf.usermodel.HSSFWorkbook(new FileInputStream(path));
org.apache.poi.hssf.usermodel.HSSFSheet sheet = wb.getSheetAt(0); //获取2003excel第一页
esheet = sheet;
}
int[] cols = new int[]{0,1};
// readExcel(excel标签页,获取行,获取列);
readExcel(esheet,1,cols);
}

protected static ArrayList<String> readExcel(Sheet sheet, int rownum,int[] cols) {
ArrayList<String> rowData = new ArrayList<String>();

for (int colIndex : cols) {
Row row = sheet.getRow(rownum);
Cell cell = row.getCell(colIndex);
rowData.add(getCellValue(cell));
}

return rowData;
}

/** 获取单元格内容 */
private static String getCellValue(Cell cell) {
if (cell == null) {
return "";
}
String value = cell.toString().trim();
System.out.println(value+"===============");
try {
// This step is used to prevent Integer string being output with '.0'.
Float.parseFloat(value);
value = value.replaceAll("\\.0$", "");
value = value.replaceAll("\\.0+$", "");
return value;
} catch (NumberFormatException ex) {
return value;
}
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值