读取excel标题、内容

来源:互联网 发布:网络政治参与图片 编辑:程序博客网 时间:2024/06/11 11:49

首先添加两个包,在pom.xml里面加入:

<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version></dependency>        <dependency>            <groupId>org.apache.poi</groupId>            <artifactId>poi-ooxml-schemas</artifactId>            <version>3.9</version>        </dependency>

干货:

/** * 读取excel标题、内容 */public void findCompanyIndustry() {    List<Industry> list = industryRepository.findAll();    System.out.println(list);    Map<Integer, String> map = new HashMap<Integer, String>();    Workbook workbook = null;    String str = "";    try {        InputStream in = new FileInputStream("d:\\项目资料\\测试行业.xlsx");        try {            workbook = WorkbookFactory.create(in);        } catch (IOException e) {            throw new ServiceException(ResultEnum.ERROR.getCode(), "saveIndustry方法IOException");        } catch (InvalidFormatException e) {            throw new ServiceException(ResultEnum.ERROR.getCode(), "saveIndustry方法InvalidFormatException");        }        Sheet sheet = workbook.getSheetAt(0);        Row row = sheet.getRow(0);        // 标题总列数        int colNum = row.getPhysicalNumberOfCells();        System.out.println("colNum:" + colNum);        String[] title = new String[colNum];        for (int i = 0; i < colNum; i++) {            title[i] = getCellFormatValue(row.getCell((short) i));        }        System.out.println("获得Excel表格的标题:");        for (String s : title) {            System.out.print(s + " ");        }        System.out.println("");        int rowNum = sheet.getLastRowNum();        // 正文内容应该从第二行开始,第一行为表头的标题        for (int i = 1; i <= rowNum; i++) {            row = sheet.getRow(i);            int j = 0;            while (j < colNum) {                // 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据                // 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean                // str += getStringCellValue(row.getCell((short) j)).trim() +                // "-";                str += getCellFormatValue(row.getCell((short) j)).trim() + "-";                j++;            }            map.put(i, str);            str = "";        }        System.out.println("获得Excel表格的内容:");        for (int i = 1; i <= map.size(); i++) {            System.out.println(map.get(i));        }        saveCompanyIndustry(list, map);    } catch (FileNotFoundException e) {        throw new ServiceException(ResultEnum.ERROR.getCode(), "saveIndustry方法IFileNotFoundException");    }}/** * 根据HSSFCell类型设置数据 * * @param cell * @return */private String getCellFormatValue(Cell cell) {    String cellvalue = "";    if (cell != null) {        // 判断当前Cell的Type        switch (cell.getCellType()) {            // 如果当前Cell的Type为NUMERIC            case Cell.CELL_TYPE_NUMERIC:            case Cell.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 Cell.CELL_TYPE_STRING:                // 取得当前的Cell字符串                cellvalue = cell.getRichStringCellValue().getString();                break;            // 默认的Cell值            default:                cellvalue = " ";        }    } else {        cellvalue = "";    }    return cellvalue;}
控制台输出:colNum:5获得Excel表格的标题:发行人中文名称 主体评级 一级行业 二级行业 三级行业 获得Excel表格的内容:冀中能源峰峰集团有限公司    AA    原材料    煤炭    煤炭    阳泉煤业(集团)有限责任公司    AAA    原材料    煤炭    煤炭    山东能源集团有限公司    AAA    原材料    煤炭    煤炭    山西焦煤集团有限责任公司    AAA    原材料    煤炭    煤炭    晋能集团有限公司    AAA    原材料    煤炭    煤炭    淮南矿业(集团)有限责任公司    AAA    原材料    煤炭    煤炭    山西潞安矿业(集团)有限责任公司    AAA    原材料    煤炭    煤炭    开滦(集团)有限责任公司    AA+    原材料    煤炭    煤炭    


1 0
原创粉丝点击