1. POI中Excel对象的结构

Workbook(相当于一个Excel)  --createSheet()-->  
Sheet(sheet页)            --createRow()-->   
Row(行对象)               --createCell()-->  
Cell(单元格)  
ooxml 依赖中支持HSSF 及 XSSF
Excel中主要的对象获取后,便可以对其做属性和值的设置,如下demo

2. 文件下载

/**
* Excel 自定义工具类
* @author yuanye.wong
*/
public class ExcelUtil {
    public static HSSFWorkbook createWorkbook(String sheetName, String[] header){
        if (Objects.isNull(header) || header.length == 0) {
            return null;
        }
        if(sheetName == null) {
            sheetName = new String("sheet1");
        }
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 设置 sheetName
        HSSFSheet sheet = workbook.createSheet(sheetName);
        sheet.setDefaultColumnWidth(18);
        // 标题样式
        HSSFCellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.index);
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // 创建标题行
        HSSFRow sheetRow = sheet.createRow(0);
        // 遍历添加表头
        for (int i = 0; i < header.length; i++) {
            // 创建一个单元格
            HSSFCell cell = sheetRow.createCell(i);
            // 创建一个内容对象
            HSSFRichTextString text = new HSSFRichTextString(header[i]);
            // 填充标题
            cell.setCellValue(text);
            cell.setCellStyle(headerStyle);
        }
        return workbook;
    }
}

// 下载数据模板
public HttpServletResponse generateTemplateDemo(HttpServletResponse response) {
    String[] titles = {"手机号","姓名","身份证"};
    String fileName = "数据模板.xlsx";
    // 设置response属性
    String fileNameURL = null;
    try {
        fileNameURL = URLEncoder.encode(fileName, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new AdminBusinessException(BusinessExceptionCode.FILE_NAME_ENCODE_ERROR);
    }
    // 设置contentType
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    // *** 设置header(处理文档名包含中文)
    response.setHeader("Content-disposition", "attachment;filename="+fileNameURL+";"+"filename*=utf-8''"+fileNameURL);
    // 生成Excel
    HSSFWorkbook workbook = ExcelUtil.createWorkbook(null, titles);
    // 按需装填数据
    /*List<Demo> list = getData();
    HSSFSheet sheet = workbook.getSheetAt(0);
    for (int i = 0; i < list.size(); i++) {
        //从第二行起
        HSSFRow row = sheet.createRow(i + 1);
        row.createCell(0).setCellValue(new HSSFRichTextString(details.get(i).getAttr01()));
        row.createCell(1).setCellValue(new HSSFRichTextString(details.get(i).getAttr02()));
        row.createCell(2).setCellValue(new HSSFRichTextString(details.get(i).getAttrXx()));
    }*/
    try {
        ServletOutputStream outputStream = response.getOutputStream();
        // 写入输出流
        workbook.write(outputStream);
        outputStream.close();
        outputStream.flush();
    } catch (IOException e) {
        log.info("下载数据模板出错:{}",e.getClass().getName());
    }
    return response;
}

3. 文件上传

  1. 接口使用 form-data 传参,使用 MultipartFile 接收文件对象
  2. MultipartFile 转为输入流,构造 Workbook
  3. 根据POI中Excel对象结构,依次获取Sheet,Row,Cell迭代器对象
  4. 遍历Cell迭代器获取每个cell的值
// *** 设置cellType,避免类型转换报错
cell.setCellType(CellType.STRING);
entity.setPhone(cell.getStringCellValue());

罗曼蒂克在消亡
15 声望1 粉丝

I put a flower in your hair...