隨筆-295  評論-26  文章-1  trackbacks-0

          ? Session session = HibernateUtil.getSession();
          ??????????? Criteria crit = session.createCriteria(entityClass);
          ??????????? crit.add(Restrictions.eq("orgTypeId", orgId));?
          ??????????? crit.add(Restrictions.eq("StructID", strTypeid));
          ??????????? List entities = crit.list();
          ??????????? populate(sheet, entities);

          ? Hibernate 條件查詢

          /*
          ?* ExportManager.java
          ?*
          ?* Created on 2006年7月30日, 下午2:40
          ?*
          ?* To change this template, choose Tools | Template Manager
          ?* and open the template in the editor.
          ?*/

          package path.system.manager;

          import java.io.*;
          import java.util.List;
          import java.util.Date;
          import java.sql.Timestamp;
          import java.lang.reflect.*;
          import java.math.BigDecimal;

          import jxl.*;
          import jxl.write.*;
          import org.hibernate.Session;
          import org.hibernate.Query;
          import org.hibernate.Criteria;
          import org.hibernate.criterion.Restrictions;

          import path.system.manager.HibernateUtil;
          import path.util.DoNumber;

          /**
          ?*
          ?* @author zhaoming
          ?*/
          public class ExportManager {
          ???
          ??? private Class entityClass;
          ??? private Field[] fields;
          ??? private Class[] fieldTypes;
          ???
          ??? private static WritableCellFormat integerFormat = new WritableCellFormat(NumberFormats.INTEGER);
          ??? private static WritableCellFormat floatFormat = new WritableCellFormat(new NumberFormat("#.####"));
          ??? private static WritableCellFormat dateFormat = new WritableCellFormat(new DateFormat("yyyy-MM-dd"));
          ???
          ??? public ExportManager() {
          ??? }
          ???
          ??? /**
          ???? * 輸出excel
          ???? * @param is 原始excel模版輸入流
          ???? * @param os 目的輸出流,這里是ServletOutputStream
          ???? */
          ??? public void exportExcel(InputStream is, OutputStream os, String orgId) {
          ??????? WritableWorkbook wb = null;
          ??????? try {
          ??????????? wb = Workbook.createWorkbook(os, Workbook.getWorkbook(is));
          ??????????? WritableSheet sheet = wb.getSheet(0);
          ??????????? init(sheet);
          ??????????? Session session = HibernateUtil.getSession();
          ??????????? Criteria crit = session.createCriteria(entityClass);
          ??????????? crit.add(Restrictions.eq("orgTypeId", orgId));
          ??????????? List entities = crit.list();
          ??????????? populate(sheet, entities);
          ??????????? wb.write();
          ??????? } catch (Exception e) {
          ??????????? e.printStackTrace();
          ??????? } finally {
          ??????????? try { wb.close(); } catch (Exception e) {}
          ??????? }
          ??? }
          ??? public void exportExcelCd(InputStream is, OutputStream os, String orgId, String strTypeid) {
          ??????? WritableWorkbook wb = null;
          ??????? try {
          ??????????? wb = Workbook.createWorkbook(os, Workbook.getWorkbook(is));
          ??????????? WritableSheet sheet = wb.getSheet(0);
          ??????????? init(sheet);
          ??????????? Session session = HibernateUtil.getSession();
          ??????????? Criteria crit = session.createCriteria(entityClass);
          ??????????? crit.add(Restrictions.eq("orgTypeId", orgId));?
          ??????????? crit.add(Restrictions.eq("StructID", strTypeid));
          ??????????? List entities = crit.list();
          ??????????? populate(sheet, entities);
          ??????????? wb.write();
          ??????? } catch (Exception e) {
          ??????????? e.printStackTrace();
          ??????? } finally {
          ??????????? try { wb.close(); } catch (Exception e) {}
          ??????? }
          ??? }
          ???
          ??? private void init(WritableSheet sheet) throws Exception {
          ??????? entityClass = Class.forName(sheet.getCell(0, 0).getContents().trim());
          ??????? Cell[] fieldNames = sheet.getRow(1);
          ??????? int len = fieldNames.length;
          ??????? fields = new Field[len];
          ??????? fieldTypes = new Class[len];
          ??????? for (int i = 0; i < len; i++) {
          ??????????? fields[i] = entityClass.getDeclaredField(fieldNames[i].getContents().trim());
          ??????????? fieldTypes[i] = fields[i].getType();
          ??????? }
          ??? }
          ???
          ??? private void populate(WritableSheet sheet, List entities) throws Exception {
          ??????? for (int i = 0; i < entities.size(); i++) {
          ??????????? Object entity = entities.get(i);
          ??????????? for (int j = 0; j < fields.length; j++) {
          ??????????????? fields[j].setAccessible(true);
          ??????????????? WritableCell cell = getCell(i + 3, j, fields[j].get(entity));
          ??????????????? sheet.addCell(cell);
          ??????????? }
          ??????? }
          ??? }
          ???
          ??? private WritableCell getCell(int row, int col, Object value) {
          ??????? if (fieldTypes[col] == String.class)
          ??????????? return new Label(col, row, (String) value);
          ??????? if (fieldTypes[col] == long.class || fieldTypes[col] == Long.class)
          ??????????? return new jxl.write.Number(col, row, value == null ? 0 : ((Long) value).longValue(), integerFormat);
          ??????? if (fieldTypes[col] == int.class || fieldTypes[col] == Integer.class)
          ??????????? return new jxl.write.Number(col, row, value == null ? 0 : ((Integer) value).intValue(), integerFormat);
          ??????? if (fieldTypes[col] == double.class || fieldTypes[col] == Double.class)
          ??????????? return new jxl.write.Number(col, row, value == null ? 0 : ((Double) value).doubleValue(), floatFormat);
          ??????? if (fieldTypes[col] == float.class || fieldTypes[col] == Float.class)
          ??????????? return new jxl.write.Number(col, row, value == null ? 0 : ((Float) value).floatValue(), floatFormat);
          ??????? if (fieldTypes[col] == BigDecimal.class)
          ??????????? return new jxl.write.Number(col, row, value == null ? 0 : ((BigDecimal) value).doubleValue(), floatFormat);
          ??????? if (fieldTypes[col] == Timestamp.class || fieldTypes[col] == Date.class)
          ??????????? return new jxl.write.DateTime(col, row, value == null ? new Date() : (Date) value, dateFormat);
          ??????? return null;
          ??? }
          ???
          }



          大盤預測 國富論
          posted on 2007-08-10 14:18 華夢行 閱讀(238) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 华阴市| 田林县| 县级市| 德钦县| 溆浦县| 云和县| 宁津县| 神池县| 翁牛特旗| 石阡县| 汝州市| 茂名市| 长沙市| 孝昌县| 樟树市| 嘉祥县| 修水县| 永春县| 镶黄旗| 宜君县| 台前县| 上栗县| SHOW| 阆中市| 新巴尔虎左旗| 虹口区| 柳州市| 商都县| 遵义县| 吉林省| 清涧县| 阜新市| 章丘市| 晴隆县| 平泉县| 敖汉旗| 宁波市| 茌平县| 金华市| 武川县| 榆社县|