一。简介
1.运行原理
POI是Apache组织提供的一套操作office软件(Excel word PowerPoint)的API(application program interface)接口,通俗的讲就是用java类操作Excel word PPT 的工具.
POI就是 使用java代码 操作 office软件. POI 操作Excel
POI 的设计思想 (将软件中的各个部分幻化成一个对应的类)
整个Excel对象 ---- sheet --- row ---- cell --- 设置值
2.处理流程
① 创建工作簿excel
② 创建工作页 sheet
③ 创建行对象 row
④ 创建单元格 cell
⑤ 为单元格设置值
⑥ 通过流导出
3.生成Excel步骤
① 创建工作簿excel
② 创建工作页 sheet
③ 创建行对象 row
④ 创建单元格 cell
⑤ 为单元格设置值
⑥ 通过流导出
4.导入excel数据
① 指定导入文件
② 指定导入sheet页名
③ 一行行读取并循环读取每个单元格
二。导出demo
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
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.ss.util.CellRangeAddress;
/**
* @author yongqi.wang
* @version 创建时间:2018年6月7日 下午4:23:43 类说明:poi测试demo
*/
public class Poi {
public static void main(String[] args) {
Sheet sheet = null;
// 创建一个excel文件并指定文件名
File file = new File("D://testPoi");
@SuppressWarnings("resource")
Workbook workbook = new HSSFWorkbook();
// 标题行样式
CellStyle titleStyle = workbook.createCellStyle();
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
Font titleFont = workbook.createFont();
titleFont.setFontHeightInPoints((short) 20);
titleFont.setBoldweight((short) 700);
titleStyle.setFont(titleFont);
// 如果数据超过了 excel页名
sheet = workbook.createSheet("poi测试用例");
// 设置当前操作加密密码 不可修改、不可添加、不可复制
sheet.protectSheet(new String("yongqi_wang"));
// 创建标题行 行数从0开始 从第一列开始
Row titleRow = sheet.createRow(0);
titleRow.createCell(0).setCellValue("标题名");
titleRow.getCell(0).setCellStyle(titleStyle);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3 - 1));
Row row = sheet.createRow(1);
// 创建第一行数据
for (int i = 0; i < 3; i++) {
Cell createCell = row.createCell(i);
createCell.setCellValue(i);
}
FileOutputStream outputStream;
try {//创建文件输出流并输出
outputStream = new FileOutputStream(file);
workbook.write(outputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果展示
三。导入demo
import java.io.FileInputStream;
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;
/**
* @author yongqi.wang
* @version 创建时间:2018年6月7日 下午4:23:43 类说明:poi测试demo
*/
public class Poi {
public static void main(String[] args) {
Workbook workbook = null;
// 创建输入流
FileInputStream fis;
try {
fis = new FileInputStream("D://testPoi");
workbook = new HSSFWorkbook(fis);
} catch (Exception e) {
e.printStackTrace();
}
// 读取文件
// 指定读取页
Sheet sheet = workbook.getSheet("poi测试用例");
// 当前页共有多少条
int rowNum = sheet.getLastRowNum();
// 读取标题行
Row row = sheet.getRow(0);
System.out.println(row.getCell(0).getStringCellValue());
// 标题行为第一行所以直接读取第二行
// 跳过标题行
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
// 遍历行
short cellNum = row.getLastCellNum();
// 注意:读取的值类型要预先设定好, 比如以下读取数值类型的值
for (int j = 0; j < cellNum; j++) {
Cell cell = row.getCell(j);
cell.setCellType(Cell.CELL_TYPE_STRING);
System.out.println(cell.getStringCellValue());
}
}
}
}
结果展示