java从excle中读取数据(poi)

来源:互联网 发布:小偷公司知乎 编辑:程序博客网 时间:2024/05/16 17:32

1.导入poi-3.6.jar

package utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ExcleReader {
 private POIFSFileSystem fs;
 private HSSFWorkbook wb;
 private HSSFSheet sheet;
 private HSSFRow row;
//成功
 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  ExcleReader ei = new ExcleReader();
  ei.excelImport();
 }

 public void excelImport() throws Exception {
  try {
   // 对读取Excel表格标题测试
   ExcleReader excelReader = new ExcleReader();
   InputStream is2 = new FileInputStream("F:\\Book1.xls");
   excelReader.readExcelContent(is2);
  } catch (FileNotFoundException e) {
   System.out.println("未找到指定路径的文件!");
   e.printStackTrace();
  }
 }

 public void readExcelContent(InputStream is) throws Exception {

  //String subname = "DB01202000";
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  List<String> nolist = new ArrayList<String>();
  int a = 0;
  try {
   fs = new POIFSFileSystem(is);
   wb = new HSSFWorkbook(fs);
  } catch (IOException e) {
   e.printStackTrace();
  }
  sheet = wb.getSheetAt(0);
  // 得到总行数
  int rowNum = sheet.getLastRowNum();
  row = sheet.getRow(0);
  int colNum = row.getPhysicalNumberOfCells();
  // 正文内容应该从第二行开始,第一行为表头的标题
  for (int i = 1; i <=rowNum; i++) {
   row = sheet.getRow(i);
   int j = 0;
  
   String ch=getCellFormatValue(row.getCell((short) 0)).trim();
   String en=getCellFormatValue(row.getCell((short) 1)).trim();
   System.out.println(ch);
   System.out.println(en);
   
  }
//   if (capturetime.indexOf("-") != -1) {
//    if(capturetime.length()>10){
//     capturetime=capturetime.substring(capturetime.length()-10, capturetime.length());
//    }
//    pr.setCapturetime(sdf.parse(capturetime));
//   }
 }

 private String getCellFormatValue(HSSFCell cell) {
  String cellvalue = "";
  if (cell != null) {
   // 判断当前Cell的Type
   switch (cell.getCellType()) {
   // 如果当前Cell的Type为NUMERIC
   case HSSFCell.CELL_TYPE_NUMERIC:
   case HSSFCell.CELL_TYPE_FORMULA: {
    // 判断当前的cell是否为Date
    if (HSSFDateUtil.isCellDateFormatted(cell)) {
     // 如果是Date类型则,转化为Data格式

     // 方法1:这样子的data格式是带时分秒的:2011-10-12 0:00:00
     // cellvalue = cell.getDateCellValue().toLocaleString();

     // 方法2:这样子的data格式是不带带时分秒的:2011-10-12
     Date date = cell.getDateCellValue();
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
     cellvalue = sdf.format(date);
    }
    // 如果是纯数字
    else {
     // 取得当前Cell的数值
     cellvalue = String.valueOf(cell.getNumericCellValue());
    }
    break;
   }
    // 如果当前Cell的Type为STRIN
   case HSSFCell.CELL_TYPE_STRING:
    // 取得当前的Cell字符串
    cellvalue = cell.getRichStringCellValue().getString();
    break;
   // 默认的Cell值
   default:
    cellvalue = " ";
   }
  } else {
   cellvalue = "";
  }
  return cellvalue;
 }
 public boolean checktime(String picktime, String pubdate) {
  boolean res = false;
  if (!picktime.trim().equals("") && !pubdate.trim().equals("")) {
   if (picktime.indexOf("0000") == -1 && pubdate.indexOf("0000") == -1) {
    res = true;
   }
  }
  return res;
 }
}

 

原创粉丝点击