博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POI数据导入
阅读量:6998 次
发布时间:2019-06-27

本文共 2603 字,大约阅读时间需要 8 分钟。

hot3.png

使用POI做数据导入首先需要添加POI依赖

org.apache.poi
poi
3.9
org.apache.poi
poi-ooxml
3.9
org.apache.poi
poi-ooxml-schemas
3.9

测试用例

package poitest;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import org.junit.Test;import java.io.FileInputStream;/** * @Author: mol * @Description: * @Date: create in 8:56 2018/3/15 */public class POITest {    private String fileName = "E:\\corner\\下载 (12).xls";    @Test    public void testPOi()throws Exception{        //判断文件是否是Excel表格        if(fileName.endsWith(".xls") || fileName.endsWith(".xlsx")){            //判断Excel版本            boolean is03Excel = fileName.endsWith(".xls");            FileInputStream fileInputStream = new FileInputStream(fileName);            //根据不同的版本创建对应的工作簿            Workbook workbook = is03Excel?new HSSFWorkbook(fileInputStream):new XSSFWorkbook(fileInputStream);            //根据下标获取工作表            Sheet sheet = workbook.getSheetAt(0);            //获取工作表最后一行的索引            int lastRowNum = sheet.getLastRowNum();            System.out.println(lastRowNum);            //循环取出每一行            for(int rowNum = 2;rowNum < sheet.getLastRowNum(); rowNum++){               //获取当前索引行               Row row = sheet.getRow(rowNum);               //获取当前行中第一个单元格row.getCell(0)获取这个单元格的内容row.getCell(0).getStringCellValue()               System.out.println("单位:"+row.getCell(0).getStringCellValue());               //Cell.CELL_TYPE_STRING将单元格的内容设为String类型 否则可能会报 Cannot get a text value from a numeric cell               row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);               System.out.println("已导入学生人数:"+row.getCell(1).getStringCellValue());               row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);               System.out.println("考试人数:"+row.getCell(2).getStringCellValue());               row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);               System.out.println("学习人数:"+row.getCell(3).getStringCellValue());               row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);               System.out.println("及格率:"+row.getCell(4).getStringCellValue());           }        }    }}

 

转载于:https://my.oschina.net/u/3042999/blog/1635096

你可能感兴趣的文章