zl程序教程

您现在的位置是:首页 >  Java

当前栏目

java -POI的基本操作Excel

2023-02-18 16:35:54 时间

导入依赖

<dependencies>
<!--操作旧版本-->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.0.1</version>
    </dependency>
<!--操作新版本-->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>4.0.1</version>
    </dependency>
</dependencies>

案例

创建一个简单的Excel

  • XSSFWorkbook 关键字 :工作薄 一个Excel文件
  • XSSFSheet 关键字 : 工作表 Excel中的工作表
  • XSSFRow关键字 : 行 每个工作表的行
  • XSSFCell 关键字 : 单元格 每个行中的单元格
  • XSSFCellStyle关键字 : 单元格样式
package cn.itcsdn;

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.FileOutputStream;
//        XSSFWorkbook  工作薄    一个Excel文件
//        XSSFSheet     工作表    Excel中的工作表
//        XSSFRow       行        每个工作表的行
//        XSSFCell      单元格    每个行中的单元格
//        XSSFCellStyle 单元格样式
//创建一个Excel并且向里面写入一句话
public class POIDemo1  {
    public static void main(String[] args) throws Exception{

//        1、创建一个全新的工作薄 里面什么都没有
        XSSFWorkbook workbook = new XSSFWorkbook();
//        2、创建一个工作表
        XSSFSheet sheet = workbook.createSheet("测试");
//        设置列宽
        sheet.setColumnWidth(0,17*256); // 单位 1代表一个字母的256分之一
//        3、创建第一行行
        XSSFRow row = sheet.createRow(0);
//        4、创建第一个单元格
        XSSFCell cell = row.createCell(0);
//        5、向单元格中放一句话
        cell.setCellValue("创建第一个POI程序");
//        6、把Excel输出到磁盘上
        workbook.write(new FileOutputStream("D://oneExcel.xlsx"));
//			释放资源
        workbook.close();
    }
}

创建一个带样式的Excel

package cn.itcsdn;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;


//使用java代码创建一个新版本全新的excel,里面带样式
public class POIDemo2 {
    public static void main(String[] args) throws Exception {
//        创建了一个里面什么都没有的工作薄
        Workbook workbook = new XSSFWorkbook();

//        创建样式、
        CellStyle cellStyle = workbook.createCellStyle();
//        居中对齐
        cellStyle.setAlignment(HorizontalAlignment.CENTER);  //水平对齐方式
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);  //垂直对齐方式
//      创建字体对象,并修改
        Font font = workbook.createFont();
        font.setFontName("宋体"); //设置字体
        font.setFontHeightInPoints((short) 16);//设置字体大小
        font.setBold(true);//是否加粗
        cellStyle.setFont(font);


//        创建样式、
        CellStyle cellStyle1 = workbook.createCellStyle();
//        居中对齐
        cellStyle1.setAlignment(HorizontalAlignment.CENTER);  //水平对齐方式
        cellStyle1.setVerticalAlignment(VerticalAlignment.CENTER);  //垂直对齐方式

        cellStyle1.setBorderBottom(BorderStyle.THIN); //下边框 细线
        cellStyle1.setBorderLeft(BorderStyle.THIN);
        cellStyle1.setBorderRight(BorderStyle.THIN);
        cellStyle1.setBorderTop(BorderStyle.THIN);

//       设置样式:字体  黑体 12号
        Font font1 = workbook.createFont();
        font1.setFontName("黑体");
        font1.setFontHeightInPoints((short) 12);
        font1.setBold(false);
        cellStyle1.setFont(font1);

//        创建新的工作表sheet
        Sheet sheet = workbook.createSheet("POI测试字");
//        设置sheet的列宽
        sheet.setColumnWidth(0,4200); 
        sheet.setColumnWidth(1,26*256); //1代表一个字母宽度的256分之一
        sheet.setColumnWidth(2,16*256);
        sheet.setColumnWidth(3,26*256); 
        sheet.setColumnWidth(4,16*256); 
        sheet.setColumnWidth(5,16*256); 
        sheet.setColumnWidth(6,16*256);
        sheet.setColumnWidth(7,16*256); 
        sheet.setColumnWidth(8,16*256); 
        Row bigTitleRow = sheet.createRow(0);
//        创建单元格
        for (int i = 1; i <= 8; i++) {
            bigTitleRow.createCell(i);
        }
//        设置行高
        bigTitleRow.setHeightInPoints(36);
//        合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0,0,1,8));
//        向合并后的单元格中写入一句话
        Cell cell = bigTitleRow.getCell(1);
        cell.setCellValue("有样式的表");
        cell.setCellStyle(cellStyle);
//        创建小标题行
//        你的客户
        Row titleRow = sheet.createRow(1);
        Cell cell1 = titleRow.createCell(1);
        cell1.setCellValue("你的客户");
        cell1.setCellStyle(cellStyle1);

//        把excel输出到磁盘上
        workbook.write(new FileOutputStream("D://styleexcel.xlsx"));
    }
}

读取内容

一第一个为例:

package cn.itcsdn;

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.FileInputStream;
import java.io.FileOutputStream;

//读取一个Excel中的内容
public class POIDemo3 {
    public static void main(String[] args) throws Exception{
//        创建一个的工作薄
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("D://oneExcel.xlsx"));
//        获取一个工作表
        XSSFSheet sheet = workbook.getSheetAt(0);
//        获取第一行行
        XSSFRow row = sheet.getRow(0);
//        获取第一个单元格
        XSSFCell cell = row.getCell(0);
//        获取单元格中的内容
        String value = cell.getStringCellValue();
//		  打印输出
        System.out.println(value);
//   	释放资源
        workbook.close();
    }
}