Apache POI教程的使用

一、Apache POI简介

Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

HSSF 是Horrible SpreadSheet Format的缩写,通过HSSF,你可以用纯Java代码来读取、写入、修改Excel文件。

HSSF 为读取操作提供了两类API:usermodel和eventusermodel,即“用户模型”和“事件-用户模型”

二、POI结构说明

  • HSSF提供读写Microsoft Excel XLS格式档案的功能。
  • XSSF提供读写Microsoft Excel OOXML XLSX格式档案的功能。
  • HWPF提供读写Microsoft Word DOC格式档案的功能。
  • HSLF提供读写Microsoft PowerPoint格式档案的功能。
  • HDGF提供读Microsoft Visio格式档案的功能。
  • HPBF提供读Microsoft Publisher格式档案的功能。
  • HSMF提供读Microsoft Outlook格式档案的功能

三、常用组件

类名说明
HSSFWorkbookExcel的文档对象
HSSFSheetExcel的表单
HSSFRowExcel的行
HSSFCellExcel的格子单元
HSSFFontExcel字体
HSSFDataFormat格子单元的日期格式
HSSFHeaderExcel文档Sheet的页眉
HSSFFooterExcel文档Sheet的页脚
HSSFCellStyle格子单元样式
HSSFDateUtil日期
HSSFPrintSetup打印
HSSFErrorConstants错误信息表

引入pom文件

 <!-- Excel 导入导出-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.8</version>
        </dependency>

xls 格式的工具类

仅供参考

package com.user.base.util.Excel.POI;

import com.user.base.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :mmzs
 * @date :Created in 2020/3/26 16:36
 * @description:poi导入导出Excel,Excel有.xls,2003最多只允许存储65536条数据,HSSFWorkbook
 * @modified By:xphy
 * @version: 1$
 */
@Slf4j
public class ExcelXlsPoiUtils {

    /***
     * @description: poi导出excel
     * @param response 响应
     * @param path 生成的文件路径
     * @param fileName 文件名
     * @param sheetName 工作蒲名
     * @param headers 表格头部
     * @param data 表格数据
     * @return: void
     * @author: Andy
     * @time: 2020/3/27 14:40
     */
    public static void Export(HttpServletResponse response, String path, String fileName,String sheetName,String[] headers, List<String[]> data) throws IOException {
        //设置文件名称
        String fileNameTime = getFileName(path, fileName);
        //实例化HSSFWorkbook
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建一个Excel表单,参数为sheet的名字
        HSSFSheet sheet = workbook.createSheet("sheet");
        //设置表头
        setTitle(workbook, sheet, headers);
        //设置单元格并赋值
        setData(sheet, data);
        //设置浏览器下载
        setBrowser(response, workbook, fileNameTime+".xls");
        log.info("导出解析成功!");
    }

    /**
     * 方法名:setTitle
     * 功能:设置表头
     * 描述:
     * 创建人:typ
     * 创建时间:2018/10/19 10:20
     * 修改人:
     * 修改描述:
     * 修改时间:
     */
    private static void setTitle(HSSFWorkbook workbook, HSSFSheet sheet, String[] str) {
        try {
            //创建表头
            HSSFRow row = sheet.createRow(0);
            //设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度
            for (int i = 0; i <= str.length; i++) {
                sheet.setColumnWidth(i, 15 * 256);
            }
            //设置为居中加粗,格式化时间格式
            HSSFCellStyle style = workbook.createCellStyle();
            // 设置字体
            Font font = workbook.createFont();
            // 设置字体大小
            font.setFontHeightInPoints((short) 11);
            // 字体加粗
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            // 设置字体名字
            font.setFontName("Courier New");
            style.setWrapText(true);// 设置自动换行
            style.setFont(font);
            style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
            //创建表头名称
            HSSFCell cell;
            for (int j = 0; j < str.length; j++) {
                cell = row.createCell(j);
                cell.setCellValue(str[j]);
                cell.setCellStyle(style);
            }
        } catch (Exception e) {
            log.info("导出时设置表头失败!");
            e.printStackTrace();
        }
    }

    /**
     * 方法名:setData
     * 功能:表格赋值
     * 描述:
     * 创建人:typ
     * 创建时间:2018/10/19 16:11
     * 修改人:
     * 修改描述:
     * 修改时间:
     */
    private static void setData(HSSFSheet sheet, List<String[]> data) {
        try {
            int rowNum = 1;
            for (int i = 0; i < data.size(); i++) {
                HSSFRow row = sheet.createRow(rowNum);
                for (int j = 0; j < data.get(i).length; j++) {
                    row.createCell(j).setCellValue(data.get(i)[j]);
                }
                rowNum++;
            }
            log.info("表格赋值成功!");
        } catch (Exception e) {
            log.info("表格赋值失败!");
            e.printStackTrace();
        }
    }

