Java代码操作Excel

创建一个Maven项目

导入依赖

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!--0.3-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.9</version>
    </dependency>
    <!--0.7-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.9</version>
    </dependency>

    <!--时间工具-->
    <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.10.1</version>
    </dependency>

注意 案例皆以0.3版本为例(0.7版本只需要 Workbook workbook = new SXSSFWorkbook();
FileOutputStream outputStream = new FileOutputStream(path + “大数据班分数表07.xlsx”);其他代码不变)

案例一

public static void main(String[] args) throws IOException {
        //1 创建工作簿
        Workbook workbook = new HSSFWorkbook();
        //2 创建表
        Sheet sheet = workbook.createSheet("大数据班");
        //3 创建第第一行
        Row row = sheet.createRow(0);
        //4 创建 第一个单元格(1,1)
        Cell cell = row.createCell(0);
        // 第一个格子填入的数据
        cell.setCellValue("三天的学习成绩平均分:");
        Cell cell1 = row.createCell(1);
        // 第二个格子填入的数据
        cell1.setCellValue("89");

        //创建第二行数据(2,1)
        Row row1 = sheet.createRow(1);
        //4 创建 第一个单元格(2,1)
        Cell cell2 = row1.createCell(0);
        // 第一个格子填入的数据
        cell2.setCellValue("统计时间:");
        Cell cell3 = row1.createCell(1);
        // 第二个格子填入的数据
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell3.setCellValue(time);

        // 生成表(io 流)
        FileOutputStream outputStream = new FileOutputStream(path + "大数据班分数表03.xls");

        //输出
        workbook.write(outputStream);
        outputStream.close();
        System.out.println("生成完成!");






    }

生成文件

在这里插入图片描述
在这里插入图片描述

案例二

  private static  String path = "src/main/java";

    public static void main(String[] args) throws IOException {
        long begin = System.currentTimeMillis();
        //创建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //创建表
        Sheet sheet = workbook.createSheet();
        for(int i=0;i<65536;i++){
            Row row = sheet.createRow(i);
            for(int j=0;j<10;j++){
                Cell cell = row.createCell(j);
                cell.setCellValue(j);
            }
        }
        FileOutputStream fileOutputStream = new FileOutputStream(path+"data.xls");
        //输出
        workbook.write(fileOutputStream);

        fileOutputStream.close();
        long end = System.currentTimeMillis();

        System.out.println("执行所花费的时间:"+(double)(end-begin)/1000);
    }

效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
注意:0.3版本最多有65563行数据
0.7版本你可认为有无数行,前者速度快后者慢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

缘不易

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值