JAVA读取Excel任何格式的内容

来源:互联网 发布:双系统ubuntu安装教程 编辑:程序博客网 时间:2024/05/20 09:25

1.Maven XML文件:

<dependencies>    <dependency>        <groupId>net.sf.opencsv</groupId>        <artifactId>opencsv</artifactId>        <version>2.1</version>    </dependency>    <dependency>        <groupId>org.apache.poi</groupId>        <artifactId>ooxml-schemas</artifactId>        <version>1.1</version>        <type>pom</type>    </dependency>    <dependency>        <groupId>org.apache.poi</groupId>        <artifactId>poi</artifactId>        <version>3.9</version>    </dependency>    <dependency>        <groupId>org.apache.poi</groupId>        <artifactId>ooxml-schemas</artifactId>        <version>1.1</version>    </dependency>    <dependency>        <groupId>org.apache.poi</groupId>        <artifactId>poi-ooxml</artifactId>        <version>3.9</version>    </dependency>    <dependency>        <groupId>dom4j</groupId>        <artifactId>dom4j</artifactId>        <version>1.6.1</version>    </dependency>
2.java 代码
//读取Excel内容的方法,//输入值Row row = sheet.getRow(rIndex),rIndex等于行号减1,若不清楚请参考下面Row的来源//输入值String column,为列号,如A//返回值是excel单元格内容public String getNumThrColumnComplex(Row row, String column) {    MethodUsedImp methodUsedImp = new MethodUsedImp();    int cIndex = methodUsedImp.nameToColumn(column);//将列号转为对应的数字,如A列对应0    Cell cell = null;    if (row != null && cIndex != -1) {        cell = row.getCell(cIndex);    } else {        return null;    }    if(cell != null) {        switch (cell.getCellType()) {            case HSSFCell.CELL_TYPE_NUMERIC:                return String.valueOf(cell.getNumericCellValue());            case HSSFCell.CELL_TYPE_STRING:                return String.valueOf(cell.getRichStringCellValue());            case HSSFCell.CELL_TYPE_ERROR:                return String.valueOf(cell.getErrorCellValue());            case HSSFCell.CELL_TYPE_FORMULA:                try {                    return String.valueOf(cell.getNumericCellValue());                } catch (IllegalStateException e) {                    return String.valueOf(cell.getRichStringCellValue());                }            case HSSFCell.CELL_TYPE_BLANK:                return null;            default:                System.out.println(cell.getCellType());                return null;        }    }else        return null;}public int nameToColumn(String name) {    int column = -1;    if(name != null){        for (int i = 0; i < name.length(); ++i) {            int c = name.charAt(i);            column = (column + 1) * 26 + c - 'A';        }        return column;    }    return -1;}Row的来源:File file = new File(inputpath);//inputpath是输入目录Workbook wb = null;try {    InputStream inputStream = new FileInputStream(file);    String fileName = file.getName();    if (fileName.endsWith("xls")) {        wb = new HSSFWorkbook(inputStream); // 解析xls格式    } else if (fileName.endsWith("xlsx")) {        wb = new XSSFWorkbook(inputStream); // 解析xlsx格式    }else if (fileName.endsWith("xlsm")){        wb = new XSSFWorkbook(inputStream);    }}  catch (FileNotFoundException e) {    e.printStackTrace();} catch (IOException e) {    System.out.println("请查看输入文件格式是否有误");    e.printStackTrace();}sheet = wb.getSheet(temps_9.getSheet());//temps_9.getSheet()是表名Row row = sheet.getRow(rIndex);
 
原创粉丝点击