Java用POI操作Excel | 狂神

小文件写出

 03版本的excel:用HSSFWorkbook

public class excelWriteTest {

    static String PATH ="D:\\springbootProject\\poi\\src\\main\\java";

    @Test
    public void testWrite03() throws IOException {
        //创建工作本
        Workbook workbook=new HSSFWorkbook();
        //创建工作表
        Sheet sheet = workbook.createSheet("我能快速上手项目");
        //创建行(第一行用0表示
        Row row1 = sheet.createRow(0);
        //创建单元格
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("我能快速上手");
        //第二行
        Row row2=sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        Cell cell22=row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);

        //生成一张表(io)
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "pzh学习表.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("生成完毕");
    }
}

07版本主要是对象不一样硬XSSF

大数据写出

03版本的Excel的最多只能处理65536行,否则抛出异常

03这个是批量的导入过程写入缓存,不操作磁盘,最后一次写入磁盘,速度快

public class excelWriteTest {

    static String PATH ="D:\\springbootProject\\poi\\src\\main\\java";

    @Test
    public void testWrite03() throws IOException {
        //创建工作本
        Workbook workbook=new HSSFWorkbook();
        //创建工作表
        Sheet sheet = workbook.createSheet("我能快速上手项目");
        //创建行(第一行用0表示
        Row row1 = sheet.createRow(0);
        //创建单元格
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("我能快速上手");
        //第二行
        Row row2=sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        Cell cell22=row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);

        //生成一张表(io)
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "pzh学习表.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("生成完毕");
    }

    @Test
    public void testWrite03BigData() throws IOException {
        //时间
        long begin = System.currentTimeMillis();

        //创建本
        Workbook workbook = new HSSFWorkbook();

        //创建表
        Sheet sheet = workbook.createSheet(); //不写用默认名字;

        //写入数据
        for(int rowNum=0;rowNum<65536;rowNum++){
            Row row = sheet.createRow(rowNum);
            for(int cellNum=0;cellNum<=10;cellNum++){
                Cell cell=row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("over");
        FileOutputStream outputStream = new FileOutputStream(PATH + "testWrite03.xls");
        workbook.write(outputStream);
        outputStream.close();
        long end=System.currentTimeMillis();
        System.out.println((double) (end-begin)/1000);
    }
}

读取导入

注意这个读取的时候,hssfwordbook的构造器要传入流,在cell.get的时候要注意类型(很坑这里)

@Test
    public void testRead03() throws IOException {
        //获取流
        FileInputStream fileInputStream = new FileInputStream(PATH + "javapzh学习表.xls");
        Workbook workbook = new HSSFWorkbook(fileInputStream);
        //拿到这种表
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(0);
        Cell cell = row.getCell(0);
        System.out.println(cell.getStringCellValue());
        fileInputStream.close();
    }

读取不同类型

//读取不同类型的数据
public class ReadDifferentType {

    //excel文件路径
    static String path = "C:\\Users\\lenovo\\Desktop\\idea_workspace\\springboot_poi\\";

    public static void main(String[] args) throws Exception {

        readDifferentType();
    }

    public static void readDifferentType() throws Exception{
        //1.创建流来获得excel表
        FileInputStream fileInputStream = new FileInputStream(path + "07版本表.xlsx");
        //2.读取excel工作簿
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        //3.读取第一张表
        Sheet sheet = workbook.getSheetAt(0);
        //4.读取第一行
        Row row_1 = sheet.getRow(0);
        //5.输出第一行的内容
        if(row_1 != null) {
            //获取这行的个数
            int cellCount = row_1.getPhysicalNumberOfCells();
            for (int i = 0; i < cellCount ; i++){
                Cell cell = row_1.getCell(i);
                if(cell != null){
                    //标题都是String,这里不需要判断
                    int cellType = cell.getCellType();
                    String cellValue = cell.getStringCellValue();
                    System.out.println("第1行第"+(i+1)+"列,值为:"+cellValue);
                }
            }
        }
        System.out.println("============");
        //6.获取表中的内容
            //获得行的数目
        int rowCount = sheet.getPhysicalNumberOfRows();
        for(int rowNum = 1; rowNum < rowCount; rowNum++){
            Row rowData = sheet.getRow(rowNum);
            if(rowData != null){
                //获得该行的列的数目
                int lineCount = rowData.getPhysicalNumberOfCells();
                for(int lineNum = 0; lineNum < lineCount ;lineNum++){
                    System.out.print("第" + rowNum +"行,第" + lineNum +"列:");
                    Cell cell = rowData.getCell(lineNum);
                    //判断数据类型
                    if(cell != null){
                        int cellType = cell.getCellType();
                        switch (cellType){
                            case XSSFCell.CELL_TYPE_STRING:
                                System.out.println("字符串:" + cell.getStringCellValue()); break;
                            case XSSFCell.CELL_TYPE_BOOLEAN:
                                System.out.println("布尔:" + cell.getBooleanCellValue());  break;
                            case XSSFCell.CELL_TYPE_BLANK:
                                System.out.println("空"); break;
                            case XSSFCell.CELL_TYPE_ERROR:
                                System.out.println("没有该数据类型"); break;
                            case XSSFCell.CELL_TYPE_NUMERIC:
                                //如果是日期就直接输出,否则就装换为String,然后输出
                                if(HSSFDateUtil.isCellDateFormatted(cell)){
                                    System.out.println("日期格式:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                            .format(new Date(String.valueOf(cell.getDateCellValue())))); break;
                                }else{
                                    cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                                    System.out.println("整型:" + cell.toString()); break;
                            }
                            case Cell.CELL_TYPE_FORMULA:
                                String formula = cell.getCellFormula();
                                System.out.println("公式:" + formula); break;
                            default:
                                System.out.println("该位置没有数据");break;
                        }
                    }
                }
            }
        }
        fileInputStream.close();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_54232666/article/details/129540169