一个简单的文档导出公共处理类

package com.common.export;

import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.List;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/**
 * 导入导出
 * @author dell
 *
 */
public class ExportOrImport {
    
    // 大标题样式
    static WritableFont bigTitleFont = new WritableFont(WritableFont.ARIAL, 10,
            WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE);
    static WritableCellFormat titleFormat = new WritableCellFormat(bigTitleFont);
/**
 * 导出数据
 * @param os
 * @param obj
 * @param head
 * @throws Exception
 */
    public static <T> void export(OutputStream os, List<T> obj, String[] head,String[] fidldName)
            throws Exception {
        // 创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(os);
        // 创建新的一页
        WritableSheet sheet = workbook.createSheet("First Sheet", 0);
        // 创建要显示的具体内容
        // String[] head = HeaderExcel.List;
        for (int i = 0; i < head.length; i++) {
            Label formate = new Label(i, 0, head[i] + "", titleFormat);
            sheet.addCell(formate);
        }
        int i = 0;
         Field  field  = null;
        for (T t : obj) {
             Object [] objw=new Object[50];
              int k = 0;
              //System.err.println(fidldName.length);
              if (fidldName==null) {
                  java.lang.reflect.Field[] fields = t.getClass().getDeclaredFields();
                  for(java.lang.reflect.Field f:fields){
                        //私有变量必须先设置Accessible为true
                            f.setAccessible(true);
                            if(f.get(t) != null)
                                objw[k]=f.get(t);
                            else
                                objw[k]="-";
                            k++;
                            }
              }else{
              for (int j = 0; j < fidldName.length; j++) {
                   field = t.getClass().getDeclaredField(fidldName[j]); 
                   field.setAccessible(true);
                   if(field.get(t) != null)
                        objw[k]=field.get(t);
                    else
                        objw[k]="-";
                    k++;
                    }
              }
            for (int j = 0; j < head.length; j++) {
                    Label data = new Label(j, 1 + i, "" + objw[j]);
                    sheet.addCell(data);
            }
            i++;
        }
        // 把创建的内容写入到输出流中,并关闭输出流
        workbook.write();
        workbook.close();
        os.close();
    }
}

调用方法:

//数据集合
List<Funture> list =(List<Funture>) map.get("funtureDate");
//表名称
String name="{"+pointName+"}数据查询导出数据";
String fileName = new String( (name).getBytes("GBK"), "iso-8859-1");
OutputStream os = new FileOutputStream(fileName + ".xls");
//实体字段
String[] SmartStrip={"createTime","deviceName"};
//表头
public static String[] smart={"时间","设备名称",};

ExportOrImport.export(os, list, smart,SmartStrip);

  

posted on 2016-11-25 17:35  Longevity  阅读(319)  评论(0编辑  收藏  举报