poi的读取和写入

package com.sheng;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFCellUtil;
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.joda.time.DateTime;
import org.junit.Test;

import java.io.*;
import java.util.Date;

/**

  • TODO

  • @author gaohuansheng

  • @version V1.0.0

  • @date 2023/5/18 19:44
    */
    public class ExcelWriteTest {
    @Test
    public void testWrite03() throws IOException {
    String path = “E:\javacode\EasyExcel”;
    //创建一个工作簿 03
    Workbook workbook = new HSSFWorkbook();
    //创建一个工作表
    Sheet sheet = workbook.createSheet(“高欢胜测试”);
    //创建行
    Row row = sheet.createRow(0);
    //创建列
    Cell cell = row.createCell(0);
    cell.setCellValue(“统计人数”);
    Cell cell1 = row.createCell(1);
    cell1.setCellValue(2023.21);
    //第二行
    Row row1 = sheet.createRow(1);
    Cell cell2 = row1.createCell(0);
    cell2.setCellValue(“统计时间”);
    Cell cell3 = row1.createCell(1);
    cell3.setCellValue(new DateTime().toString(“yyyy-MM-dd HH:mm:ss”));
    //创建输出流
    FileOutputStream outputStream = new FileOutputStream(path + “高欢胜测试03.xls”);
    //写出文件
    workbook.write(outputStream);
    //关闭流
    outputStream.close();
    //打印
    System.out.println(“03文件测试成功”);

    }

    @Test
    public void testWrite03Big() throws IOException {
    String path = “E:\javacode\EasyExcel”;

     long start = System.currentTimeMillis();
     //创建工作簿
     Workbook workbook = new HSSFWorkbook();
     //创建工作表 第一个sheet名称叫gaohuansheng
     Sheet sheet = workbook.createSheet("gaohuansheng");
     //创建行
     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 outputStream = new FileOutputStream(path + "03ceshi.xls");
     workbook.write(outputStream);
     outputStream.close();
    
    
     long end = System.currentTimeMillis();
    
     System.out.println((double) (end - start) / 1000);
    

    }

    @Test
    public void testReadExcel() throws IOException {
    //文件根路径
    String path = “E:\javacode\EasyExcel”;
    //获取输入流
    FileInputStream inputStream = new FileInputStream(“E:\javacode\EasyExcel03ceshi.xls”);
    //获取工作簿
    Workbook workbook = new HSSFWorkbook(inputStream);
    //获取工作表 获取第一个sheet页
    Sheet sheet = workbook.getSheetAt(0);
    // 获取行
    for (int i = 0; i < 65536; i++) {
    Row row = sheet.getRow(i);
    for (int j = 0; j < 10; j++) {
    Cell cell = row.getCell(j);
    System.out.print(cell.getNumericCellValue());
    }
    }

     inputStream.close();
    

    }

    @Test
    public void testReadExcelGeshi() throws IOException {
    FileInputStream inputStream = new FileInputStream(“E:\javacode\会员消费商品明细表.xls”);
    Workbook workbook = new HSSFWorkbook(inputStream);
    Sheet sheet = workbook.getSheetAt(0);
    //获取单元测第一行标题
    Row row = sheet.getRow(0);
    if (row != null) {
    //一共多少列
    int number = row.getPhysicalNumberOfCells();
    //循环单元列的数量
    for (int i = 0; i < number; i++) {
    //获取单元格
    Cell cell = row.getCell(i);
    if (cell != null) {
    // 获取单元格中的值 表头为汉字 为字符串确定
    String value = cell.getStringCellValue();
    System.out.print(value + " | “);
    }
    }
    System.out.println();
    }
    //获取所有的行
    int rows = sheet.getPhysicalNumberOfRows();
    //从1开始 ,扣除表头标题
    for (int i = 1; i < rows; i++) {
    //每一行
    Row rowData = sheet.getRow(i);
    //获取列的数量
    int number = rowData.getPhysicalNumberOfCells();
    for (int j = 0; j < number; j++) {
    Cell rowDataCell = rowData.getCell(j);//每一个单元格 获取格式
    if (rowDataCell != null) {
    //获取每列单元格格式
    int cellType = rowDataCell.getCellType();
    //每列的结果 默认都是字符串 不是字符串转换为字符串
    String cellValue = “”;
    // 判断每列的类型
    switch (cellType) {
    case HSSFCell.CELL_TYPE_STRING://字符串
    System.out.print(”[String]“);
    cellValue = rowDataCell.getStringCellValue();
    break;
    case HSSFCell.CELL_TYPE_BOOLEAN://布尔
    System.out.print(”[BOOLEAN]“);
    cellValue = String.valueOf(rowDataCell.getBooleanCellValue());
    break;
    case HSSFCell.CELL_TYPE_BLANK://空
    System.out.print(”[BLANK]“);
    break;
    case HSSFCell.CELL_TYPE_NUMERIC://数字(日期) 和普通数字
    //判断是否是时间
    if (HSSFDateUtil.isCellDateFormatted(rowDataCell)) {
    System.out.print(”[日期格式]“);
    Date date = rowDataCell.getDateCellValue();
    cellValue = new DateTime(date).toString(“yyyy-MM-dd”);
    } else {
    //不是时间 是数字 转化成字符串 防止数字过长
    System.out.print(”[数字]“);
    rowDataCell.setCellType(HSSFCell.CELL_TYPE_STRING);
    cellValue = rowDataCell.toString();
    }
    break;
    case HSSFCell.CELL_TYPE_ERROR://空
    System.out.print(”[数据类型错误]");
    break;
    }
    System.out.println(cellValue);
    }
    }
    }
    inputStream.close();
    }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值