zoukankan      html  css  js  c++  java
  • Java Excel导出

    package com.gome.budget.common.utils;
    
    import com.alibaba.excel.EasyExcelFactory;
    import com.alibaba.excel.ExcelWriter;
    import com.alibaba.excel.metadata.Sheet;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    public class ExcelExportUtil {
    
        public static void main(String[] args) throws FileNotFoundException {
    
            List<List<String>> header = createTestListStringHead();
            List<List<Object>> dataLists = createTestListObject();
    
            OutputStream os = new FileOutputStream("D:\share\expense.xlsx");
            ExcelExportUtil exporter = new ExcelExportUtil();
            exporter.initWriter(os);
            exporter.createSheet(1, 0, "损益表2", header)
                    .writeDatas(dataLists);
    
            exporter.createSheet(2, 0, "费用表2", header)
                    .writeDatas(dataLists);
    
            exporter.close();
        }
    
        public ExcelExportUtil() {
        }
        public static List<List<String>> createTestListStringHead() {
            List<List<String>> header = new ArrayList<List<String>>();
            List<String> headList = new ArrayList<String>();
            headList.add("预算组织");
            header.add(headList);
    
            headList = new ArrayList<String>();
            headList.add("项目");
            header.add(headList);
    
            headList = new ArrayList<String>();
            headList.add("前2个考核期实际数");
            header.add(headList);
    
            headList = new ArrayList<String>();
            headList.add("前1个考核期发生月实际数");
            header.add(headList);
    
            headList = new ArrayList<String>();
            headList.add("前1个考核期未发生月预测数");
            header.add(headList);
    
            headList = new ArrayList<String>();
            headList.add("前1个考核期预测数");
            header.add(headList);
    
            headList = new ArrayList<String>();
            headList.add("当前考核期上报数");
            header.add(headList);
    
            headList = new ArrayList<String>();
            headList.add("同比");
            header.add(headList);
    
            headList = new ArrayList<String>();
            headList.add("环比");
            header.add(headList);
    
            return header;
        }
        public static List<List<Object>> createTestListObject() {
            List<List<Object>> dataList = new ArrayList<List<Object>>();
            List<Object> data = new ArrayList<Object>();
            data.add("电器集团");
            data.add("一、销售净额");
            data.add("33977039584.3376");
            data.add("21934607354.7551");
            data.add("13185382748.9218");
            data.add("35119990103.6769");
            data.add("37225695903.0281");
            data.add("6.00%");
            data.add("9.56%");
            dataList.add(data);
    
            data = new ArrayList<Object>();
            data.add("电器集团");
            data.add("减:销售折扣");
            data.add("-1058035879.32");
            data.add("2410985173.15");
            data.add("59573049.4876506");
            data.add("2470558222.63765");
            data.add("181020526.822908");
            data.add("-92.67%");
            data.add("117.11%");
            dataList.add(data);
    
    
            return dataList;
        }
    
        private ExcelWriter writer;
        private List<Sheet> sheets = new ArrayList<Sheet>();
    
        /**
         * 初始化writer
         * @param os
         */
        public ExcelExportUtil initWriter(OutputStream os) {
            writer = EasyExcelFactory.getWriter(os);
    
            return this;
        }
    
        public ExcelExportUtil createSheet(int sheetNo, int headLineMun, String sheetName, List<List<String>> header) {
            Sheet sheet1 = new Sheet(sheetNo, headLineMun);
            sheet1.setSheetName(sheetName);
            sheet1.setHead(header);
            sheet1.setAutoWidth(Boolean.TRUE);
    
    
            this.sheets.add(sheet1);
    
            return this;
        }
    
        /**
         * 关闭资源
         */
        public void close() {
            writer.finish();
        }
    
        public ExcelExportUtil writeDatas(List<List<Object>> dataLists) {
            //写第一个sheet, sheet1  数据全是List<String> 无模型映射关系
            for (Sheet sheet :
                    sheets) {
                writer.write1(dataLists, sheet);
            }
    
            return this;
        }
    
        public ExcelWriter getWriter() {
            return writer;
        }
    
        public void setWriter(ExcelWriter writer) {
            this.writer = writer;
        }
    
        public List<Sheet> getSheets() {
            return sheets;
        }
    
        public void setSheets(List<Sheet> sheets) {
            this.sheets = sheets;
        }
    }
  • 相关阅读:
    仿网易菜单 实现侧滑 SlidingMenu
    MD5 Util
    Android 关于SD卡、机身内存以及分辨率的转换的工具类
    android TextView 显示图片,类似于聊天窗口。
    WEB显示(隐藏)系统时间
    I/O复习四 字符流 InputStreamReader/OutputStreamWriter
    Knockout应用开发指南(完整版) 目录索引
    C#设计模式(23种设计模式)
    win7+ubuntu 13.04双系统安装方法
    GeoServer地图开发解决方案
  • 原文地址:https://www.cnblogs.com/honghong75042/p/11047759.html
Copyright © 2011-2022 走看看