package com;
import java.io.FileInputStream;
import java.io.InputStream;
/**
* 注意:使用前请导入相关的jar包("poi-3.7-20101029.jar");
* ISNULL带表当前的单元格是空的
*/
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;
/**
*
* @author 石头 这个类紧用于读取;(用于2003版excel包含2003版)
* @since2011-10-27
* @version e0.1
*/
public class InputExcel {
/**
*
* @param path文件路
* @param index
* excel工作表(index带表你想获取的那个工作表是excel中的第几张,从0开始;
* @throws Exception
*/
public static StringBuffer inputExcel(String path, int index) throws Exception {
StringBuffer excel_Data = new StringBuffer();
InputStream input = new FileInputStream(path);
HSSFWorkbook work = new HSSFWorkbook(input);
HSSFSheet sheet = work.getSheetAt(index);// 获取工作表
if (null != sheet) {
for (int i = 0; i < sheet.getLastRowNum(); i++)// getLastRowNum是得到工作表的数据总行数;
{
HSSFRow row = sheet.getRow(i);// 每次循环下标指向下一行
if (null != row) {
for (int j = 0; j < row.getLastCellNum(); j++) {
HSSFCell cell = row.getCell(j);// 每次循环下标指向下一个单元格
if (null != cell) {
// 下面是一个
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
excel_Data.append(cell.getStringCellValue()
+ " ");
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
excel_Data.append(cell.getBooleanCellValue()
+ " ");
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// 这里读取excel表单中的日期
excel_Data.append(HSSFDateUtil
.getJavaDate(
cell.getNumericCellValue())
.toLocaleString().split(" ")[0]);
} else {
excel_Data.append(cell
.getNumericCellValue() + " ");
}
break;
case HSSFCell.CELL_TYPE_ERROR:
excel_Data.append(cell.getErrorCellValue()
+ "!");
break;
case HSSFCell.CELL_TYPE_FORMULA:
excel_Data.append(cell.getCellFormula() + " ");
break;
default:
excel_Data.append("'无法读取'");
break;
}
}
}
excel_Data.append("\n");
} else {
excel_Data.append("ISNULL ");
}
}
} else {
excel_Data.append("工作表不存在");
}
return excel_Data;
}
public static void main(String[] args) throws Exception {
StringBuffer b=inputExcel("F:/work.xls", 0);
System.out.println(b);
}
}