poi導出excel例子

package com.ue.jeebase.modules.ysj.service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

public class Test {
    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws IOException {

        SXSSFWorkbook wb = new SXSSFWorkbook(500);
        Sheet sheet = wb.createSheet("Export");



        CellStyle style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_CENTER);

        Row titleRow = sheet.createRow(0);
        Cell cell = titleRow.createCell(0);
        cell.getSheet().addMergedRegion(new CellRangeAddress(0, 0, 0, 20));
        cell.setCellValue("test");
        cell.setCellStyle(style);

        Row titleRow1 = sheet.createRow(1);
        Cell cell0 = titleRow1.createCell(0);
        cell0.getSheet().addMergedRegion(new CellRangeAddress(1, 2, 0, 0));
        cell0.setCellValue("test");
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 垂直居中
        cell0.setCellStyle(style);

        Cell cell1 = titleRow1.createCell(1);
        cell1.getSheet().addMergedRegion(new CellRangeAddress(1, 1, 1, 5));
        cell1.setCellValue("test");
        cell1.setCellStyle(style);

        Cell cell2 = titleRow1.createCell(6);
        cell2.getSheet().addMergedRegion(new CellRangeAddress(1, 1, 6, 11));
        cell2.setCellValue("test");
        cell2.setCellStyle(style);

        Cell cell3 = titleRow1.createCell(12);
        cell3.getSheet().addMergedRegion(new CellRangeAddress(1, 1, 12, 14));
        cell3.setCellValue("test");
        cell3.setCellStyle(style);

        Cell cell4 = titleRow1.createCell(15);
        cell4.getSheet().addMergedRegion(new CellRangeAddress(1, 1, 15, 20));
        cell4.setCellValue("test");
        cell4.setCellStyle(style);

        String[] str = { "test", "test", "test", "test", "test", "test",
                "test", "test", "test", "test", "test", "test", "test", "test",
                "test", "test", "test", "test", "test", "test" };
        Row titleRow2 = sheet.createRow(2);
        CellStyle style2 = wb.createCellStyle();
        style2.setAlignment(CellStyle.ALIGN_CENTER);
        titleRow2.setHeight((short) 970);
        style2.setWrapText(true);
        for (int i = 0; i < str.length; i++) {
            Cell cellTitle = titleRow2.createCell(i + 1);
            cellTitle.setCellValue(str[i]);
            cellTitle.setCellStyle(style2);
        }

        TestExcel t = new TestExcel();
        setValue(TestExcel.class, t);
        List<String> data = getValue(TestExcel.class, t);
        List<List<String>> lists = new ArrayList<List<String>>();
        lists.add(data);
        for (int i = 0; i < lists.size(); i++) {
            Row row = sheet.createRow(i + 3);
            for (int j = 0; j < data.size(); j++) {
                Cell cel = row.createCell(j);
                cel.setCellValue(lists.get(i).get(j));
                cel.setCellStyle(style2);
            }
        }
        FileOutputStream out = new FileOutputStream("D:\\test.xlsx");
        wb.write(out);
        out.close();
        wb.dispose();

        System.out.println("Export success.");
    }

    @SuppressWarnings("all")
    public static List<String> getValue(Class clazz, Object obj) {
        try {
            List<String> data = new ArrayList<String>();
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field field : declaredFields) {
                String name = field.getName();
                name = name.substring(0, 1).toUpperCase() + name.substring(1);
                Method declaredMethod = clazz.getMethod("get" + name);
                Object invoke = declaredMethod.invoke(obj);
                data.add((String) invoke);
            }
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("all")
    public static void setValue(Class clazz, Object obj) {
        try {
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field field : declaredFields) {
                String name = field.getName();
                name = name.substring(0, 1).toUpperCase() + name.substring(1);
                Method declaredMethod;
                declaredMethod = clazz.getDeclaredMethod("set" + name,
                        String.class);
                declaredMethod.setAccessible(true);
                declaredMethod.invoke(obj, "1");
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

class TestExcel {
    private String test;

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章