09Apache POI学习笔记
Apache POI学习笔记
文章目录
- 1 Poi介绍
- 1.1 poi简介
- 1.2 依赖包
- 1.3 POI包结构
- 1.4 优劣势
- 2 入门案例
- 2.1 从Excel文件读取数据
- 2.1.1 读取步骤
- 2.1.2 读取Excel
- 2.2 向Excel文件写入数据
- 2.2.1 写入步骤
- 2.2.2 写入Excel
- 3 实战练习
- 3.1 样式
- 3.1.1 对齐方式
- 3.1.2 边框
- 3.1.3 背景颜色
- 3.1.4 合并单元格
- 3.2 导入Excel
- 3.3 导出Excel
1 Poi介绍
1.1 poi简介
1.有Apache公司提供
2.Java编写的免费开源的跨平台的Java API
3.提供API给Java程序堆Microsoft Office格式档案读和写的功能
1.2 依赖包
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.14</version>
</dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.14</version>
</dependency>
1.3 POI包结构
。HSSF – 读写Microsoft Excel XLS
。XSSF --读写Microsoft Excel OOXML XLSX
。HWPF–读写Microsoft Word DOC
。HSLF --读写Microsoft PowerPoint
1.4 优劣势
Jxl:x消耗小,图片和图形支持有限
POI:功能更加完善,用户量的最大,使用最简单
2 入门案例
2.1 从Excel文件读取数据
2.1.1 读取步骤
1.获取工作薄 workbook (Excel文件)
2.获取工作表 sheet
3.遍历工作表获得行对象 row
4.遍历行对象获取单元格对象 cell
5.获得单元格的值
H:\009Apache POI\hello.xlsx
2.1.2 读取Excel
package com.tangguanlin.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.IOException;
/*** 说明:Poi读取Excel* 作者:汤观林* 日期:2021年12月27日 14时*/
public class ReadExcel1 {public static void main(String[] args) throws IOException {//1.获取工作薄 workbookXSSFWorkbook workbook = new XSSFWorkbook("H:\\009Apache POI\\hello.xlsx");//2.获取工作表 sheet//XSSFSheet sheet = workbook.getSheetAt(0); //根据下标取XSSFSheet sheet = workbook.getSheet("Sheet1"); //根据名称取//普通for循环int lastRowNum = sheet.getLastRowNum(); //最大行数for(int i=0;i<=lastRowNum;i++){Row row = sheet.getRow(i);short lastCellNum = row.getLastCellNum(); //最大列数for(int j=0;j<lastCellNum;j++){String value = row.getCell(j).getStringCellValue();System.out.println(value);}}//增强for循环-------------------------------------------------------------//3.遍历工作表获得行对象 rowfor(Row row:sheet){//4.遍历行对象获取单元格对象 cellfor(Cell cell:row){//5.获得单元格的值String content = cell.getStringCellValue();System.out.println(content);}}//6.释放资源workbook.close(); }
}
运行结果:
你好
我的
世界
2.2 向Excel文件写入数据
2.2.1 写入步骤
1.创建一个Excel工作薄 workbook (Excel文件)
2.创建工作表 sheet
3.创建行 row
4.创建单元格赋值 cell
5.通过输出流将对象下载到磁盘
2.2.2 写入Excel
package com.tangguanlin.poi;
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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*** 说明:Poi写入Excel* 作者:汤观林* 日期:2021年12月27日 15时*/
public class WriteExcel1 {public static void main(String[] args) throws IOException {//1.创建工作簿XSSFWorkbook workbook = new XSSFWorkbook();//2.创建工作表XSSFSheet sheet = workbook.createSheet("工作表1");//3.创建行XSSFRow row0 = sheet.createRow(0);//4.创建列row0.createCell(0).setCellValue("传智播客");row0.createCell(1).setCellValue("黑马程序员");row0.createCell(2).setCellValue("博学谷");XSSFRow row1 = sheet.createRow(1);row1.createCell(0).setCellValue("传智播客");row1.createCell(1).setCellValue("黑马程序员");row1.createCell(2).setCellValue("博学谷");//5.输出流FileOutputStream out = new FileOutputStream("H:\\009Apache POI\\heima.xlsx"); //文件位置 不存在会自动创建workbook.write(out);out.flush();//6.释放资源out.close();workbook.close();System.out.println("写入成功");}
}
运行结果:
H:\009Apache POI\heima.xlsx
3 实战练习
3.1 样式
3.1.1 对齐方式
//创建单元格样式对象
XSSFCellStyle cellStyle = workbook.createCellStyle();//创建字体样式XSSFFont font = workbook.createFont();font.setFontName("黑体"); //字体font.setColor(IndexedColors.BLUE.getIndex()); //字体颜色cellStyle.setFont(font);cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 设置单元格水平方向对其方式cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 设置单元格垂直方向对其方式//列中使用单元格样式
cell1.setCellStyle(cellStyle);
3.1.2 边框
//创建单元格样式对象
XSSFCellStyle cellStyle = workbook.createCellStyle();cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); //上边边框 cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边边框颜色cellStyle.setBorderRight(CellStyle.BORDER_THIN); //右边边框 cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex()); //右边边框颜色 cellStyle.setBorderBottom(CellStyle.BORDER_THIN); //底部边框cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //底部边框颜色 cellStyle.setBorderLeft(CellStyle.BORDER_THIN); //左边边框cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex()); //左边边框颜色//列中使用单元格样式
cell.setCellStyle(cellStyle);
3.1.3 背景颜色
//创建单元格样式对象
XSSFCellStyle cellStyle = workbook.createCellStyle();cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); //背景色
cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); //前景色
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); //列中使用单元格样式
cell.setCellStyle(cellStyle);
3.1.4 合并单元格
// 起始行 结束行 起始列 结束列
sheet.addMergedRegion(new CellRangeAddress(1, 2, 1, 2));
3.2 导入Excel
H:\009Apache POI\product.xlsx
代码:
Product产品类:
package com.tangguanlin.poi;
import lombok.Data;
/*** 说明:产品类* 作者:汤观林* 日期:2021年12月27日 16时*/
@Data
public class Product {private int pid;private String pname;private double price;private int pstock;
}
Import导入类:
package com.tangguanlin.poi.web;
import com.tangguanlin.poi.model.Product;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/*** 说明:导入Excel文件数据* 作者:汤观林* 日期:2021年12月27日 16时*/
public class Import {public static void main(String[] args) throws IOException {//读取Excel文件中的数据List<Product> productList = new ArrayList<Product>();//1 获取工作薄workbookXSSFWorkbook workbook = new XSSFWorkbook("H:\\009Apache POI\\product.xlsx");//2 获取sheetXSSFSheet sheet = workbook.getSheetAt(0);//3 获取行int lastRowNum = sheet.getLastRowNum(); //最大行数for(int i=1;i<=lastRowNum;i++){XSSFRow row = sheet.getRow(i);if(row!=null){List<String> list = new ArrayList<>();for(Cell cell: row){if(cell!=null){cell.setCellType(Cell.CELL_TYPE_STRING); //统一设置为string类型list.add(cell.getStringCellValue());}}Product product = new Product(Integer.valueOf(list.get(0)),list.get(1),Double.valueOf(list.get(2)),Integer.valueOf(list.get(3)));productList.add(product);}}for(Product product:productList){System.out.println(product);}}
}
运行结果:
Product{pid=1, pname='苹果', price=25.0, pstock=100}
Product{pid=2, pname='皇冠梨', price=15.0, pstock=300}
Product{pid=3, pname='香蕉', price=18.0, pstock=250}
Product{pid=4, pname='火龙果', price=3.0, pstock=100}
Product{pid=5, pname='榴莲', price=5.0, pstock=50}
Product{pid=6, pname='橙子', price=2.0, pstock=120}
3.3 导出Excel
package com.tangguanlin.poi.web;
import com.tangguanlin.poi.model.Product;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/*** 说明:将数据导出到Excel* 作者:汤观林* 日期:2021年12月27日 16时*/
public class Export {public static void main(String[] args) throws IOException {//将数据写入到Excel文件中List<Product> productList = new ArrayList<>();Product product1 = new Product(1,"苹果",25.0,100);productList.add(product1);Product product2 = new Product(1,"皇冠梨",15.0,300);productList.add(product2);Product product3 = new Product(1,"香蕉",18.0,250);productList.add(product3);Product product4 = new Product(1,"火龙果",3.0,100);productList.add(product4);Product product5 = new Product(1,"榴莲",5.0,50);productList.add(product5);Product product6 = new Product(1,"橙子",2.0,120);productList.add(product6);//1.创建工作簿XSSFWorkbook workbook = new XSSFWorkbook();//2.创建工作表XSSFSheet sheet = workbook.createSheet("工作表1");//创建单元格样式对象XSSFCellStyle cellStyle = workbook.createCellStyle();//对齐方式cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平居中cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //垂直居中//边框cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); //上边边框cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边边框颜色cellStyle.setBorderRight(CellStyle.BORDER_THIN); //右边边框cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex()); //右边边框颜色cellStyle.setBorderBottom(CellStyle.BORDER_THIN); //底部边框cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //底部边框颜色cellStyle.setBorderLeft(CellStyle.BORDER_THIN); //左边边框cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex()); //左边边框颜色//背景颜色cellStyle.setFillForegroundColor(IndexedColors.PINK.getIndex()); //前景色cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); //实心cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); //背景色cellStyle.setFillPattern(CellStyle.BIG_SPOTS);//创建字体样式XSSFFont font = workbook.createFont();font.setFontName("黑体"); //字体font.setColor(IndexedColors.BLUE.getIndex()); //字体颜色cellStyle.setFont(font);XSSFRow row0 = sheet.createRow(0);XSSFCell cell0 = row0.createCell(0);cell0.setCellValue("商品编号");cell0.setCellStyle(cellStyle);XSSFCell cell1 = row0.createCell(1);cell1.setCellValue("商品名称");cell1.setCellStyle(cellStyle);XSSFCell cell2 = row0.createCell(2);cell2.setCellValue("商品价格(单位:元/斤)");cell2.setCellStyle(cellStyle);XSSFCell cell3 = row0.createCell(3);cell3.setCellValue("商品库存(单位:吨)");cell3.setCellStyle(cellStyle);for(int i=0;i<productList.size();i++){//3.创建行XSSFRow row = sheet.createRow(i+1);//4.创建列row.createCell(0).setCellValue(productList.get(i).getPid());row.createCell(1).setCellValue(productList.get(i).getPname());row.createCell(2).setCellValue(productList.get(i).getPrice());row.createCell(3).setCellValue(productList.get(i).getPstock());}//合并单元格 起始行 结束行 起始列 结束列sheet.addMergedRegion(new CellRangeAddress(3,5,1,3));//5.输出流FileOutputStream out = new FileOutputStream("H:\\009Apache POI\\product_export.xlsx"); //文件位置 不存在会自动创建workbook.write(out);out.flush(); //输出//6.释放资源out.close();workbook.close();System.out.println("写入成功");}
}
运行结果:
H:\009Apache POI\product_export.xlsx
09Apache POI学习笔记相关推荐
- POI学习笔记 自定义颜色
http://yunzhongxia.iteye.com/blog/561425 http://yunzhongxia.iteye.com/blog/558998 项目中经常要解析和生成Excel文件 ...
- Apache POI学习笔记
概述 Apache POI 简介是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office(Excel.WORD.Power ...
- EasyExcel和POI学习笔记
EasyExcel和POI使用详解 一.Poi-Excel 写 1.常规入门 POI是Apache软件基金会的,POI为"Poor Obfuscation Implementation&qu ...
- JSP学习笔记(四十九):抛弃POI,使用iText生成Word文档
POI操作excel的确很优秀,操作word的功能却不敢令人恭维.我们可以利用iText生成rtf文档,扩展名使用doc即可. 使用iText生成rtf,除了iText的包外,还需要额外的一个支持rt ...
- [LBS学习笔记4]地理特征POI、AOI、路径轨迹
1 简述 今天继续LBS地理信息的学习,目标是写到10篇博客的时候,做出一个地图工具页面用,包含地图空间索引Geohash.S2.H3的可视化展示. 地理特征分为点(POI).线(路径).面(AOI) ...
- Android学习笔记:短信控制手机之“短信开启定位”
2019独角兽企业重金招聘Python工程师标准>>> 首先,我必须说,我是菜鸟.接触Android不就久,在这里记下,第一想让自己有更加深刻的印象,第二也想和大家一起分享.如果出现 ...
- 【实习】T100开发学习笔记
T100开发学习笔记 笔记目录 一些小技巧 实用的通用快捷键 所有全局变量(top_global.inc文件) 笔记目录 Linux 学习笔记 T100 基础架构.命名原则 Genero FGL (T ...
- excel文本方式区学习笔记
excel文本方式区学习笔记 import java.io.FileInputStream; import java.io.InputStream; import org.apache.poi.hss ...
- 贝叶斯方法学习笔记(一)
贝叶斯方法学习笔记(一) 一.基本概念 二.实例(史蒂文的身份): 三.基本的概率分布及其性质 四.实例(用短信数据推断行为): 数据集来源 一.基本概念 先验概率:我们把对一个事件A发生的信念记作P ...
最新文章
- 跳槽,你心脚同步吗?
- 关于MyEcplise中常见的问题和解决方案
- HTML DOM教程 22-HTML DOM Form 对象
- java获取正在执行的timer_Java线程与并行编程(一)
- “上班那点儿事交流圈”发展计划
- 计算机网络email服务的配置,计算机网络邮件配置实验报告
- C++ 智能指针unique_ptr的简单实现
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_1_缓冲流的原理...
- .NET 5(C#) 将控制台程序(Console Application)发布成单个exe文件
- lingo数学软件完整教程
- python bytes类型中是ascii码_Python3 中bytes数据类型深入理解(ASCII码对照表)
- iphone图片编辑画笔_iPhone手机怎么编辑图片?还不知道的话真的要了解一波了~...
- 我与AWS Proserve团队的故事
- Python调用HEG批量转换hdf影像为tiff
- chrome-调试按钮详解
- 2022-2028全球与中国汽车制动系统市场现状及未来发展趋势
- 突破技术发展瓶颈、成功转型的重要因素
- java操作svg文件
- Mac OSX中设置路由
- centos 安装并使用rar解压压缩文件