简化版poi工具

发现自己一年之前的poi工具太重,并没有实现最基础的功能.
又重新抽了一个基础版的poi工具.
最基本的,导入到excel,存入本机

-- pom(这个直接在maven上搜不知道为啥搜不到)


  org.apache.poi
  poi-ooxml
  3.17-beta1


  org.apache.poi
  poi
  3.17-beta1

-- 代码

import [com.google.common.collect.Lists]

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.sql.Timestamp;

import java.util.Calendar;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Set;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * @author: hman
 * @date 2018-11-16
 * @desc: poi工具
 */
public class ExcelUtils {

  /**

   * @param sheetName excel文件名

   * @param headMap 表头map

   * @param dataList 表格数据

   */

  public static void exportXlsx(OutputStream outputStream, String sheetName,

      Map headMap, List> dataList) {

    Workbook workbook = exportXlsx(sheetName, headMap, dataList);

    try {

      workbook.write(outputStream);

    } catch (Exception e) {

      e.printStackTrace();

    } finally {

      if (outputStream != null) {

        try {

          outputStream.close();

        } catch (IOException e) {

          e.printStackTrace();

        }

      }

    }

  }

  /**

   * 导出数据

   */

  public static Workbook exportXlsx(String sheetName, Map headMap,

      List> dataList) {

    Workbook workbook = new XSSFWorkbook();

    Sheet sheet = workbook.createSheet(sheetName);

    int rowIndex = 0, columnIndex = 0;

    Set keys = headMap.keySet();

    //表头

    Row row = sheet.createRow(rowIndex++);

    for (String key : keys) {

      Cell cell = row.createCell(columnIndex++);

      cell.setCellValue(headMap.get(key));

    }

    //内容

    if (dataList != null && !dataList.isEmpty()) {

      for (Map map : dataList) {

        row = sheet.createRow(rowIndex++);

        columnIndex = 0;

        for (String key : keys) {

          Cell cell = row.createCell(columnIndex++);

          setCellValue(cell, map.get(key));

        }

      }

    }

    return workbook;

  }

  private static void setCellValue(Cell cell, Object obj) {

    if (obj == null) {

      return;

    }

    if (obj instanceof String) {

      cell.setCellValue((String) obj);

    } else if (obj instanceof Date) {

      Date date = (Date) obj;

      if (date != null) {

        cell.setCellValue(DateUtil.formatToDate(date.getTime()));

      }

    } else if (obj instanceof Calendar) {

      Calendar calendar = (Calendar) obj;

      if (calendar != null) {

        cell.setCellValue(DateUtil.formatToDate(calendar.getTimeInMillis()));

      }

    } else if (obj instanceof Timestamp) {

      Timestamp timestamp = (Timestamp) obj;

      if (timestamp != null) {

        cell.setCellValue(DateUtil.formatDateStrTime(timestamp.getTime()));

      }

    } else if (obj instanceof Long) {

      Long longstr = (Long) obj;

      if (longstr != null) {

        cell.setCellValue(DateUtil.formatToDate(longstr));

      }

    }  else if (obj instanceof Double) {

      cell.setCellValue((Double) obj);

    } else {

      cell.setCellValue(obj.toString());

    }

  }

}

-- 自己测试

public static void main(String[] args) throws FileNotFoundException {
    Map data = new HashMap() {{


      put("createTime", 1);
      put("updateTime", 1);
      put("errorCode", 1);
    }};

    List> objects = Lists.newArrayList();
    objects.add(data);

    String dateStrTime = DateUtil.formatDateStrTime(System.currentTimeMillis() / 1000);
    File file = new File(
        "/Users/momo/Downloads/test" + ".xls");
    if (file.exists()) {
      file.delete();
    }
    OutputStream outputStream = new FileOutputStream(file);

    Map head = new HashMap() {
      {
        put("createTime", "创建时间");
        put("updateTime", "更新时间");
        put("errorCode", "错误码");
      }
    };
    ExcelUtils.exportXlsx(outputStream, "testSheet", head, objects);
  }
}

你可能感兴趣的:(简化版poi工具)