guanxf

          我的博客:http://blog.sina.com.cn/17learning

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            71 隨筆 :: 1 文章 :: 41 評論 :: 0 Trackbacks
          package org.jeecgframework.core.util.excel;
          import java.awt.image.BufferedImage;
          import java.io.ByteArrayOutputStream;
          import java.io.File;
          import java.io.IOException;
          import java.lang.reflect.Field;
          import java.lang.reflect.Method;
          import java.lang.reflect.ParameterizedType;
          import java.util.ArrayList;
          import java.util.Collection;
          import java.util.Collections;
          import java.util.Iterator;
          import java.util.List;
          import java.util.Map;
          import javax.imageio.ImageIO;
          import org.apache.poi.hssf.usermodel.HSSFCellStyle;
          import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
          import org.apache.poi.hssf.usermodel.HSSFRichTextString;
          import org.apache.poi.hssf.usermodel.HSSFWorkbook;
          import org.apache.poi.hssf.util.HSSFColor;
          import org.apache.poi.ss.usermodel.Cell;
          import org.apache.poi.ss.usermodel.CellStyle;
          import org.apache.poi.ss.usermodel.Drawing;
          import org.apache.poi.ss.usermodel.Font;
          import org.apache.poi.ss.usermodel.RichTextString;
          import org.apache.poi.ss.usermodel.Row;
          import org.apache.poi.ss.usermodel.Sheet;
          import org.apache.poi.ss.util.CellRangeAddress;
          import org.jeecgframework.core.annotation.excel.Excel;
          import org.jeecgframework.core.annotation.excel.ExcelCollection;
          import org.jeecgframework.core.annotation.excel.ExcelEntity;
          import org.jeecgframework.core.annotation.excel.ExcelTarget;
          import org.jeecgframework.core.util.excel.entity.ComparatorExcelField;
          import org.jeecgframework.core.util.excel.entity.ExcelExportEntity;
          import org.jeecgframework.core.util.excel.entity.ExcelTitle;
          /**
           * excel 導出工具類
           * 
           * @author guanxf
           * 解決問題:
           * 1、導出Excel分sheet頁的問題
           * 2、繪制表格失敗的問題
           */
          public class ExcelExportUtil {
          private static int SHEET_MAX_NUM=1000;
          /**
          * 一個excel 創建多個sheet
          * @param list
          *            多個Map key title 對應表格Title key entity 對應表格對應實體 key data
          *            Collection 數據
          * @return
          */
          public static HSSFWorkbook exportExcel(List<Map<String, Object>> list) {
          HSSFWorkbook workbook = new HSSFWorkbook();
          for (Map<String, Object> map : list) {
          createSheetInUserModel2File(workbook,
          (ExcelTitle) map.get("title"),
          (Class<?>) map.get("entity"),
          (Collection<?>) map.get("data"));
          }
          return workbook;
          }
          /**
          * @param entity
          *            表格標題屬性
          * @param pojoClass
          *            Excel對象Class
          * @param dataSet
          *            Excel對象數據List
          * @param out
          *            輸出流
          */
          public static HSSFWorkbook exportExcel(ExcelTitle entity,
          Class<?> pojoClass, Collection<?> dataSet) {
          HSSFWorkbook workbook = new HSSFWorkbook();
          // createSheetInUserModel2File(workbook, entity, pojoClass, dataSet);
          //創建Sheet
          createSheetInExcel(workbook, entity, pojoClass, dataSet);
          return workbook;
          }
            /**
             * 創建sheet頁
             * @param workbook
             * @param entity
             * @param pojoClass
             * @param dataSet
             */
          private static void createSheetInExcel(HSSFWorkbook workbook,
          ExcelTitle entity, Class<?> pojoClass, Collection<?> dataSet) {
          Sheet sheet =null;
          List<Object> dataSetDataBySheet=new ArrayList<Object>();
          HSSFCellStyle style = workbook.createCellStyle();
          HSSFCellStyle titleStyle = workbook.createCellStyle();
          int index=0;
          int sheetNo=0;//sheet頁的編號
          Iterator<?> its = dataSet.iterator();
          while (its.hasNext()) {
          Object t = its.next();
          dataSetDataBySheet.add(t);
          if(index%SHEET_MAX_NUM==0 && index>0){ 
          ++sheetNo;
          sheet= workbook.createSheet(entity.getSheetName()+"_"+sheetNo);
          createUserModel2File(workbook, sheet,style, titleStyle, entity, pojoClass, dataSetDataBySheet);
          dataSetDataBySheet=new ArrayList<Object>();
             }
          index++;
          }
          //創建最后一頁的數據
          sheet= workbook.createSheet(entity.getSheetName()+"_"+(sheetNo+1));
          createUserModel2File(workbook, sheet,style, titleStyle, entity, pojoClass, dataSetDataBySheet);
          }
          /***
           * 根據sheet頁生成數據
           * @param sheet
           * @param entity
           * @param pojoClass
           * @param dataSet
           */
          private static void createUserModel2File(HSSFWorkbook workbook,Sheet sheet,HSSFCellStyle style,HSSFCellStyle titleStyleSet, 
          ExcelTitle entity, Class<?> pojoClass, Collection<?> dataSet) {
          try {
          Drawing patriarch = sheet.createDrawingPatriarch(); 
          List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>();
          // 得到所有字段
          Field fileds[] = getClassFields(pojoClass);
          ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
          String targetId = null;
          if (etarget != null) {
          targetId = etarget.id();
          }
          getAllExcelField(targetId, fileds, excelParams, pojoClass, null);
          sortAllParams(excelParams);
          int index = 0;
          int feildWidth = getFieldWidth(excelParams);
          if (entity.getTitle() != null) {
          int i =  createHeaderRow(entity, sheet, workbook, feildWidth);
          sheet.createFreezePane(0, 2+i, 0, 2+i);
          index += i;
          } else {
          sheet.createFreezePane(0, 2, 0, 2);
          }
          createTitleRow(entity,sheet, workbook,titleStyleSet, index, excelParams);
          index += 2;
          setCellWith(excelParams, sheet);
          Iterator<?> its = dataSet.iterator();
          while (its.hasNext()) {
          Object t = its.next();
          index += createCells(patriarch,index, t, excelParams, sheet, workbook,style);
          }
          } catch (Exception e) {
          e.printStackTrace();
          }
          }
          private static void createSheetInUserModel2File(HSSFWorkbook workbook,
          ExcelTitle entity, Class<?> pojoClass, Collection<?> dataSet) {
          try {
          Sheet sheet = workbook.createSheet(entity.getSheetName());
          HSSFCellStyle style = workbook.createCellStyle();
          Drawing patriarch = sheet.createDrawingPatriarch(); 
          List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>();
          // 得到所有字段
          Field fileds[] = getClassFields(pojoClass);
          ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
          String targetId = null;
          if (etarget != null) {
          targetId = etarget.id();
          }
          getAllExcelField(targetId, fileds, excelParams, pojoClass, null);
          sortAllParams(excelParams);
          int index = 0;
          int feildWidth = getFieldWidth(excelParams);
          if (entity.getTitle() != null) {
          int i =  createHeaderRow(entity, sheet, workbook, feildWidth);
          sheet.createFreezePane(0, 2+i, 0, 2+i);
          index += i;
          } else {
          sheet.createFreezePane(0, 2, 0, 2);
          }
          HSSFCellStyle titleStyle = workbook.createCellStyle();
          createTitleRow(entity,sheet, workbook,titleStyle, index, excelParams);
          index += 2;
          setCellWith(excelParams, sheet);
          Iterator<?> its = dataSet.iterator();
          while (its.hasNext()) {
          Object t = its.next();
          index += createCells(patriarch,index, t, excelParams, sheet, workbook,style);
          }
          } catch (Exception e) {
          e.printStackTrace();
          }
          }
          /**
          * 對字段根據用戶設置排序
          */
          private static void sortAllParams(List<ExcelExportEntity> excelParams) {
          Collections.sort(excelParams,new ComparatorExcelField());
          for(ExcelExportEntity entity:excelParams){
          if(entity.getList() != null){
          Collections.sort(entity.getList(),new ComparatorExcelField());
          }
          }
          }
          /**
          * 創建 最主要的 Cells
          * @throws Exception
          */
          private static int createCells(Drawing patriarch, int index, Object t,
          List<ExcelExportEntity> excelParams, Sheet sheet,
          HSSFWorkbook workbook, HSSFCellStyle style) throws Exception {
          ExcelExportEntity entity;
          Row row = sheet.createRow(index);
          row.setHeight((short) 350);
          int maxHeight = 1, cellNum = 0;
                 /* //避免創建的style太多,把style的創建提到循環外面
                  HSSFCellStyle style = workbook.createCellStyle();*/
          for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
          entity = excelParams.get(k);
          if (entity.getList() != null) {
          Collection<?> list = (Collection<?>) entity.getGetMethod()
          .invoke(t, new Object[] {});
          int listC = 0;
          for (Object obj : list) {
          createListCells(patriarch,index + listC, cellNum, obj,
          entity.getList(), sheet, workbook);
          listC++;
          }
          cellNum += entity.getList().size();
          if (list.size() > maxHeight) {
          maxHeight = list.size();
          }
          } else {
          Object value = entity.getGetMethods() != null ? getFieldBySomeMethod(
          entity.getGetMethods(), t) : entity.getGetMethod()
          .invoke(t, new Object[] {});
          if (entity.getType() == 1) {
          createStringCell(row, cellNum++,
          value == null ? "" : value.toString(),
          index % 2 == 0 ? getTwoStyle(workbook,entity,style)
          : getOneStyle(workbook,entity,style),entity);
          } else {
          createImageCell(patriarch,entity, row, cellNum++, value == null ? ""
          : value.toString());
          }
          }
          }
          //合并需要合并的單元格
          cellNum = 0;
          for(int k = 0, paramSize = excelParams.size(); k < paramSize; k++){
          entity = excelParams.get(k);
          if (entity.getList() != null) {
          cellNum += entity.getList().size();
          }else if (entity.isNeedMerge()) {
          sheet.addMergedRegion(new CellRangeAddress(index, index + maxHeight-1, cellNum,
          cellNum));
          cellNum++;
          }
          }
          return maxHeight;
          }
          /**
          * 創建List之后的各個Cells
          */
          private static void createListCells(Drawing patriarch, int index, int cellNum, Object obj,
          List<ExcelExportEntity> excelParams, Sheet sheet,
          HSSFWorkbook workbook) throws Exception {
          ExcelExportEntity entity;
          Row row;
          if(sheet.getRow(index)==null){
          row = sheet.createRow(index);
          row.setHeight((short) 350);
          }else{
          row = sheet.getRow(index);
          }
                  //避免創建的style太多,把style的創建提到循環外面
                  HSSFCellStyle style = workbook.createCellStyle();
          for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
          entity = excelParams.get(k);
          Object value = entity.getGetMethods() != null ? getFieldBySomeMethod(
          entity.getGetMethods(), obj) : entity.getGetMethod()
          .invoke(obj, new Object[] {});
          if (entity.getType() == 1) {
          createStringCell(row, cellNum++,
          value == null ? "" : value.toString(),
          row.getRowNum() % 2 == 0 ? getTwoStyle(workbook,entity,style)
          : getOneStyle(workbook,entity,style),entity);
          }else {
          createImageCell(patriarch,entity, row, cellNum++, value == null ? ""
          : value.toString());
          }
          }
          }
          /**
          * 多個反射獲取值
          * @param list
          * @param t
          * @return
          * @throws Exception
          */
          private static Object getFieldBySomeMethod(List<Method> list, Object t)
          throws Exception {
          for (Method m : list) {
          t = m.invoke(t, new Object[] {});
          }
          return t;
          }
          private static void setCellWith(List<ExcelExportEntity> excelParams,
          Sheet sheet) {
          int index = 0;
          for (int i = 0; i < excelParams.size(); i++) {
          if (excelParams.get(i).getList() != null) {
          List<ExcelExportEntity> list = excelParams.get(i).getList();
          for (int j = 0; j < list.size(); j++) {
          sheet.setColumnWidth(index, 256 * list.get(j).getWidth());
          index++;
          }
          } else {
          sheet.setColumnWidth(index, 256 * excelParams.get(i).getWidth());
          index++;
          }
          }
          }
          /**
          * 創建表頭
          * @param entity 
          * @param index
          */
          private static void createTitleRow(ExcelTitle title,Sheet sheet, HSSFWorkbook workbook,HSSFCellStyle titleStyleSet, 
          int index, List<ExcelExportEntity> excelParams) {
          Row row = sheet.createRow(index);
          Row row1 = sheet.createRow(index + 1);
          row.setHeight((short) 450);
          int cellIndex = 0;
          CellStyle titleStyle = getTitleStyle(workbook,titleStyleSet,title);
          for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) {
          ExcelExportEntity entity = excelParams.get(i);
          createStringCell(row, cellIndex, entity.getName(), titleStyle,entity);
          if (entity.getList() != null) {
          List<ExcelExportEntity> sTitel = entity.getList();
          sheet.addMergedRegion(new CellRangeAddress(index, index,cellIndex,cellIndex
          + sTitel.size() - 1));
          for (int j = 0, size = sTitel.size(); j < size; j++) {
          createStringCell(row1, cellIndex, sTitel.get(j).getName(),
          titleStyle,entity);
          cellIndex++;
          }
          } else {
          sheet.addMergedRegion(new CellRangeAddress(index, index + 1, cellIndex,
          cellIndex));
          cellIndex++;
          }
          }
          }
          /**
          * 創建文本類型的Cell
          * @param row
          * @param index
          * @param text
          * @param style
          * @param entity 
          */
          private static void createStringCell(Row row, int index, String text,
          CellStyle style, ExcelExportEntity entity) {
          Cell cell = row.createCell(index);
          RichTextString Rtext = new HSSFRichTextString(text);
          cell.setCellValue(Rtext);
          cell.setCellStyle(style);
          }
          /**
          * 圖片類型的Cell
          * @param patriarch 
          * @param entity
          * @param row
          * @param i
          * @param string
          */
          private static void createImageCell(Drawing patriarch, ExcelExportEntity entity, Row row,
          int i, String string) {
          row.setHeight((short) (50*entity.getHeight()));
          row.createCell(i);
          HSSFClientAnchor anchor = new HSSFClientAnchor(
                   0,0,0,0, (short) i, row.getRowNum(),
                   (short) (i+1),row.getRowNum()+1);
          ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
          BufferedImage bufferImg;
          try {
          //FIXME 這里需要修改成統一的圖片絕對路徑的存放地址
          String path = ExcelExportUtil.class.getClassLoader().getResource("") +string;
          path = path.replace("WEB-INF/classes/","");
          path = path.replace("file:/","");
          bufferImg = ImageIO.read(
          new File(path));
          ImageIO.write(bufferImg,string.substring(string.indexOf(".")+1,string.length()),byteArrayOut);
          } catch (IOException e) {
          e.printStackTrace();
          }
          patriarch.createPicture(anchor,
          row.getSheet().getWorkbook().addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
          }
          /**
          * 創建 表頭
          * @param title
          * @param sheet
          * @param workbook
          * @param feildWidth
          */
          private static int createHeaderRow(ExcelTitle entity, Sheet sheet,
          HSSFWorkbook workbook, int feildWidth) {
          Row row = sheet.createRow(0);
          row.setHeight((short) 900);
          createStringCell(row, 0, entity.getTitle(), getHeaderStyle(workbook,entity),null);
          sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, feildWidth));
          if(entity.getSecondTitle()!=null){
          row = sheet.createRow(1);
          HSSFCellStyle style = workbook.createCellStyle();
          style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
          createStringCell(row, 0, entity.getSecondTitle(), style,null);
          sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, feildWidth));
          return 2;
          }
          return 1;
          }
          /**
          * 獲取導出報表的字段總長度
          * @param exportFieldTitle
          * @param secondTitle
          * @return
          */
          private static int getFieldWidth(List<ExcelExportEntity> excelParams) {
          int length = -1;// 從0開始計算單元格的
          for (ExcelExportEntity entity : excelParams) {
          length += entity.getList() != null ? entity.getList().size() : 1;
          }
          return length;
          }
          /**
          * 獲取需要導出的全部字段
          * @param targetId
          *            目標ID
          * @param filed
          * @throws Exception
          */
          private static void getAllExcelField(String targetId, Field[] fields,
          List<ExcelExportEntity> excelParams, Class<?> pojoClass,
          List<Method> getMethods) throws Exception {
          // 遍歷整個filed
          ExcelExportEntity excelEntity;
          for (int i = 0; i < fields.length; i++) {
          Field field = fields[i];
          // 先判斷是不是collection,在判斷是不是java自帶對象,之后就是我們自己的對象了
          if(isNotUserExcelUserThis(field, targetId)){continue;}
          if (isCollection(field.getType())) {
          ExcelCollection excel = field
          .getAnnotation(ExcelCollection.class);
          ParameterizedType pt = (ParameterizedType) field
          .getGenericType();
          Class<?> clz = (Class<?>) pt.getActualTypeArguments()[0];
          List<ExcelExportEntity> list = new ArrayList<ExcelExportEntity>();
          getExcelFieldList(targetId, getClassFields(clz), clz,
          list, null);
          excelEntity = new ExcelExportEntity();
          excelEntity.setName(getExcelName(excel.exportName(),
          targetId));
          excelEntity.setOrderNum(getCellOrder(excel.orderNum(), targetId));
          excelEntity.setGetMethod(getMethod(field.getName(),
          pojoClass));
          excelEntity.setList(list);
          excelParams.add(excelEntity);
          } else if (isJavaClass(field)) {
          Excel excel = field.getAnnotation(Excel.class);
          excelEntity = new ExcelExportEntity();
          excelEntity.setType(excel.exportType());
          getExcelField(targetId, field, excelEntity, excel,
          pojoClass);
          if (getMethods != null) {
          List<Method> newMethods = new ArrayList<Method>();
          newMethods.addAll(getMethods);
          newMethods.add(excelEntity.getGetMethod());
          excelEntity.setGetMethods(newMethods);
          }
          excelParams.add(excelEntity);
          } else {
          //TODO 處理我們自己的數據對象
          Excel excel = field.getAnnotation(Excel.class);
          excelEntity = new ExcelExportEntity();
          excelEntity.setType(excel.exportType());
          getExcelField(targetId, field, excelEntity, excel,
          pojoClass);
          if (getMethods != null) {
          List<Method> newMethods = new ArrayList<Method>();
          newMethods.addAll(getMethods);
          newMethods.add(excelEntity.getGetMethod());
          excelEntity.setGetMethods(newMethods);
          }
          excelParams.add(excelEntity);
          //TODO ==========注釋掉============2013-10-21
          // List<Method> newMethods = new ArrayList<Method>();
          // if (getMethods != null) {
          // newMethods.addAll(getMethods);
          // }
          // newMethods.add(getMethod(field.getName(), pojoClass));
          // getAllExcelField(targetId, getClassFields(field.getType()), excelParams, field.getType(),
          // newMethods);
          //=========注釋掉=============End
          }
          }
          }
          /**
          * 判斷在這個單元格顯示的名稱
          * @param exportName
          * @param targetId
          * @return
          */
          private static String getExcelName(String exportName, String targetId) {
          if (exportName.indexOf(",") < 0) {
          return exportName;
          }
          String[] arr = exportName.split(",");
          for (String str : arr) {
          if (str.indexOf(targetId) != -1) {
          return str.split("_")[0];
          }
          }
          return null;
          }
          /**
          * 獲取這個字段的順序
          * @param orderNum
          * @param targetId
          * @return
          */
          private static int getCellOrder(String orderNum, String targetId) {
          if(isInteger(orderNum)||targetId == null){
          return Integer.valueOf(orderNum);
          }
          String[] arr = orderNum.split(",");
          for (String str : arr) {
          if (str.indexOf(targetId) != -1) {
          return Integer.valueOf(str.split("_")[0]);
          }
          }
          return 0;
          }
          /**
               * 判斷字符串是否是整數
               */
              public static boolean isInteger(String value) {
                  try {
                      Integer.parseInt(value);
                      return true;
                  } catch (NumberFormatException e) {
                      return false;
                  }
              }
          /**
          * 判斷是不是使用
          * @param exportName
          * @param targetId
          * @return
          */
          private static boolean isUseInThis(String exportName, String targetId) {
          return targetId == null || exportName.equals("")
          || exportName.indexOf(",") < 0
          || exportName.indexOf(targetId) != -1;
          }
          private static boolean isNotUserExcelUserThis(Field field, String targetId) {
          boolean boo = true;
          if(boo&&field.getAnnotation(ExcelCollection.class)!=null
          &&isUseInThis(field.getAnnotation(ExcelCollection.class).exportName(),targetId)){
          boo = false;
          }else if(boo&&field.getAnnotation(Excel.class)!=null
          &&isUseInThis(field.getAnnotation(Excel.class).exportName(),targetId)){
          boo = false;
          }else if(boo&&field.getAnnotation(ExcelEntity.class)!=null
          &&isUseInThis(field.getAnnotation(ExcelEntity.class).exportName(),targetId)){
          boo = false;
          }
          return boo;
          }
          private static void getExcelFieldList(String targetId, Field[] fields,
          Class<?> pojoClass, List<ExcelExportEntity> list,
          List<Method> getMethods) throws Exception {
          ExcelExportEntity excelEntity;
          for (int i = 0; i < fields.length; i++) {
          Field field = fields[i];
          if(isNotUserExcelUserThis(field, targetId)){continue;}
          if (isJavaClass(field)) {
          Excel excel = field.getAnnotation(Excel.class);
          excelEntity = new ExcelExportEntity();
          getExcelField(targetId, field, excelEntity, excel,
          pojoClass);
          excelEntity.setType(excel.exportType());
          if (getMethods != null) {
          List<Method> newMethods = new ArrayList<Method>();
          newMethods.addAll(getMethods);
          newMethods.add(excelEntity.getGetMethod());
          excelEntity.setGetMethods(newMethods);
          }
          list.add(excelEntity);
          } else {
          List<Method> newMethods = new ArrayList<Method>();
          if (getMethods != null) {
          newMethods.addAll(getMethods);
          }
          newMethods.add(getMethod(field.getName(), pojoClass));
          getExcelFieldList(targetId, getClassFields(field.getType()), field.getType(), list,
          newMethods);
          }
          }
          }
          public static void getExcelField(String targetId, Field field,
          ExcelExportEntity excelEntity, Excel excel, Class<?> pojoClass)
          throws Exception {
          excelEntity.setName(getExcelName(excel.exportName(), targetId));
          excelEntity.setWidth(excel.exportFieldWidth());
          excelEntity.setHeight(excel.exportFieldHeight());
          excelEntity.setNeedMerge(excel.needMerge());
          excelEntity.setOrderNum(getCellOrder(excel.orderNum(),targetId));
          excelEntity.setWrap(excel.isWrap());
          String fieldname = field.getName();
          excelEntity.setGetMethod(getMethod(fieldname, pojoClass));
          if (excel.exportConvertSign() == 1) {
          StringBuffer getConvertMethodName = new StringBuffer("convertGet");//TODO 自定義轉換方法
          getConvertMethodName
          .append(fieldname.substring(0, 1).toUpperCase());
          getConvertMethodName.append(fieldname.substring(1));
          Method getConvertMethod = pojoClass.getMethod(
          getConvertMethodName.toString(), new Class[] {});
          excelEntity.setGetMethod(getConvertMethod);
          }
          }
          private static boolean isJavaClass(Field field) {
          Class<?> fieldType = field.getType();
          boolean isBaseClass = false;
          if(fieldType.isArray()){
          isBaseClass = false;
          }else if (fieldType.isPrimitive()||fieldType.getPackage()==null
          || fieldType.getPackage().getName().equals("java.lang")
          || fieldType.getPackage().getName().equals("java.math")
          || fieldType.getPackage().getName().equals("java.util")) {
          isBaseClass =  true;
          }
          return isBaseClass;
          }
          /**
          * 字段說明的Style
          * @param workbook
          * @return
          */
          public static HSSFCellStyle getTitleStyle(HSSFWorkbook workbook,HSSFCellStyle titleStyle, ExcelTitle entity) {
          titleStyle.setFillForegroundColor(entity.getHeaderColor()); // 填充的背景顏色
          titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
          titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
          titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 填充圖案
          titleStyle.setWrapText(true);
          return titleStyle;
          }
          /**
          * 表明的Style
          * @param workbook
          * @return
          */
          public static HSSFCellStyle getHeaderStyle(HSSFWorkbook workbook, ExcelTitle entity) {
          HSSFCellStyle titleStyle = workbook.createCellStyle();
          Font font = workbook.createFont();
          font.setFontHeightInPoints((short) 24);
          titleStyle.setFont(font);
          titleStyle.setFillForegroundColor(entity.getColor()); 
          titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
          titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
          return titleStyle;
          }
          public static HSSFCellStyle getTwoStyle(HSSFWorkbook workbook, ExcelExportEntity entity,HSSFCellStyle style) {
          style.setBorderLeft((short) 1); // 左邊框
          style.setBorderRight((short) 1); // 右邊框
          style.setBorderBottom((short) 1);
          style.setBorderTop((short) 1);
          style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index); // 填充的背景顏色
          style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 填充圖案
          style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
          style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
          if(entity.isWrap()){style.setWrapText(true);}
          return style;
          }
          public static HSSFCellStyle getOneStyle(HSSFWorkbook workbook, ExcelExportEntity entity,HSSFCellStyle style) {
          style.setBorderLeft((short) 1); // 左邊框
          style.setBorderRight((short) 1); // 右邊框
          style.setBorderBottom((short) 1);
          style.setBorderTop((short) 1);
          style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
          style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
          if(entity.isWrap()){style.setWrapText(true);}
          return style;
          }
          /**
          * 判斷是不是集合的實現類
          * @param clazz
          * @return
          */
          private static boolean isCollection(Class<?> clazz) {
          String colleciton = "java.util.Collection";
          Class<?>[] faces = clazz.getInterfaces();
          for (Class<?> face : faces) {
          if(face.getName().equals(colleciton)){
          return true;
          }else{
          if(face.getSuperclass()!= Object.class&&face.getSuperclass()!=null){
          return isCollection(face.getSuperclass());
          }
          }
          }
          if(clazz.getSuperclass()!= Object.class&&clazz.getSuperclass()!=null){
          return isCollection(clazz.getSuperclass());
          }
          return false;
          }
          private static Method getMethod(String name, Class<?> pojoClass)
          throws Exception {
          StringBuffer getMethodName = new StringBuffer("get");
          getMethodName.append(name.substring(0, 1).toUpperCase());
          getMethodName.append(name.substring(1));
          return pojoClass.getMethod(getMethodName.toString(), new Class[] {});
          }
          /**
          * 獲取class的 包括父類的
          * @param type
          * @return
          */
          private static Field[] getClassFields(Class<?> clazz) {
          List<Field> list = new ArrayList<Field>();
          Field[] fields;
          do{
          fields = clazz.getDeclaredFields();
          for(int i = 0;i<fields.length;i++){
          list.add(fields[i]);
          }
          clazz = clazz.getSuperclass();
          }while(clazz!= Object.class&&clazz!=null);
          return list.toArray(fields);
          }
          }
          posted on 2014-05-29 11:14 管先飛 閱讀(7877) 評論(0)  編輯  收藏 所屬分類: Java技術
          主站蜘蛛池模板: 且末县| 和政县| 英超| 云安县| 福建省| 即墨市| 共和县| 东台市| 颍上县| 荥阳市| 潼南县| 宁陕县| 府谷县| 汉中市| 岑巩县| 昭苏县| 图木舒克市| 衡山县| 张家港市| 康平县| 墨竹工卡县| 略阳县| 永新县| 鄂托克前旗| 托克逊县| 平乐县| 巴彦淖尔市| 木里| 肥西县| 宁晋县| 莒南县| 通渭县| 宝应县| 宜阳县| 吉首市| 赞皇县| 寻乌县| 安远县| 邹城市| 岳池县| 合阳县|