一個簡單的文檔導出公共處理類

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();
    }
}

調用方法:java

//數據集合
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);
相關文章
相關標籤/搜索