Java使用POI导出数据到Excel

需要引用的POM依赖如下:


            org.apache.poi
            poi
            3.15
        
        
            org.apache.poi
            poi-ooxml-schemas
            3.15
        
        
            org.apache.poi
            poi-ooxml
            3.15
        

具体工具类以及测试方法如下:

package com.tyky.jeesite.modules.selfServiceSystem.common.utils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 * 导出Excel公共方法
 * @version 1.0
 *
 * @author chance
 *
 */
public class POIExcelUtils {

    //显示的导出表的标题
    private String title;
    //导出表的列名
    private String[] rowName ;

    private List  dataList = new ArrayList();

    //构造方法,传入要导出的数据
    public POIExcelUtils(String title,String[] rowName,List  dataList){
        this.dataList = dataList;
        this.rowName = rowName;
        this.title = title;
    }

    /*
     * 导出数据
     * */
    public void export() throws Exception{
        try{
            HSSFWorkbook workbook = new HSSFWorkbook();                        // 创建工作簿对象
            HSSFSheet sheet = workbook.createSheet(title);                     // 创建工作表

            // 产生表格标题行
            HSSFRow rowm = sheet.createRow(0);
            HSSFCell cellTiltle = rowm.createCell(0);

            //sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面  - 可扩展】
            HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象
            HSSFCellStyle style = this.getStyle(workbook);                    //单元格样式对象

            sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1)));
            cellTiltle.setCellStyle(columnTopStyle);
            cellTiltle.setCellValue(title);

            // 定义所需列数
            int columnNum = rowName.length;
            HSSFRow rowRowName = sheet.createRow(2);                // 在索引2的位置创建行(最顶端的行开始的第二行)

            // 将列头设置到sheet的单元格中
            for(int n=0;n json2List(JSONArray dataArray){
        List  dataList = new ArrayList();
        for(int i = 0;i < dataArray.size();i++){
            Object[] objs = new Object[4];
            JSONObject obj = (JSONObject) dataArray.get(i);
            JSONObject t = obj.getJSONObject("columns");
            objs[0] = i;
            objs[1] = t.getString("ITEM_ID");//事项id
            objs[2] = t.getString("NAME");//事项名称
            objs[3] = t.getString("ORG_NAME"); //部门名称
            dataList.add(objs);
        }
        return dataList;
    }

    public static void main(String[] args) throws Exception {
        List> listMap = new ArrayList<>();
        HashMap dataMap = new HashMap<>();

        for(int i=0;i<30;i++){
            dataMap.put("datetime", "2017-12-13 10:43:00");
            dataMap.put("person", "张翠山");
            dataMap.put("type", "文本");
            dataMap.put("content", "工作一定要认真,态度要端正,作风要优良,行事要效率,力争打造一个完美的产品出来。");
            listMap.add(dataMap);
        }

        String title = "张翠山的发言记录";
        String[] rowsName = new String[]{"序号","时间","发言人","类型","消息"};
        List  dataList = new ArrayList();
        Object[] objs = null;
        for (int i = 0; i < listMap.size(); i++) {
            HashMap data = listMap.get(i);
            objs = new Object[rowsName.length];
            objs[0] = i;
            objs[1] = data.get("datetime");
            objs[2] = data.get("person");
            objs[3] = data.get("type");
            objs[4] = data.get("content");
            dataList.add(objs);
        }
        POIExcelUtils ex = new POIExcelUtils(title, rowsName, dataList);
        ex.export();
    }


}

个人笔记,方便下次使用的时候直接拷贝

你可能感兴趣的:(JavaWeb基础,JSON)