【Java】 使用POI对Excel进行读写操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/loy_184548/article/details/80096788

使用POI对Excel进行读写操作


参考地址:here and here

1 . 下载JAR包并导入

下载地址:here

a. 新建lib文件夹,将poi-3.17.jar以及poi-ooxml-3.17放进入
b. 在Maven中加入

<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <scope>system</scope>
   <version>3.17</version>
   <systemPath>${project.basedir}/lib/poi-3.17.jar</systemPath>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <scope>system</scope>
   <version>3.17</version>
   <systemPath>${project.basedir}/lib/poi-ooxml-3.17.jar</systemPath>
</dependency>
  1. 代码部分
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadWriteExcel {


    private static final String EXCEL_XLS = "xls";
    private static final String EXCEL_XLSX = "xlsx";

    // 判断Excel的版本,获取Workbook
    public static Workbook getWorkbok(InputStream in, File file) throws IOException {
        Workbook wb = null;
        if(file.getName().endsWith(EXCEL_XLS)){  //Excel 2003
            wb = new HSSFWorkbook(in);
        }else if(file.getName().endsWith(EXCEL_XLSX)){  // Excel 2007/2010
            wb = new XSSFWorkbook(in);
        }
        return wb;
    }

    // 判断文件是否为excel
    public static void checkExcelVaild(File file) throws Exception{
        if(!file.exists()){
            throw new Exception("文件不存在");
        }
        if(!(file.isFile() && (file.getName().endsWith(EXCEL_XLS) || file.getName().endsWith(EXCEL_XLSX)))){
            throw new Exception("文件不是Excel");
        }
    }

    // 读取excel内容
    public static void main(String[] args) throws Exception {

        File excelFile = new File("/Users/ouyalin/Desktop/2014211225.xls"); // 创建文件对象
        FileInputStream is = new FileInputStream(excelFile); // 文件流

        checkExcelVaild(excelFile);
        Workbook workbook = getWorkbok(is,excelFile);

        Sheet sheet = workbook.getSheetAt(0);   // 工作表对象
        int rowLength = sheet.getLastRowNum()+1; //总行数
        Row row = sheet.getRow(0); //工作表的列
        int colLength = row.getLastCellNum(); //总列数
        //得到指定的单元格
        Cell cell = row.getCell(0);
        CellStyle cellStyle = cell.getCellStyle(); //得到单元格样式
        for (int i = 0; i < rowLength; i++) {
            row = sheet.getRow(i);
            for (int j = 0; j < colLength; j++) {
                cell = row.getCell(j);   // 获得指定单元格

                /*
                 * 时间格式比如 2014-02-01,cell的toString是 01-feb-2014
                 * 就用new SimpleDateFormat("dd-MMM-yyyy").parse(cell.toString())来解决
                 */

                if (cell != null)  {
                    cell.setCellType(CellType.STRING);      //将所有的需要读的Cell表格设置为String格式
                    System.out.print(cell.getStringCellValue() + "\t");     // 获取单元格内容
                }
            }
            System.out.println();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/loy_184548/article/details/80096788