Apache POI - Java Excel 学习笔记一

1、poi-excel的第一个例子,写一个字符串到excel表

        Workbook wb=new HSSFWorkbook(); 
        Sheet sheet = wb.createSheet("第一个sheet页");
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("1135746");;
        FileOutputStream fileOut=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\my.xls");
        wb.write(fileOut);
        fileOut.close(); 
        wb.close();

2、poi-excel的第二个例子,写不同的类型到excel表

        Workbook wb=new HSSFWorkbook();
        Sheet sheet = wb.createSheet("表一");
        Row row = sheet.createRow(0);   
        Cell cell = row.createCell(0);
        cell.setCellValue(new Date());
        CreationHelper creationHelper=wb.getCreationHelper();
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-MM-dd hh:mm:ss"));
        cell.setCellStyle(cellStyle);
        Cell cell2 = row.createCell(1);
        cell2.setCellValue("我叫王明远");
        Cell cell3 = row.createCell(2);
        cell3.setCellValue(false);
        Cell cell4 = row.createCell(3);
        cell4.setCellValue(Calendar.getInstance());
        cell4.setCellStyle(cellStyle);
        FileOutputStream fileOutputStream=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\my.xls");
        wb.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("输出完成");

3、poi-excel的第三个例子,从excel表中遍历数据

@SuppressWarnings("resource")
    public static void main(String[] args) throws IOException {
        InputStream is = new FileInputStream("C:\\Users\\Administrator\\Desktop\\王明远-月度总结及计划.xls");
        POIFSFileSystem fs = new POIFSFileSystem(is);
        HSSFWorkbook workbook = new HSSFWorkbook(fs);
        HSSFSheet hssfSheet = workbook.getSheetAt(0);// 获得第一个sheet页
        if (hssfSheet == null) {
            return;
        }
        // 遍历行
        for (int i = 0; i < hssfSheet.getLastRowNum(); i++) {
            HSSFRow hssfRow = hssfSheet.getRow(i);
            if (hssfRow == null) {
                continue;
            }
            // 遍历列
            for (int j = 0; j < hssfRow.getLastCellNum(); j++) {
                HSSFCell cell = hssfRow.getCell(j);
                if (cell == null) {
                    continue;
                }
                System.out.println(" "+getValue(cell));
            }
        }
    }

    @SuppressWarnings({ "unused", "deprecation" })
    private static String getValue(HSSFCell cell) {
        if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
            return String.valueOf(cell.getBooleanCellValue());
        } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            return String.valueOf(cell.getNumericCellValue());
        } else {
            return String.valueOf(cell.getStringCellValue());
        }
    }

4、poi-excel的第四个例子,从excel表中用ExcelExtractor类去遍历数据

        InputStream is = new FileInputStream("C:\\Users\\Administrator\\Desktop\\王明远-月度总结及计划.xls");
        POIFSFileSystem fs = new POIFSFileSystem(is);
        HSSFWorkbook workbook = new HSSFWorkbook(fs);
        ExcelExtractor excelExtractor=new ExcelExtractor(workbook);//遍历excel
        excelExtractor.setIncludeSheetNames(false);//不需要sheet名
        System.out.println(excelExtractor.getText());
        workbook.close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值