Java架构-Apache POI Excel

相信在项目中,对数据进行动态导出这是一个比较常见的功能。对于数据导出咱们可使用Apache-POI这个框架来帮我来进行Excel的写入与读取。下面就用代码来实现Apache POI写入与读取excel文件。java

一、Apache POI基本概念apache

下面将简单的描述一下当进行Excel读取与写入的时候要使用到的基本类。bash

  1. HSSF 为前缀的类名表示操做的是Microsoft Excel 2003文件。
  2. XSSF 为前缀的类名表示操做的是Microsoft Excel 2007或之后的版本
  3. XSSFWorkbook 和 HSSFWorkbook表示一个Excel的Workbook.
  4. HSSFSheet 和 XSSFSheet 表示一个Excel的Worksheet.
  5. Row 表示一个Excel行
  6. Cell 表示当前Row中一个Cell.

二、下载Apache POI架构

在项目中是使用Maven来管理Jar依赖的,因此在Pom.xml添加如下依赖:框架

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>3.15</version>
</dependency>
复制代码

三、写入一个Excel文件xss

下面的代码将会简单的展现使用Apache POI写入一个Excel文件。数据将会写入到XSSFWorkbook对象中。学习

ApachePOIExcelWrite.java
复制代码
package com.weimob.o2o.carl.poi;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ApachePOIExcelWrite {

    private static final String FILE_NAME = "/tmp/MyFirstExcel.xlsx";

    public static void main(String[] args) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Data types in Java");
        Object[][] dataTypes = {
                {"DataType", "Type", "Size(in bytes)"},
                {"int", "Primitive", 2},
                {"float", "Primitive", 4},
                {"double", "Primitive", 8},
                {"char", "Primitive", 1},
                {"String", "Non-Primitive", "No fixed size"}
        };

        int rowNum = 0;
        System.out.println("Creating excel");
        for(Object[] dataType : dataTypes){
            Row row = sheet.createRow(rowNum++);
            int colNum = 0;
            for(Object field : dataType){
                Cell cell = row.createCell(colNum++);
                if(field instanceof String){
                    cell.setCellValue((String) field);
                } else if (field instanceof Integer){
                    cell.setCellValue((Integer) field);
                }
            }
        }
        try {
            FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
            workbook.write(outputStream);
            workbook.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Done");

    }

}

复制代码

你将会在你项目所在的磁盘中的tmp文件夹中获得如下的excel文件:spa

四、读取一个Excel文件3d

下面的代码展现如何使用Apache POI读取Excel文件。getCellTypeEnum方法在 3.15 中不推荐使用而且会在 4.0 版本中将会更名为:getCellType.excel

ApachePOIExcelRead.java
复制代码
package com.weimob.o2o.carl.poi;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

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

public class ApachePOIExcelRead {

    private static final String FILE_NAME = "/tmp/MyFirstExcel.xlsx";

    public static void main(String[] args) {
        try {
            FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
            Workbook workbook = new XSSFWorkbook(excelFile);
            Sheet dataTypeSheet = workbook.getSheetAt(0);
            Iterator<Row> iterator = dataTypeSheet.iterator();
            while(iterator.hasNext()){
                Row currentRow = iterator.next();
                Iterator<Cell> cellIterator = currentRow.iterator();
                while(cellIterator.hasNext()){
                    Cell currentCell = cellIterator.next();
                    //getCellTypeEnum shown as deprecated for version 3.15
                    //getCellTypeEnum ill be renamed to getCellType starting from version 4.0
                    if(currentCell.getCellTypeEnum() == CellType.STRING){
                        System.out.print(currentCell.getStringCellValue() + "--");
                    } else if(currentCell.getCellTypeEnum() == CellType.NUMERIC){
                        System.out.print(currentCell.getNumericCellValue() + "--");
                    }
                }
                System.out.println();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
复制代码

你的控制台将会输出如下代码:

为何某些人会一直比你优秀,是由于他自己就很优秀还一直在持续努力变得更优秀,而你是否是还在知足于现状心里在窃喜!

合理利用本身每一分每一秒的时间来学习提高本身,不要再用"没有时间“来掩饰本身思想上的懒惰!趁年轻,使劲拼,给将来的本身一个交代!

To-陌霖Java架构
复制代码

分享互联网最新文章 关注互联网最新发展

相关文章
相关标签/搜索