package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelTest {
public static void main(String[] args) {
// POIFSFileSystem fs = null;
// HSSFWorkbook wb = null;
// try {
// fs = new POIFSFileSystem(new FileInputStream(""));
// wb = new HSSFWorkbook(fs);
// } catch (IOException e) {
// e.printStackTrace();
// }
HSSFWorkbook workbook;
HSSFSheet sheet;
HSSFRow row;
HSSFCell cell = null;
int columnNo;// 列数
File file=new File("E:/excel.xls");
FileInputStream fis=null;
try {
fis=new FileInputStream(file);
workbook = new HSSFWorkbook(fis);
//获取第一个表单
sheet = workbook.getSheetAt(0);
//获取第2行 行对象
row = sheet.getRow(1);
//顺次读取单元格数据
cell = row.getCell(0);
//文本类型
String str=cell.getStringCellValue();
System.out.println(str);
cell = row.getCell(1);
//数字类型
int num=(int)cell.getNumericCellValue();
System.out.println("num:"+num);
long longn=(long)cell.getNumericCellValue();
System.out.println("longn:"+longn);
float floatn=(float)cell.getNumericCellValue();
System.out.println("floatn:"+floatn);
//日期类型
cell = row.getCell(2);
System.out.println(cell.getDateCellValue());
String strFormat = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
try {
String entryPersonDate = sdf.format(cell.getDateCellValue());
System.out.println(entryPersonDate);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//时间类型
cell = row.getCell(3);
// HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
// cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("hh:mm:ss")); // 设置cell样式为定制的日期格式
// cell.setCellStyle(cellStyle); // 设置该cell日期的显示格式
// System.out.println(cell.getDateCellValue());
String str1 = "hh:mm:ss";
SimpleDateFormat sdf1 = new SimpleDateFormat(str1);
try {
System.out.println(cell.getDateCellValue());
String entryPersonDate = sdf1.format(cell.getDateCellValue());
System.out.println(entryPersonDate);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}