简单列表excel导出

package com.yuancredit.gathering.feedback.util;

import ****.TaskFeedBackExcel;// 实体类
import org.apache.poi.hssf.usermodel.HSSFRichTextString;// poi 帮助类
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.exception.excel.ExcelExportException;
import org.jeecgframework.poi.exception.excel.enums.ExcelExportEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class ExportExcelUtil {
    private static Logger logger = LoggerFactory.getLogger(ExportExcelUtil.class);

    public static void main(String[] args) throws Exception{
        String sheetName="testsheet";
        List<Title> titles = new ArrayList<>();
        Title t1 = new Title("名称", "cusName");
        Title t2 = new Title("电话", "cusMobile");
        Title t3 = new Title("状态", "cusStatus");
        titles.add(t1);
        titles.add(t2);
        titles.add(t3);

        List<TaskFeedBackExcel> list  = new ArrayList<>();
        for (int i=0;i<3;i++){
            TaskFeedBackExcel t = new TaskFeedBackExcel();
            t.setCusName("Nmae"+(i+1));
            t.setCusMobile("molibe"+(i+1));
            t.setOverdueStatus("status"+(i+1));
            list.add(t);
        }

        Workbook wb = exportExcel(sheetName, titles , list, TaskFeedBackExcel.class, ExcelType.HSSF);

        FileOutputStream out = new FileOutputStream("E:/test.xls");
        try {
            wb.write(out);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(out != null) out.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public static Workbook exportExcel(String sheetName, List<Title> titles, Collection<?> dataSet, Class<?> pojoClass, ExcelType type) {
        Object workbook;
        if (ExcelType.HSSF.equals(type)) {
            workbook = new HSSFWorkbook();
        } else if (dataSet.size() < 10000) {
            workbook = new XSSFWorkbook();
        } else {
            workbook = new SXSSFWorkbook();
        }

       createSheet((Workbook)workbook, sheetName, titles, dataSet, pojoClass, type);
        return (Workbook)workbook;
    }

    public static void createSheet(Workbook workbook, String sheetName, List<Title> titles, Collection<?> dataSet, Class<?> pojoClass, ExcelType type) {
        if (workbook != null && sheetName != null && titles != null && dataSet != null) {
            try {
                // sheet
                Sheet sheet = null;
                try {
                    sheet = workbook.createSheet(sheetName);
                } catch (Exception var7) {
                    sheet = workbook.createSheet();
                }
                int rowCount = 0;
                Row rowTitle = sheet.createRow(rowCount);
                // title
                for (int index=0; index < titles.size(); index++){
                    Title t = titles.get(index);
                    Cell cell = rowTitle.createCell(index);
                    RichTextString text = null;
                    if (type.equals(ExcelType.HSSF)) {
                        text = new HSSFRichTextString(t.name);
                    } else {
                        text = new XSSFRichTextString(t.name);
                    }
                    cell.setCellValue(text);
                }
                rowCount++;

                // 列表
                Method method = null;
                Iterator<?> its = dataSet.iterator();
                while(its.hasNext()) {
                    Object object = its.next();

                    Row rowData = sheet.createRow(rowCount);
                    for (int index = 0; index < titles.size(); index++){
                        Title t = titles.get(index);
                        Cell cell = rowData.createCell(index);

                        Object value = null;
                        try {
                            method = pojoClass.getMethod("get" + toUpperCase4Index(t.key));
                            value = method.invoke(object);
                        }catch (Exception e){
                            logger.error(e.getMessage());
                            e.printStackTrace();
                            cell.setCellValue("");
                            continue;
                        }

                        String str = objectToString(value);
                        RichTextString text = null;
                        if (type.equals(ExcelType.HSSF)) {
                            text = new HSSFRichTextString(str);
                        } else {
                            text = new XSSFRichTextString(str);
                        }
                        cell.setCellValue(text);
                    }
                }
            } catch (Exception var9) {
                throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, var9.getCause());
            }
        } else {
            throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
        }
    }

    public static String objectToString(Object object){
    if(object != null){// 可根据对象具体类型做转换
        object.toString();
    }
    return "";
    }
public static String toUpperCase4Index(String string) { char[] methodName = string.toCharArray() ; methodName[ 0] = toUpperCase(methodName[ 0]) ; return String. valueOf(methodName) ; } public static char toUpperCase( char chars) { if ( 97 <= chars && chars <= 122) { chars ^= 32 ; } return chars ; } public static class Title{ String name ; String key ; public Title(String name , String key) throws Exception { if(name == null || name.length() == 0){ throw new Exception( "参数异常") ; } if(key == null || key.length() == 0){ throw new Exception( "参数异常") ; } this. name = name ; this. key = key ; } }}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值