POI操作EXCEL文档使用及踩坑

java POI使用及踩坑

1. POI的下载

下载地址
https://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-4.1.2-20200217.zip

在这里插入图片描述
随便选择一个镜像地址下载。

如果是使用的maven则可以到maven仓库复制一下
https://mvnrepository.com/artifact/org.apache.poi/poi/4.1.2

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>

2. 导入包(使用包)

如果你是使用的maven,那就不要看这个了

使用poi需要两个jar包 poi-xxx.jar和commons-mathxxx.jar
在这里插入图片描述

在这里插入图片描述

3. 开始使用POI(正片)

我们这里就只使用POI来操作excel

  1. 创建一个工作簿和表
public static void main(String[] args) throws IOException {
        //创建一个工作蒲
        Workbook workbook = new HSSFWorkbook();
        //创建一张表
        Sheet sheet = workbook.createSheet("表的名字");

        //创建一个写入流
        try (FileOutputStream outputStream = new FileOutputStream("a.xls")){
            workbook.write(outputStream);
        }
    }
  }

项目运行后,会在当前的项目下面创建一个空的excel表格 a.xls

  1. 设置行和列,并且写入
public static void main(String[] args) throws IOException {
        //创建一个工作蒲
        Workbook workbook = new HSSFWorkbook();
        //创建一张表
        Sheet sheet = workbook.createSheet("表的名字");

        //创建一个写入流
        try (FileOutputStream outputStream = new FileOutputStream("a.xls")){
            

            Row row0 = sheet.createRow(0);
            row0.createCell(0).setCellValue("这个第一个行的一个单元格");
            workbook.write(outputStream);
        }

    }
  1. 设置单元格的样式
    这里就来一个背景颜色和粗体字
 public static void main(String[] args) throws IOException {
        //创建一个工作蒲
        Workbook workbook = new HSSFWorkbook();
        //创建一张表
        Sheet sheet = workbook.createSheet("表的名字");

        //创建一个style
        CellStyle style = workbook.createCellStyle();
        //设置水平居中
        style.setAlignment(HorizontalAlignment.CENTER);
        //设置黄色的背景颜色
        style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        //设置背景颜色为铺满
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        //设置字体为粗体
        HSSFFont font = (HSSFFont) workbook.createFont();
        font.setBold(true);
        style.setFont(font);


        //创建一个写入流
        try (FileOutputStream outputStream = new FileOutputStream("a.xls")){
           

            Row row0 = sheet.createRow(0);
            Cell cell00 = row0.createCell(0);
            cell00.setCellStyle(style);
            cell00.setCellValue("这个是有背景颜色的");

            Cell cell01 = row0.createCell(1);
            cell01.setCellValue("这个是没有背景颜色的");
            workbook.write(outputStream);

        }

    }

这个时候运行,然后打开a.xls
在这里插入图片描述
4. 使用公式
对于Excel的老玩家的来说,使用公式,肯定是很舒服的。
比如下面来一个求和、、求平均数例子
代码有一点点多,但是不难

public static void main(String[] args) throws IOException {
        //创建一个工作蒲
        Workbook workbook = new HSSFWorkbook();
        //创建一张表
        Sheet sheet = workbook.createSheet("表的名字");

        sheet.setColumnWidth(0,15000);


        //设置第一列和第二列为默认居中
        CellStyle defaultStyle =  workbook.createCellStyle();
        defaultStyle.setAlignment(HorizontalAlignment.CENTER);

        sheet.setDefaultColumnStyle(0,defaultStyle);
        sheet.setDefaultColumnStyle(1,defaultStyle);

        //创建一个style
        CellStyle style = workbook.createCellStyle();
        //设置水平居中
        style.setAlignment(HorizontalAlignment.CENTER);
        //设置黄色的背景颜色
        style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        //设置背景颜色为铺满

        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        //设置字体
        HSSFFont font = (HSSFFont) workbook.createFont();
        font.setFontName("Arial");
        font.setFontHeightInPoints((short) 10);
        font.setFontHeight((short)500);
        font.setBold(true);
        font.setColor(IndexedColors.RED.getIndex());
        style.setFont(font);


        //创建一个写入流
        try (FileOutputStream outputStream = new FileOutputStream("a.xls")){


            Row row0 = sheet.createRow(0);
            Cell cell00 = row0.createCell(0);
            cell00.setCellStyle(style);
            cell00.setCellValue("序号");

            Cell cell01 = row0.createCell(1);
            cell01.setCellValue("成绩");


            //创建几行用来搞数字
            Row row1 = sheet.createRow(1);
            Row row2 = sheet.createRow(2);
            Row row3 = sheet.createRow(3);
            Row row4 = sheet.createRow(4);
            Row row5 = sheet.createRow(5);
            //然后创建每一行的第一个单元格
            Cell cell10 = row1.createCell(0);
            Cell cell20 = row2.createCell(0);
            Cell cell30 = row3.createCell(0);
            Cell cell40 = row4.createCell(0);
            Cell cell50 = row5.createCell(0);

            Cell cell11 = row1.createCell(1);
            Cell cell21 = row2.createCell(1);
            Cell cell31 = row3.createCell(1);
            Cell cell41 = row4.createCell(1);
            Cell cell51 = row5.createCell(1);

            //写入序号
            cell10.setCellValue(1);
            cell20.setCellValue(2);
            cell30.setCellValue(3);
            cell40.setCellValue(4);
            cell50.setCellValue(5);

            //写入成绩
            cell11.setCellValue(10);
            cell21.setCellValue(20);
            cell31.setCellValue(30);
            cell41.setCellValue(20);
            cell51.setCellValue(10);


            //最后再创建一个单元格来写和

            Row rowSum = sheet.createRow(sheet.getLastRowNum()+1);
            rowSum.createCell(0).setCellValue("成绩总和");
            rowSum.createCell(1).setCellFormula("SUM(B2:A6)");

            Row rowAverage = sheet.createRow(rowSum.getRowNum()+1);
            rowAverage.createCell(0).setCellValue("成绩平均数");
            rowAverage.createCell(1).setCellFormula("AVERAGE(B2:B7)");



            //这个一定要记得写在后面,要不然会出现无法写入的问题(打开excel为空)
            workbook.write(outputStream);
        }
        System.out.println("执行完成。。。。");
    }

效果如下。
在这里插入图片描述

注意!!!
workbook.write(outputStream); 这句代码一定要写在后面,要不然会出现打开测试的excel为空的问题。

4. 读取Excel文档

还是接着上面写的代码
下面的代码的意思是读取第一张表的第一行的,然后打印出来

 public static void main(String[] args) throws IOException {
        //读取需要使用到字节输入流
        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File("a.xls")));

        //获取第一张表
        HSSFSheet sheet0 = workbook.getSheetAt(0);

        HSSFRow row = sheet0.getRow(0);
        for (int i = 0; i < (int) row.getLastCellNum(); i++) {
            String cellValue = row.getCell(i).getStringCellValue();
            System.out.println(cellValue);
        }

    }

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值