excel的导入和下载(poi)

通过使用相关的Java库,如Apache POI、EasyPoi或EasyExcel,可以轻松地实现Excel文件的读写操作。

1.2 典型应用场景

  • 报表自动化生成(如导出统计表格)

  • 批量数据导入(如从Excel读取用户信息)

  • 文档模板填充(合同、通知等)

  • 服务器端文档格式转换

  • HSSF/XSSF:处理Excel(97-2003/2007+格式)

  • HWPF/XWPF:处理Word文档

  • HSLF/XSLF:处理PowerPoint

Maven依赖配置

复制代码
<!-- POI核心库 -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.3</version>
</dependency>
<!-- 处理xlsx格式 -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.3</version>
</dependency>

创建第一个Excel文件

java 复制代码
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelDemo {
    public static void main(String[] args) throws Exception {
        Workbook workbook = new XSSFWorkbook(); // 创建xlsx工作簿
        Sheet sheet = workbook.createSheet("用户列表");
        
        // 创建标题行
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("ID");
        headerRow.createCell(1).setCellValue("姓名");
        
        // 添加数据
        Row dataRow = sheet.createRow(1);
        dataRow.createCell(0).setCellValue(1);
        dataRow.createCell(1).setCellValue("张三");
        
        // 保存文件
        try (FileOutputStream fos = new FileOutputStream("demo.xlsx")) {
            workbook.write(fos);
        }
        workbook.close();
    }
}

读取数据

java 复制代码
    public static void read() throws Exception {
 
        // 读取磁盘上面已经存在的Excel文件
        XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream("盘符名:\\***\\123.xlsx"));
 
        // 读取Excel文件里面的第一个sheet页
        XSSFSheet sheet = excel.getSheetAt(0);
 
        // 获取最后一行有数据的文件
        int lastRowNum = sheet.getLastRowNum();
 
        for (int i = 0; i <= lastRowNum; i++) {
            // 获取某一行
            XSSFRow row = sheet.getRow(i);
            String cellValue1 = row.getCell(0).getStringCellValue();
            String cellValue2 = row.getCell(1).getStringCellValue();
            String cellValue3 = row.getCell(2).getStringCellValue();
 
            System.out.println(cellValue1 + "\t" + cellValue2 + "\t" + cellValue3);
        }
 
        // 关闭资源
        excel.close();
 
    }

三、Excel高级操作技巧

java 复制代码
// 创建单元格样式
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

Font font = workbook.createFont();
font.setBold(true);
style.setFont(font);

cell.setCellStyle(style); // 应用样式

合并单元格

java 复制代码
sheet.addMergedRegion(new CellRangeAddress(
    0, // 起始行
    0, // 结束行
    0, // 起始列
    3  // 结束列
));

处理日期与公式

java 复制代码
// 设置日期格式
CellStyle dateStyle = workbook.createCellStyle();
dateStyle.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd"));
cell.setCellValue(new Date());
cell.setCellStyle(dateStyle);

// 设置公式
cell.setCellFormula("SUM(A1:A10)");
相关推荐
程序定小飞21 小时前
基于springboot的电影评论网站系统设计与实现
java·spring boot·后端
苹果醋321 小时前
JAVA面试汇总(二)多线程(五)
运维·vue.js·spring boot·nginx·课程设计
兜兜风d'1 天前
RabbitMQ 持久性详解
spring boot·分布式·rabbitmq·1024程序员节
本贾尼1 天前
csv文件用Excel打开后出现乱码的问题及其解决方法
windows·excel
njsgcs1 天前
PDF信息vlm提取excel工具
pdf·excel
问道飞鱼1 天前
【微服务组件】Springboot结合Dubbo实现RPC调用
spring boot·微服务·rpc·dubbo
I'm Jie1 天前
(二)Gradle 依赖仓库及安全凭证配置
java·spring boot·spring·gradle·maven
李少兄1 天前
记一次 Spring Boot 项目中 Redis 工具类的重构实践
spring boot·redis·重构
摇滚侠1 天前
Spring Boot3零基础教程,生命周期启动加载机制,笔记64
spring boot·笔记
摇滚侠1 天前
Spring Boot3零基础教程,整合 Redis,笔记69
spring boot·redis·笔记