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
华梦行 阅读(230)
评论(0) 编辑 收藏