ApachePOI快速入门

Posted 星时代曹波涛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ApachePOI快速入门相关的知识,希望对你有一定的参考价值。

POI

三大实现类

HSSFWorkbook XSSFWorkbook SXSSFWorkbook

  <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
  </dependencies>
  • 03 创建 HSSFWorkbook对象 xls --poi 6536行

  • 07 创建 XSSFWorkbook对象 xlsx --ooxml 耗时长!!

  • 00 创建 SXSSFWorkbook对象 升级版 快一点!

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
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.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 03版本 后缀 xls HSSFWorkbook对象
 * 03版本 后缀 xlsx  XSSFWorkbook对象
 */
public class Demo {
    public static void main(String[] args) throws IOException {

        //新建工作簿
        Workbook workbook = new HSSFWorkbook();
        //新建表
        Sheet sheet = workbook.createSheet("统计表");
        //创建行
        Row row1 = sheet.createRow(0);
        //创建单元格
        Cell cell = row1.createCell(0);
        //在单元格填充内容
        cell.setCellValue("用户名");
        Cell cell1 = row1.createCell(1);
        cell1.setCellValue("性别");
        Row row2 = sheet.createRow(1);
        Cell cell2 = row2.createCell(0);
        cell2.setCellValue("哈哈");
        Cell cell3 = row2.createCell(1);
        cell3.setCellValue("15");
        //表格导出
        String path = "D:\web\";
        FileOutputStream fileOutputStream = new FileOutputStream(path + "03.xlsx");
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();



        FileInputStream fileInputStream = new FileInputStream("D:\web\" + "03.xlsx");
        Workbook wb = new HSSFWorkbook(fileInputStream);
        Sheet st = wb.getSheetAt(0);

        Row row = st.getRow(0);
        int physicalNumberOfCells = row.getPhysicalNumberOfCells();
        for (int i = 0; i < physicalNumberOfCells; i++) {
            if(i==0) {
                System.out.print("|");
            }
            Cell cellxx = row.getCell(i);
            System.out.print(cellxx+"|");
        }

        int physicalNumberOfRows = st.getPhysicalNumberOfRows();

        for (int i = 0; i < physicalNumberOfRows; i++) {
            if(i==0) {
                continue;
            }
            System.out.println("");
            Row row3 = st.getRow(i);
            int physicalNumberOfCells1 = row3.getPhysicalNumberOfCells();
            for (int j = 0; j < physicalNumberOfCells1; j++) {
                Cell cellxx = row3.getCell(j);
                System.out.print(cellxx+"|");
            }
        }
        fileInputStream.close();
    }
}

1、获取sheet的总行数
int totalRows = sheet.getPhysicalNumberOfRows();
2、获取该行的总列数
int RowCells=sheet.getRow(0).getPhysicalNumberOfCells();




以上是关于ApachePOI快速入门的主要内容,如果未能解决你的问题,请参考以下文章

EasyExcel快速入门

EasyExcel快速入门

SpringBoot入门教程:下载导出Excel(EasyExcel)

SpringBoot入门教程:下载导出Excel(EasyExcel)

SpringBoot入门教程:下载导出Excel(EasyExcel)

mybatis快速入门