为了账号安全,请及时绑定邮箱和手机立即绑定

简化版poi工具

标签:
Java Python

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

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

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>3.17-beta1</version></dependency><dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.17-beta1</version></dependency>

-- 代码

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<String, String> headMap, List<Map<String, Object>> 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<String, String> headMap,

      List<Map<String, Object>> dataList) {

    Workbook workbook = new XSSFWorkbook();

    Sheet sheet = workbook.createSheet(sheetName);

    int rowIndex = 0, columnIndex = 0;    Set<String> 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<String, Object> 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<String, Object> data = new HashMap<String, Object>() {{


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

    List<Map<String, Object>> 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<String, Object>() {
      {
        put("createTime", "创建时间");
        put("updateTime", "更新时间");
        put("errorCode", "错误码");
      }
    };
    ExcelUtils.exportXlsx(outputStream, "testSheet", head, objects);
  }
}



作者:H_Man
链接:https://www.jianshu.com/p/21955d23e55d


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消