poi导出excel

点点圈 提交于 2020-04-30 13:53:39

 

一,项目中导入导出excel的操作十分的常见,对于项目中poi总结下

基础知识讲解参考博客:    https://blog.csdn.net/vbirdbest/article/details/72870714,很详细

二,demo

 /**
     * 将数据写入到Excel
     */
    @Test
    public void testUrl() {
        FileOutputStream fileOutputStream = null;
        try {
            String excelPath = "/Users/li/Desktop/courseWork.xlsx";
       
            FileInputStream fileInputStream = new FileInputStream(excelPath);
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("first");
            HSSFRow row = sheet.createRow(0);
            HSSFCell cell = row.createCell(0);
            //HSSFCellStyle
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cell.setCellValue("姓名");
            cell.setCellStyle(cellStyle);
            row.createCell(1).setCellValue("年龄");

            List<StudentEntity> initStudentList = StudentEntity.createInitStudentList();


            for (int rowNum = 0; rowNum < initStudentList.size(); rowNum++) {
                row = sheet.createRow(rowNum + 1);
                StudentEntity studentEntity = initStudentList.get(rowNum);
                row.createCell(0).setCellValue(studentEntity.getUserName());
                row.createCell(1).setCellValue(studentEntity.getAge());
            }

            String writePath = "/Users/li/Desktop/testCourseWorkTTT.xls";
            fileOutputStream = new FileOutputStream(writePath);
            workbook.write(fileOutputStream);
            fileInputStream.close();

        } catch (Exception ex) {
            log.error("错误是", ex);
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

导出数据

/**
     * 从excel中读取数据
     */
    @Test
    public void writeFile() {

        Gson gson = new Gson();
        String readPath = null;
        FileInputStream fileInputStream = null;
        try {
            readPath = "/Users/li/Desktop/testCourseWorkTTT.xls";
            fileInputStream = new FileInputStream(readPath);
            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
            HSSFSheet sheet = workbook.getSheetAt(0);
            log.info("sheetName is:{}", sheet.getSheetName());
            HSSFRow row = sheet.getRow(0);

            List<StudentEntity> list = Lists.newArrayList();

            log.info("第一行是:{},第二行是:{}", row.getCell(0).getStringCellValue(), row.getCell(1).getStringCellValue());
            for (int rowNum = sheet.getFirstRowNum(); rowNum < sheet.getLastRowNum(); rowNum++) {
                row = sheet.getRow(rowNum + 1);
                String userName = row.getCell(0).getStringCellValue();
                Double age = row.getCell(1).getNumericCellValue();
                StudentEntity studentEntity = StudentEntity.builder().userName(userName)
                        .age(age.intValue()).build();
                list.add(studentEntity);
            }
            log.info("读取数据是:{}", gson.toJson(list));
//结果如下:
            //[{"userId":0,"userName":"","age":15},{"userId":0,"userName":"第二个小红","age":12},{"userId":0,"userName":"第三个小红","age":14},{"userId":0,"userName":"第四个小红","age":16}]

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!