POI 简介
Apache POI 是 Apache 软件基金会开源的一个子项目, POI 提供 API 给 JAVA 对 Excel 进行读和写的功能
POI 操作 Excel (HSSF,XSSF)
HSSF: 提供读写 Microsoft Excel xls 格式文档的功能, 仅支持 2007 Microsoft Office 版本以前的
XSSF: 提供读写 Miceosoft Excel xlsx 格式文档的功能, 支持 2007 Microsoft Office 版本以后的
使用 jar 包
向 Excel 写入数据
- package com.workbook;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellStyle;
- import org.apache.poi.ss.usermodel.CreationHelper;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.xssf.usermodel.XSSFSheet;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import java.io.FileOutputStream;
- import java.util.Date;
- /**
- * @AUTHOR SHY
- * @DATE 2021/1/14 - 20:46
- */
- public class Demo {
- public static void main(String[] args) {
- // 创建一个工作簿
- XSSFWorkbook wb = new XSSFWorkbook();
- // 创建一个 sheet 页并命名
- XSSFSheet sheet = wb.createSheet("sheet 名");
- // 创建一行
- Row row = sheet.createRow(0); // 0 代表第一行
- // 创建单元格
- Cell cell1 = row.createCell(0); // 代表第一个单元格
- // 写入值, 字符串
- cell1.setCellValue("字符串");
- // 创建第二个单元格
- Cell cell2 = row.createCell(1);
- // 写入值, 数值
- cell2.setCellValue(123456);
- // 创建第三个单元格
- Cell cell3 = row.createCell(2);
- // 写入值, boolean
- cell3.setCellValue(true);
- // 创建第四个单元格
- Cell cell4 = row.createCell(3);
- // 写入值, 日期类型
- CreationHelper helper = wb.getCreationHelper();
- // 创建单元格样式
- CellStyle style = wb.createCellStyle();
- // 设置日期样式
- style.setDataFormat(helper.createDataFormat().getFormat("yyyy-MM-dd"));
- Date date = new Date();
- // 放入值
- cell4.setCellValue(date);
- // 放入样式
- cell4.setCellStyle(style);
- try {
- // 输出流
- FileOutputStream outputStream = new FileOutputStream("d:\\ 这是 Excel.xlsx");
- // 写进 Excel
- wb.write(outputStream);
- // 关闭流
- outputStream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
读取 Excel 数据
- package com.workbook;
- import org.apache.poi.hssf.usermodel.HSSFDateUtil;
- 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.FileInputStream;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- /**
- * @AUTHOR SHY
- * @DATE 2021/1/15 - 20:10
- */
- public class ReadExcel {
- public static void main(String[] args) {
- ReadExcel readExcel = new ReadExcel();
- try {
- readExcel.poiExcel();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 单元格类型判断, 输出字符串.
- */
- public static String getCellStringValue(Cell cell){
- String cellValue = "";
- if(cell!=null){ //cell 不为空
- if(cell.getCellType()==Cell.CELL_TYPE_BLANK){ // 空值
- cellValue = null;
- }else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){ // 数值
- cellValue = String.valueOf(cell.getNumericCellValue());
- }else if(cell.getCellType()==Cell.CELL_TYPE_BOOLEAN){ // 布尔值
- cellValue = String.valueOf(cell.getBooleanCellValue());
- }else if(cell.getCellType()==Cell.CELL_TYPE_FORMULA){ // 公式
- try {
- cellValue = String.valueOf(cell.getNumericCellValue());
- } catch (IllegalStateException e) {
- cellValue = String.valueOf(cell.getRichStringCellValue());
- }
- }else if(cell.getCellType()==Cell.CELL_TYPE_STRING){ // 字符串
- cellValue = cell.getStringCellValue();
- }
- }
- return cellValue;
- }
- /**
- * 取单元格日期, 输出字符串.
- */
- public static String getCellDateValue(Cell cell){
- String cellDateValue = "";
- if(cell!=null){
- // 设置日期格式
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
- cellDateValue = sdf.format(date);
- return cellDateValue;
- }
- return cellDateValue;
- }
- /**
- * 读取 Excel
- * @throws Exception
- */
- public void poiExcel() throws Exception{
- // 获取输入流
- FileInputStream inputStream = new FileInputStream("d:\\ 这是 Excel.xlsx");
- // 创建一个工作簿
- XSSFWorkbook wb = new XSSFWorkbook(inputStream);
- // 获取第一个 sheet 页
- XSSFSheet sheet = wb.getSheetAt(0);
- // 获取 sheet 页有效的行数, 从 0 开始
- int rowNum = sheet.getLastRowNum();
- // 遍历 sheet 页
- for(int r=0;r<=rowNum;r++){
- // 获取一行
- Row row = sheet.getRow(r);
- // 获取有效的单元格数
- int cellNum = row.getLastCellNum();
- for(int c=0;c<cellNum;c++){
- Cell cell = row.getCell(c);
- // 把得到的 Cell 值进行类型判断, 输出字符串类型
- String cellValue = getCellStringValue(cell);
- System.out.print(cellValue+"\t");
- }
- System.out.println();
- }
- // 日期取出是数值问题, 2020-01-15 会取出 43845, 需要单独对日期单元格进行取值.
- System.out.println(getCellDateValue(sheet.getRow(0).getCell(3)));
- System.out.println("执行完毕");
- }
- }
取合并单元格的值
- /**
- * 取合并单元格的值
- */
- public static String getMergedRegionsValue(XSSFSheet sheet,int rowNum,int cellColumn) {
- // 获取 sheet 页有多少个合并单元格
- int mergedRegionsCount = sheet.getNumMergedRegions();
- for(int i=0;i<mergedRegionsCount;i++){
- // 获取单元格范围
- CellRangeAddress rangeAddress = sheet.getMergedRegion(i);
- int firstRow = rangeAddress.getFirstRow(); // 获取合并单元格第一行
- int lastRow = rangeAddress.getLastRow(); // 获取合并单元格最后一行
- int firstColumn = rangeAddress.getFirstColumn(); // 获取合并单元格第一列
- int lastColumn = rangeAddress.getLastColumn(); // 获取合并单元格最后一列
- if(rowNum>=firstRow && rowNum<=lastRow){
- if(cellColumn>=firstColumn && cellColumn<=lastColumn){
- Row row = sheet.getRow(firstRow); // 取合并单元格第一行
- Cell cell = row.getCell(firstColumn); // 第一个单元格的值
- // 返回字符串
- return getCellStringValue(cell);
- }
- }
- }
- return null;
- }
来源: http://www.bubuko.com/infodetail-3717165.html