    /***
     * @description: 输出到浏览器
     * @param response 相应
     * @param workbook 文件内容
     * @param fileName 文件名字
     * @return: void
     * @author: Andy
     * @time: 2020/3/26 17:31
     */
    private static void setBrowser(HttpServletResponse response, HSSFWorkbook workbook, String fileName) {
        try {
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
            response.flushBuffer();
            OutputStream out = response.getOutputStream();
            workbook.write(out);// 将数据写出去
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 生成excel文件
     * @param path 生成excel路径
     * @param fileName 文件名
     */
    private static String getFileName (String path,String fileName){
        String resultName = fileName;
        long  timeNew =  System.currentTimeMillis()/ 1000;
        File file = new File(path);
        if (file.exists()) {
            //如果文件存在
            resultName =  resultName + timeNew;
            //file.delete();
        }
      return resultName;
    }
    /***
     * @description: excel导入
     * @param filePath 文件路径
     * @return: java.util.List<java.lang.Object[]>
     * @author: Andy
     * @time: 2020/3/27 15:06
     */
    public static List<Object[]> importExcel(String filePath) {
        log.info("导入解析开始,fileName:{}",filePath);
        if(StringUtil.isEmpty(filePath)){
            //导入文件不存在
            return null;
        }
        try {
            List<Object[]> list = new ArrayList<>();
            //获得输入流
            InputStream inputStream = new FileInputStream(filePath);
            //获取workbook对象
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            //获取sheet的行数
            int rows = sheet.getPhysicalNumberOfRows();
            for (int i = 0; i < rows; i++) {
                //过滤表头行
                if (i == 0) {
                    continue;
                }
                //获取当前行的数据
                Row row = sheet.getRow(i);
                Object[] objects = new Object[row.getPhysicalNumberOfCells()];
                int index = 0;
                for (Cell cell : row) {
                    if (cell.getCellType()==0) {
                        objects[index] = (int) cell.getNumericCellValue();
                    }
                    if (cell.getCellType() == 1) {
                        objects[index] = cell.getStringCellValue();
                    }
                    if (cell.getCellType()== 4) {
                        objects[index] = cell.getBooleanCellValue();
                    }
                    if (cell.getCellType()==5) {
                        objects[index] = cell.getErrorCellValue();
                    }
                    index++;
                }
                list.add(objects);
            }
            inputStream.close();//是否需要关闭
            log.info("导入文件解析成功!");
            return list;
        }catch (Exception e){
            log.info("导入文件解析失败!");
            e.printStackTrace();
        }
        return null;
    }

    //测试导入
    public static void main(String[] args) {
        try {
            String fileName = "D:/export/文件名.xls";
            List<Object[]> list = importExcel(fileName);
//            for (int i = 0; i < list.size(); i++) {
//                User user = new User();
//                user.setId((Integer) list.get(i)[0]);
//                user.setUsername((String) list.get(i)[1]);
//                user.setPassword((String) list.get(i)[2]);
//                user.setEnable((Integer) list.get(i)[3]);
//                System.out.println(user.toString());
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

设置单元格的样式

/*
   * 列头单元格样式
   */
  public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
    // 设置字体
    HSSFFont font = workbook.createFont();

    // 设置字体大小
    font.setFontHeightInPoints((short) 11);
    // 字体加粗
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    // 设置字体名字
    font.setFontName("Courier New");
    // 设置样式
    HSSFCellStyle style = workbook.createCellStyle();
    // 设置低边框
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    // 设置低边框颜色
    style.setBottomBorderColor(HSSFColor.BLACK.index);
    // 设置右边框
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
    // 设置顶边框
    style.setTopBorderColor(HSSFColor.BLACK.index);
    // 设置顶边框颜色
    style.setTopBorderColor(HSSFColor.BLACK.index);
    // 在样式中应用设置的字体
    style.setFont(font);
    // 设置自动换行
    style.setWrapText(false);
    // 设置水平对齐的样式为居中对齐;
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    return style;

  }

  public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
    // 设置字体
    HSSFFont font = workbook.createFont();
    // 设置字体大小
    font.setFontHeightInPoints((short) 10);
    // 字体加粗
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    // 设置字体名字
    font.setFontName("Courier New");
    // 设置样式;
    HSSFCellStyle style = workbook.createCellStyle();
    // 设置底边框;
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    // 设置底边框颜色;
    style.setBottomBorderColor(HSSFColor.BLACK.index);
    // 设置左边框;
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    // 设置左边框颜色;
    style.setLeftBorderColor(HSSFColor.BLACK.index);
    // 设置右边框;
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
    // 设置右边框颜色;
    style.setRightBorderColor(HSSFColor.BLACK.index);
    // 设置顶边框;
    style.setBorderTop(HSSFCellStyle.BORDER_THIN);
    // 设置顶边框颜色;
    style.setTopBorderColor(HSSFColor.BLACK.index);
    // 在样式用应用设置的字体;
    style.setFont(font);
    // 设置自动换行;
    style.setWrapText(false);
    // 设置水平对齐的样式为居中对齐;
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    // 设置垂直对齐的样式为居中对齐;
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    return style;
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值