POI 操作03版excel

1.运行环境,idea编写,maven管理工程。

2.依赖文件:

   (包含poi 对excel 03,07 版 和日期格式化(这个更好用)以及junit 单元测试依赖)

POM.xml;

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>xingci</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--03excel-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.0.0</version>
        </dependency>
        <!--07excel-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.0.0</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>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>



</project>

 

 

3.代码段:

  

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

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.junit.Test;
public class excel03 {
@Test
    public void m1() throws Exception{
        //1.路径
        String path = "F:\\java_program\\xingci\\03excel.xls";
        File filepath = new File(path);
        if(!filepath.exists()){
            filepath.createNewFile();
        }

        //2.工作薄
    Workbook workbook = new HSSFWorkbook();
            //2.1工作表
    Sheet sheet = workbook.createSheet("滚你妈的");
            //2.2行
    Row rows = sheet.createRow(0);
            //2.3列
    Cell cells = rows.createCell(0);
            //2.4.写入
    cells.setCellValue("你好吗");

        //3.io 流和导出
    FileOutputStream fis = new FileOutputStream(filepath);
    workbook.write(fis);
    fis.close();
    System.out.println("导入完成");
    }
}

4.备注

  最后的导入需要的是文件而不是文件夹。file可以被用到io流里面。

write 来写入输出流当中。

posted @ 2022-05-29 10:36  星辉与你  阅读(45)  评论(0)    收藏  举报