POI读写操作excel

参考链接:

1. 引入pom依赖

 <dependencies>
     <!--xls(03)-->
     <dependency>
         <groupId>org.apache.poi</groupId>
         <artifactId>poi</artifactId>
         <version>3.9</version>
     </dependency>
     <!--xlsx(07)-->
     <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>
     <!--test-->
     <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.12</version>
     </dependency>
 </dependencies>

2. 写入excel

步骤:工作薄---工作表---行---列

2.1 HSSFWorkbook写入2003版xls格式excel(最多65535条)

 package excel;
 ​
 import cn.hutool.core.date.DateTime;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
 ​
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.Date;
 ​
 /**
  * @ClassName HssfWrite
  * @Description TODO
  * @Author Jiangnan Cui
  * @Date 2023/3/5 10:24
  * @Version 1.0
  */
 public class HssfWrite {
     public static void main(String[] args) {
         String path = "D:\\excel\\poi\\";
         // 1. 创建一个工作薄
         Workbook workbook = new HSSFWorkbook();
         // 2. 在工作簿中创建一个工作表
         Sheet sheet = workbook.createSheet("xls-test");
         // 3.在工作表中创建第1行
         Row row1 = sheet.createRow(0);
         // 在第1行创建第11单元格
         Cell cell11 = row1.createCell(0);
         // 在11单元格写入数据
         cell11.setCellValue("小崔");
         // 在第1行创建第12单元格
         Cell cell12 = row1.createCell(1);
         // 在12单元格写入数据
         cell12.setCellValue("加油");
         // 4.在工作表中创建第2行
         Row row2 = sheet.createRow(1);
         // 在第2行创建第21单元格
         Cell cell21 = row2.createCell(0);
         // 在21单元格写入数据
         cell21.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
         // 在第2行创建第22单元格
         Cell cell22 = row2.createCell(1);
         // 在22单元格写入数据
         cell22.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
 ​
         try {
             // 5.创建文件输出流
             FileOutputStream fos = new FileOutputStream(path + "03.xls");
             // 6.将工作薄写入文件
             workbook.write(fos);
             // 7.关闭流
             fos.close();
             System.out.println("03版excel写入完毕!");
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
     }
 }

2.1.1 大数据写入(最多65535条)

 package excel;
 ​
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
 ​
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 ​
 /**
  * @ClassName HssfWriteBigData
  * @Description TODO
  * @Author Jiangnan Cui
  * @Date 2023/3/5 10:59
  * @Version 1.0
  */
 public class HssfWriteBigData {
     public static void main(String[] args) throws IOException {
         String path = "D:\\excel\\poi\\";
         // 时间
         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 fos = new FileOutputStream(path +"big-03.xls");
         workbook.write(fos);
         // 关闭流
         fos.close();
         System.out.println("03版excel写入成功");
 ​
         long end=System.currentTimeMillis();
         System.out.println("总耗时:"+(double)(end-begin)/1000);
     }
 }
 ​

2.2 XSSFWorkbook写入2007版xlsx格式excel(最多20万条)

 package excel;
 ​
 import cn.hutool.core.date.DateTime;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 ​
 import java.io.FileOutputStream;
 import java.io.IOException;
 ​
 /**
  * @ClassName HssfWrite
  * @Description TODO
  * @Author Jiangnan Cui
  * @Date 2023/3/5 10:24
  * @Version 1.0
  */
 public class XssfWrite {
     public static void main(String[] args) {
         String path = "D:\\excel\\poi\\";
         // 1. 创建一个工作薄
         Workbook workbook = new XSSFWorkbook();
         // 2. 在工作簿中创建一个工作表
         Sheet sheet = workbook.createSheet("xlsx-test");
         // 3.在工作表中创建第1行
         Row row1 = sheet.createRow(0);
         // 在第1行创建第11单元格
         Cell cell11 = row1.createCell(0);
         // 在11单元格写入数据
         cell11.setCellValue("小崔");
         // 在第1行创建第12单元格
         Cell cell12 = row1.createCell(1);
         // 在12单元格写入数据
         cell12.setCellValue("加油");
         // 4.在工作表中创建第2行
         Row row2 = sheet.createRow(1);
         // 在第2行创建第21单元格
         Cell cell21 = row2.createCell(0);
         // 在21单元格写入数据
         cell21.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
         // 在第2行创建第22单元格
         Cell cell22 = row2.createCell(1);
         // 在22单元格写入数据
         cell22.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
 ​
         try {
             // 5.创建文件输出流
             FileOutputStream fos = new FileOutputStream(path + "07.xlsx");
             // 6.将工作薄写入文件
             workbook.write(fos);
             // 7.关闭流
             fos.close();
             System.out.println("07版excel写入完毕!");
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
     }
 }

2.2.1 大数据写入(最多20万条)

 package excel;
 ​
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 ​
 import java.io.FileOutputStream;
 import java.io.IOException;
 ​
 /**
  * @ClassName HssfWriteBigData
  * @Description TODO
  * @Author Jiangnan Cui
  * @Date 2023/3/5 10:59
  * @Version 1.0
  */
 public class XssfWriteBigData {
     public static void main(String[] args) throws IOException {
         String path = "D:\\excel\\poi\\";
         // 时间
         long begin = System.currentTimeMillis();
         // 创建一个工作簿
         Workbook workbook = new XSSFWorkbook();
         // 创建一个表
         Sheet sheet = workbook.createSheet();
         // 写入数据
         for (int rowNum = 0; rowNum < 100000 ; 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 fos = new FileOutputStream(path +"big-07.xlsx");
         workbook.write(fos);
         // 关闭流
         fos.close();
         System.out.println("07版excel写入成功");
 ​
         long end=System.currentTimeMillis();
         System.out.println("总耗时:"+(double)(end-begin)/1000);
     }
 }

2.3 SXSSF(待完善)

3.读入excel待完善

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值