zoukankan      html  css  js  c++  java
  • POI Workbook接口和HSSFWorkbook对象和XSSFWorkbook对象操作相应excel版本

    由于HSSFWorkbook只能操作excel2003一下版本,XSSFWorkbook只能操作excel2007以上版本,所以利用Workbook接口创建对应的对象操作excel来处理兼容性

    @Test
    public void test6() throws Exception{
    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream( new File("c://123.xls")));
    HSSFSheet sheet = workbook.getSheetAt(0);
    HSSFRow row =sheet.getRow(0);
    HSSFCell cell= row.getCell(0);
    System.out.println(cell.toString());
    }

    @Test
    public void test7() throws Exception{
    XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream( new File("c://456.xlsx")));
    XSSFSheet sheet = workbook.getSheetAt(0);
    XSSFRow row =sheet.getRow(0);
    XSSFCell cell= row.getCell(0);
    System.out.println(cell.toString());
    }

    @Test //利用Workbook接口和判断excel版本创建相应版本HSSFWorkbook/XSSFWorkbook对象
    public void test8() throws Exception{
    String file = "c://456.xlsx";
    boolean isExcel2003 = file.toLowerCase().endsWith("xls")?true:false;
    Workbook workbook = null;
    if(isExcel2003){
    workbook = new HSSFWorkbook(new FileInputStream(new File(file)));
    }else{
    workbook = new XSSFWorkbook(new FileInputStream(new File(file)));
    }
    Sheet sheet = workbook.getSheetAt(0);
    Row row =sheet.getRow(0);
    Cell cell= row.getCell(0);
    System.out.println(cell.toString());
    }

  • 相关阅读:
    【制作镜像Win*】系统配置
    【制作镜像Win*】系统安装
    【制作镜像Win*】文件准备
    【制作镜像Win*】环境准备
    【制作镜像Win*】环境准备(设置yum源)
    Portal
    bc
    2-模拟登录淘宝
    10-天猫订单数据分析
    2-Scala进阶
  • 原文地址:https://www.cnblogs.com/chenweichu/p/5590347.html
Copyright © 2011-2022 走看看