java使用poi读取excel内容方法实例

所属分类: 软件编程 / java 阅读数: 48
收藏 0 赞 0 分享

复制代码 代码如下:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

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 FileOperator {
   public static void main(String[] args) throws Exception {
      String path = "F:\\1.xls";
   print(path);
     }

  public static void print(String path) throws IOException{
      BufferedInputStream in = new BufferedInputStream(
     new FileInputStream(new File(path)));
   POIFSFileSystem fs = new POIFSFileSystem(in);
   HSSFWorkbook wb = new HSSFWorkbook(fs);
   HSSFCell cell = null;
   HSSFSheet st = wb.getSheetAt(0);
   for (int rowIndex = 0; rowIndex <= st.getLastRowNum(); rowIndex++) {
    HSSFRow row = st.getRow(rowIndex);
    if (row == null) {
     continue;
    }
    for (short columnIndex = 0, size = row.getLastCellNum(); columnIndex <= size; columnIndex++) {
     cell = row.getCell(columnIndex);
     String value = "";
     if (cell != null) {
      // 注意:一定要设成这个,否则可能会出现乱码
      cell.setEncoding(HSSFCell.ENCODING_UTF_16);
      switch (cell.getCellType()) {
      case HSSFCell.CELL_TYPE_STRING:
       value = cell.getStringCellValue();
       break;
      case HSSFCell.CELL_TYPE_NUMERIC:
       if (HSSFDateUtil.isCellDateFormatted(cell)) {
        Date date = cell.getDateCellValue();
        if (date != null) {
         value = new SimpleDateFormat("yyyy-MM-dd")
           .format(date);
        } else {
         value = "";
        }
       } else {
        value = new DecimalFormat("0").format(cell
          .getNumericCellValue());
       }
       break;
      case HSSFCell.CELL_TYPE_FORMULA:
       // 导入时如果为公式生成的数据则无值
       if (!cell.getStringCellValue().equals("")) {
        value = cell.getStringCellValue();
       } else {
        value = cell.getNumericCellValue() + "";
       }
       break;
      case HSSFCell.CELL_TYPE_BLANK:
       break;
      case HSSFCell.CELL_TYPE_ERROR:
       value = "";
       break;
      case HSSFCell.CELL_TYPE_BOOLEAN:
       value = (cell.getBooleanCellValue() == true ? "Y"
         : "N");
       break;
      default:
       value = "";
      }
      System.out.println(rowIndex + "," + columnIndex + ":"
        + value);
     }
     if (columnIndex == 0 && value.trim().equals("")) {
      break;
     }
    }
   }
     }

}

更多精彩内容其他人还在看

SpringBoot SpEL语法扫盲与查询手册的实现

这篇文章主要介绍了SpringBoot SpEL语法扫盲与查询手册的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Java创建子线程的两种方法

这篇文章主要介绍了Java创建子线程的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot2.x集成JPA快速开发的示例代码

这篇文章主要介绍了Spring Boot2.x集成JPA快速开发,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

关于Java中的mysql时区问题详解

这篇文章主要给大家介绍了关于Java中mysql时区问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

JAVA参数传递方式实例浅析【按值传递与引用传递区别】

这篇文章主要介绍了JAVA参数传递方式,结合实例形式分析了java按值传递与引用传递区别及相关操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中MessageDigest来实现数据加密的方法

这篇文章主要介绍了Java中MessageDigest来实现数据加密的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

spring 注解验证@NotNull等使用方法

这篇文章主要介绍了spring 注解验证@NotNull等使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

浅谈如何优雅地停止Spring Boot应用

这篇文章主要介绍了浅谈如何优雅地停止Spring Boot应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Python如何使用@property @x.setter及@x.deleter

这篇文章主要介绍了Python如何使用@property @x.setter及@x.deleter,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java Jmeter全局变量设置过程图解

这篇文章主要介绍了Java Jmeter全局变量设置过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多