栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

简化版poi工具

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简化版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;public class ExcelUtils {  

  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);
  }
}



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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/222187.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号