poi

这篇博客介绍了如何使用Apache POI库在Java中进行Excel文件的读写操作。通过创建Maven项目,导入必要的依赖,然后编写测试类,展示了创建Excel工作簿、设置单元格值、读取单元格内容等基本操作。博客还提到了超出设定长度的数据会侵占后续单元格,并提供了读取Excel文件的示例代码。
摘要由CSDN通过智能技术生成

java与Excel交互用的东西,说白了就是读写文档,office用的那几种(说实话我不知道他是怎么支持ppt的)。

老Apache出品。

2021-1-20,更新了5.0.0版本。

总的来说没有什么特别的地方,就是个直接用的东西。

老样子,建项目,导包,写代码调用,测试。

在这里插入图片描述

建maven项目,poidemo。

传统异能贴一个以前用过的pom代码进去,不管是不是多余,至少是覆盖了这个demo的需求,方便。

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.0.2.RELEASE</spring.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <mysql.version>5.1.6</mysql.version>
        <mybatis.version>3.4.6</mybatis.version>
    </properties>

    <dependencies>
        <!-- spring -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- log end -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
   

    </dependencies>

然后在加上这个demo的主角:

<!--  POI依赖  -->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>5.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>5.0.0</version>
    </dependency>

等它加载,就可以直接写测试类了。

poi是个直接拿来用的功能组件,只需要导包,没有其他的前置,当然如果不是在demo里使用,还是要陪喝前后台做数据传输的。

作为懒狗,直接在test里写个test类就算完事。

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class poitest {
    public static void main(String[] args) throws IOException {
        /*第一步,新建一个文件,这里测试Excel,也是因为Excel使用率高,而且长得也像数据库
        * 各种好看方便。
        * 不过这里创建的还不是Excel,而是poi里的workbook,待会有脚本去转换格式。*/
        XSSFWorkbook testbook=new XSSFWorkbook();
        //创建页。
        XSSFSheet sheet=testbook.createSheet("testSheet");
        sheet.setColumnWidth(3,20*256);//这句的意思是修改某一列的宽度
        /*我个人是不懂排版的,这个改排版的事也是看到有人这么写就跟着写了,完全不知道有什么意义,
        * 可能是多记几个关键词?可能以后用的上吧。*/
        sheet.setColumnWidth(4,20*256);
        XSSFRow row=sheet.createRow(0);//新建行
        XSSFCell cell=row.createCell(0);//给这个行新建列,就是给每个空格加了坐标
        cell.setCellValue("name");//第0行的的第0列
        row.createCell(1).setCellValue("age");
        row.createCell(2).setCellValue("sex");
        row.createCell(3).setCellValue("call");
        row.createCell(4).setCellValue("mark");
        //老user了
        row=sheet.createRow(1);//创建真正的第一行
        row.createCell(0).setCellValue("智障小张");
        row.createCell(1).setCellValue("22");
        row.createCell(2).setCellValue("♂");
        row.createCell(3).setCellValue("12894018034174032");//这里是有长度限制的,20位
        row.createCell(4).setCellValue("22岁,是学生");

        row=sheet.createRow(2);
        row.createCell(0).setCellValue("afgashlkg");
        row.createCell(1).setCellValue("asd");//其实没有限制类型,一律String
        row.createCell(2).setCellValue("男女男");
        row.createCell(3).setCellValue("12894018034174032");//这里是有长度限制的,20位
        row.createCell(4).setCellValue("io'RE'GRJIEDS'AGASD==5943;g8u[sdfgvy8ovygsdpv4bwj   liayutg");//按一个超过20的出来测试。

        //设定文件路径,去文件管理粘贴
        File file = new File("D:\\工作\\poitest\\user.xlsx");
        FileOutputStream stream = new FileOutputStream(file);
        //需要抛异常
        testbook.write(stream);
        //关流
        stream.close();

    }
}

在这里插入图片描述

值的注意的是,超出设定的长度之后,它是会溢出来的,并侵占后面的的格子。

写文件的有了,同理我们要有读文件的手段。

这次我们先写一个表格。

在这里插入图片描述

大概覆盖了一下测试范围。

现在开始编(抄)写代码:(全是库函数,没啥自己的东西)

import org.apache.poi.ss.usermodel.CellType;
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.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

public class readtest {
    public static void main(String[] args) throws IOException {
        readit();
    }

    public  static void readit() throws IOException {
        String text="";
        File file = new File("D:\\工作\\poitest\\zxcv.xlsx");
        //获得该文件的输入流
        FileInputStream stream = new FileInputStream(file);
        //多态抛异常
        Workbook sheets = new XSSFWorkbook(stream);
        //获取工作表(sheet)
        Sheet sheet = sheets.getSheetAt(0);
       /* for (int i = 1; i<=sheet.getLastRowNum() ; i++) {
            //行数
            Row row = sheet.getRow(i);

            //获取单元格取值
            String value1 = row.getCell(0).getStringCellValue();
            String value2 = row.getCell(1).getStringCellValue();
            String value3 = row.getCell(2).getStringCellValue();
            String value4 = row.getCell(3).getStringCellValue();
            String value5 = row.getCell(4).getStringCellValue();

            System.out.println(value1);
            System.out.println(value2);
            System.out.println(value3);
            System.out.println(value4);
            System.out.println(value5);
        }*/
       //上面的读取失败了:Cannot get a STRING value from a NUMERIC cell
        //所以重写一个,改成了循环。
       for (Iterator rowIterator = sheet.iterator(); rowIterator.hasNext();)
        {
            XSSFRow row=(XSSFRow) rowIterator.next();
            for (Iterator iterator=row.cellIterator();iterator.hasNext();)
            {
                XSSFCell cell=(XSSFCell) iterator.next();
                //根据单元的的类型 读取相应的结果
                if(cell.getCellType()== CellType.STRING) text+=cell.getStringCellValue()+"\t";
                else if(cell.getCellType()==CellType.NUMERIC) text+=cell.getNumericCellValue()+"\t";
                else if(cell.getCellType()==CellType.FORMULA) text+=cell.getCellFormula()+"\t";
                /*因为poi的版本更新,之前的XSSFCell.CELL_TYPE_STRING不能用了,改成了现在简化的样子
                * 是好事,不是吗?*/
            }
            text+="\n";

        }
        System.out.println(text);

        //关流
        sheets.close();
        stream.close();
    }
}

在这里插入图片描述

成功。

yysy这个迭代器是现学来的,真要我写,我下次也就只能写个思路出来,关键词肯定记不住(小声)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值