java从excle中读取数据(poi)

本文介绍了一种使用Java Apache POI库读取Excel文件的方法。通过具体代码示例展示了如何初始化工作簿、获取表格内容及处理单元格格式。适用于需要批量处理Excel文件的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值