poi解析Excel文档

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();
   }
  }
  
  
 }

 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值