锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
<property name="hibernate.query.factory_class">
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
</property>
]]>
絎竴縐嶆柟寮忥細(xì)QBC鏌ヨ
String name = "", info = "";
if (sub != null && sub.getSubname() != null) {
name = sub.getSubname();
}
if (sub != null && sub.getSubinfo() != null) {
info = sub.getSubinfo();
}
Criteria cr = session.createCriteria(Subject.class);
cr.add(Expression.like("subname","%"+name+"%"));
cr.add(Expression.like("subinfo","%"+info+"%"));
絎簩縐嶆柟寮忥細(xì)HQL鏌ヨ璇彞
String hql = "from Subject as s where s.subname like :name and s.subinfo like :info";
// 璋冪敤session鐨勮幏寰楁暟鎹垪琛ㄦ柟娉曪紝浼犻扝QL鏌ヨ璇彞
String subnamevalue="瑕佹煡璇㈢殑涓枃鍊?;
String sql="";
sql = "from Pfapp where name like :subname ";
Query query = session.createQuery(sql);
query.setString("subname","%"+subnamevalue+"%");
System.out.println("*********"+sql);
娉細(xì)Subject涓烘暟鎹簱琛╯ubject鏄犲皠鐨勭被錛涘畠鏈夌浉搴旂殑灞炴ubname銆乻ubinfo鍙?qiáng)鐩稿簲鐨刧et鍜宻et鏂規(guī)硶錛泂ub涓篠ubject鐨勪竴涓疄渚嬪寲瀵硅薄銆?br />
鍙﹀璇存槑鐨勬槸錛氬鏋滀綘涓嶅啓瀵硅薄鍚嶇О錛岀洿鎺ュ啓瀛楁錛岄偅涔坔ibernate灝辨寜鐓ф暟鎹簱瀛楁榪涜鏌ユ壘
濡傛灉浣犲啓 (瀵硅薄鍚?瀵硅薄灞炴э級(jí)錛岄偅涔坔ibernate灝辨寜鐓rmapping涓殑鏄犲皠瀛楁榪涜鏌ユ壘
榪欐槸hibernate鐨勭伒媧諱箣澶勶紒錛?
]]>
<?xml version="1.0" encoding="UTF-8"?>
<project name="gpdi" basedir="." default="deploy">
<!-- Properties and Paths -->
<property name="dir.java.src" value="src" />
<property name="dir.java.build" value="defaultroot/WEB-INF/classes" />
<property name="dir.lib" value="lib" />
<property name="dir.deploy" value="deploy" />
<property name="file.manifest" value="manifest.txt" />
<property name="file.jar" value="gpdi.jar" />
<!-- Just to fool the test -->
<property name="gpdi.root" value="." />
<!-- 鎺掗櫎java婧愭枃浠剁殑妯″紡闆?-->
<patternset id="no.java">
<exclude name="**/*.java" />
</patternset>
<path id="project.class.path">
<pathelement location="${dir.java.build}"/>
<fileset dir="${dir.lib}" id="lib.path">
<include name="**/*.jar"/>
</fileset>
</path>
<!-- 鍒濆鍖?寤虹珛鐩綍,澶嶅埗鏂囦歡 -->
<target name="init" depends="clean" description="鍒濆鍖?寤虹珛鐩綍,澶嶅埗鏂囦歡">
<mkdir dir="${dir.java.build}" />
<mkdir dir="${dir.dist}" />
<copy todir="${dir.java.build}">
<fileset dir="${dir.java.src}">
<patternset refid="no.java" />
</fileset>
</copy>
</target>
<!-- 娓呯悊,鍒犻櫎涓存椂鐩綍 -->
<target name="clean" description="娓呯悊,鍒犻櫎涓存椂鐩綍">
<delete dir="${dir.java.build}" />
<delete dir="${dir.dist}" />
<echo level="info">娓呯悊瀹屾瘯</echo>
</target>
<!-- 緙栬瘧婧愭枃浠?->
<target name="compile"
depends="init"
description="緙栬瘧婧愭枃浠?>
<javac srcdir="${dir.java.src}"
destdir="${dir.java.build}"
deprecation="off"
debug="off"
encoding="gbk"
source="1.5"
target="1.5">
<classpath refid="project.class.path"/>
</javac>
</target>
<!-- 鍒涘緩娓呭崟鏂囦歡 -->
<target name="create.manifest" description="鍒涘緩娓呭崟鏂囦歡">
<manifest file="${dir.java.src}/${file.manifest}">
<attribute name="Built-By" value="${user.name}" />
<attribute name="Main-Class" value="com.mfsoft.net.task.TaskJob" />
<attribute name="Class-Path" value="lib/log4j-1.2.9.jar lib/classes12.jar lib/joone-engine.jar" />
</manifest>
</target>
<!-- 鎵撳寘綾繪枃浠?-->
<target name="deploy" depends="create.manifest" description="鎵撳寘綾繪枃浠?>
<mkdir dir="${dir.deploy}" />
<mkdir dir="${dir.deploy}/log" />
<jar jarfile="${dir.deploy}/${file.jar}" manifest="${dir.java.src}/${file.manifest}">
<fileset dir="${dir.java.build}"
includes="db.properties,config.properties,log4j.properties,com/mfsoft/net/task/*.class,com/mfsoft/credit/modelmang/operation/*.class,
com/mfsoft/net/datatranslate/*.class,com/mfsoft/net/pre/*.class,com/mfsoft/icity/platform/database/*.class,com/mfsoft/icity/platform/datashare/*.class,
com/mfsoft/icity/platform/publicclass/*.class,com/mfsoft/credit/viable/*.class" />
</jar>
<!--澶嶅埗鐢ㄥ埌鐨勫簱浠ュ強(qiáng)鍏朵粬鏂囦歡 -->
<mkdir dir="${dir.deploy}/lib"/>
<copy todir="${dir.deploy}/lib">
<fileset dir="defaultroot/WEB-INF/lib" includes="classes12.jar,joone-engine.jar,log4j-1.2.9.jar"/>
</copy>
<mkdir dir="${dir.deploy}/model"/>
<copy todir="${dir.deploy}/model">
<fileset dir="defaultroot/WEB-INF/classes" includes="*.snet"/>
</copy>
</target>
</project>
public ActionForward userSave(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;
return mapping.findForward("userSave");
}
String path = mapping.findForward("delete").getPath();
//TODO: 璇誨彇鏈珹ction鎵鏈夌殑璇鋒眰鍙傛暟錛屽皢path閲嶆柊鏋勯狅紝鍔犱笂璇鋒眰鍙傛暟
ActionForward forward= new ActionForward(path+"&pageId=1");
forward.setRedirect(true);
//浼犲弬鏁板悗榪斿洖
return forward;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
鍦ㄥ叿浣撶殑欏圭洰搴旂敤涓?鎴戜滑寰堝皯浼?xì)鐢ㄥ堫C竴瀵逛竴鐨勭駭鑱旀搷浣?涓斿緢澶氭椂鍊欎竴瀵逛竴鐨勬搷浣滃彲浠ラ氳繃澶氬涓瀹炵幇,浣嗘槸涓瀵逛竴鏈夎嚜宸辯殑濂藉,灝辨槸鍦ㄤ換浣曟椂鍊欓兘鑳戒繚璇佷富澶栭敭鏁版嵁鐨勪竴鑷存?浣嗘槸澶氬涓鐨勬儏鍐典笅鏈繀鑳藉仛鍒?
鎴戝湪欏圭洰涓叿浣撶敤鍒頒簡(jiǎn)涓瀵逛竴鐨勭駭鑱旀搷浣?浠庣綉涓婃嬁浜?涓涓緥瀛愭潵鍋?浣嗘槸娌℃湁鎴愬姛,浜庢槸鍙ソ鑷繁鐮旂┒,鐜板湪鎶婁慨鏀瑰悗鐨勪唬鐮佸笘鍑烘潵.
鏈緥瀛愪嬌鐢?font size="3">hibernate3.jar+oracle鏁版嵁搴?br />
搴撹〃:
CARD_TEST(CARDID[NUMBER(18)],CARDNUM[VARCHAR2(20)])
USER_TEST(USERID[NUMBER(18)],USERNAME[VARCHAR2(20)])
TestMain.java
package com.gpdi.test;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class TestMain {
/**
* 瀛樺偍涓涓敤鎴?br />
* @param user
*/
public void save(User user){
user.setUsername("TEST");
Card card=new Card();
//Card card1=new Card();
card.setCardnum("HGJUYT");
//card1.setCardnum("fghy55");
user.setCard(card);
//user.setCard(card1);
Session session=HibernateSessionFactory.currentSession();
card.setUser(user);
//card1.setUser(user);
Transaction tr=session.beginTransaction();
session.save(user);
//session.save(card);
tr.commit();
session.close();
}
public static void main(String[] args) throws Exception {
TestMain test=new TestMain();
test.save(new User());
}
}
Card.java
package com.gpdi.test;
/**
* Card generated by MyEclipse - Hibernate Tools
*/
public class Card implements java.io.Serializable {
// Fields
private Integer cardid;
private String cardnum;
private User user;
// Constructors
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
/** default constructor */
public Card() {
}
/** full constructor */
public Card(String cardnum) {
this.cardnum = cardnum;
}
// Property accessors
public Integer getCardid() {
return this.cardid;
}
public void setCardid(Integer cardid) {
this.cardid = cardid;
}
public String getCardnum() {
return this.cardnum;
}
public void setCardnum(String cardnum) {
this.cardnum = cardnum;
}
}
User.java
package com.gpdi.test;
import java.util.HashSet;
import java.util.Set;
/**
* User generated by MyEclipse - Hibernate Tools
*/
public class User implements java.io.Serializable {
// Fields
private Integer userid;
private String username;
private Card card;
//private Set card = new HashSet();
// Constructors
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
/** default constructor */
public User() {
}
/** full constructor */
public User(String username) {
this.username = username;
}
// Property accessors
public Integer getUserid() {
return this.userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
}
HibernateSessionFactory.java
package com.gpdi.test;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html}.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* Default constructor.
*/
private HibernateSessionFactory() {
}
}
Card.hbm.xml
package com.gpdi.test;
import org.hibernate.Session;
import com.gpdi.orm.object.Address;
import com.gpdi.orm.object.People;
public class Testp {
/**
* @param args
*/
public static void main(String[] args)
{
People people = new People();
people.setName("linda");
Address address = new Address();
address.setAddressName("yunnan");
address.setCodeNumber("564123");
address.setPeople(people);
people.getAddresses().add(address);
Session session = HibernateSessionFactory.currentSession();
session.beginTransaction();
session.save(people);
session.getTransaction().commit();
}
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.gpdi.orm.object.People" table="People">
<id name="id" column="peopleId" unsaved-value="0" >
<generator class="increment">
</generator>
</id>
<property name="name" column="name"></property>
<set name="addresses" cascade="save-update">
<key column="peopleId" not-null="true" />
<one-to-many class="com.gpdi.orm.object.Address"/>
</set>
</class>
</hibernate-mapping>
package com.gpdi.orm.object;
import java.util.HashSet;
import java.util.Set;
public class People implements java.io.Serializable
{
private long id;
private String name;
private Set addresses = new HashSet();
public Set getAddresses() {
return addresses;
}
public void setAddresses(Set addresses) {
this.addresses = addresses;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.gpdi.softevaluate.action;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ProjectSetAction extends DispatchAction
{
/**
* <p>妯℃澘楠岃瘉</p>
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward check(ActionMapping mapping, ActionForm
form,HttpServletRequest request, HttpServletResponse response)throws
Exception
{
String rest="";
String lotId="";
String retn="";
String flag="";
CommDaoFactory mgObj = new CommDaoFactory();
try
{
rest=request.getParameter("rest");
lotId=request.getParameter("lotId");
}
catch(Exception e)
{
}
if(!rest.equals("0")||!rest.equals("")||rest!=null)
{
flag="no";
}
else
{
flag="ok";
}
PrintWriter out=response.getWriter();
//鏈嶅姟鍣ㄨ繑鍥炰俊鎭?br />
out.write(flag);
out.close();
//璇誨彇鏈珹ction鎵鏈夌殑璇鋒眰鍙傛暟錛屽皢path閲嶆柊鏋勯狅紝鍔犱笂璇鋒眰鍙傛暟
//ActionForward forward= new ActionForward("project/projectSet.do?do=add&lotId=6");
//forward.setRedirect(true);
//浼犲弬鏁板悗榪斿洖
//return forward;
return mapping.findForward(null);
}
public
ActionForward creattable(ActionMapping mapping, ActionForm
form,HttpServletRequest request, HttpServletResponse response)throws
Exception
{
//瀹炵幇鍔熻兘浠g爜
return mapping.findForward("create");
}
}
3.struts.config.xml
<action attribute="projectSetForm" name="projectSetForm"
parameter="do" path="/project/projectSet" scope="request"
type="com.gpdi.softevaluate.action.ProjectSetAction" validate="false">
<forward name="list" path="/project/viewProject.jsp" />
<forward name="add" path="/project/addProject.jsp" />
<forward name="create" path="/project/createTable.jsp" />
</action>
js浠g爜:
<script language="javascript">
var tag = false;
function doDelete()
{
if(deletecheck())
{
var theForm=document.projectSetForm;
theForm.action = "projectSet.do?do=delete&lotId=<%=request.getParameter("lotId") %>";
theForm.submit();
}
}
function deletecheck()
{
var theForm = document.projectSetForm;
var checked = false;
if(typeof(theForm.checkboxDelete.length)=="undefined"){
if(theForm.checkboxDelete.checked == true){
checked = true;
}
}else{
var len = theForm.checkboxDelete.length;
for (var i=0;i<len;i++){
if(theForm.checkboxDelete[i].checked == true){
checked = true;
break;
}
}
}
if(!checked){
alert("璇瘋嚦灝戦夋嫨涓鏉¤鍒犻櫎鐨勮褰曪紒")
return(false);
}
if(confirm("紜畾瑕佸垹闄ゅ悧錛?)){
return(true);
}
return(false);
}
function allcheck(){
var theForm = document.projectSetForm;
var len = theForm.checkboxDelete.length;
var i = 0
if (tag == false)
for (i=0;i<len;i++){
theForm.checkboxDelete[i].checked = true;
tag = true;
}
else{
for ( i=0;i<len;i++){
theForm.checkboxDelete[i].checked = false;
tag = false;
}
}
}
</script>
ScoreSetExport scoreSetExport = new ScoreSetExport();
LinkedHashMap hm = new LinkedHashMap();
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
org.hibernate.classic.Session session = sessionFactory.openSession();
Query query =session.createQuery("from ScoreViable");
List users=query.list();
session.close();
sessionFactory.close();
for (ListIterator iterator = users.listIterator(); iterator.hasNext(); )
{
ScoreViable user = (ScoreViable) iterator.next();
System.out.println(user.getBeforCount() +"\n\tAge: " + user.getEndCount());
maxscore=user.getBeforCount()+"";
minscore=user.getEndCount()+"";
}
str[0]=scoreid;
str[1]=maxscore;
str[2]=minscore;
List<ScoreRecordDetail> list = scoreSetExport.exportScoreSet(str,hm);
ExcelManagerIf eem = ExcelManagerFactory.getInstance();
/******************************************************************/
/**涓嶅幓鎺変竴琛屼粛鐒惰皟鐢╡xport,鍘繪帀涓琛岃皟鐢╡xport1,鍙拡瀵逛竴縐嶆眹鎬誨鍑?/
//eem.export(response, list,hm);
eem.export1(response, list,hm);
/******************************************************************/
return null;
}
}
3.ExcelManagerIf.java
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.gpdi.orm.object.ScoreRecordDetail;
public interface ExcelManagerIf
{
/********************************************************************************************************/
/** 2008-04-18 11:54*/
public boolean export1(HttpServletResponse response, List<ScoreRecordDetail> list,LinkedHashMap hashMap);
/********************************************************************************************************/
}
4.ExcelManagerFactory.java
public class ExcelManagerFactory
{
private static String key = "key";
private ExcelManagerFactory() {
}
private static ExcelManagerIf instance = null;
public static ExcelManagerIf getInstance(){
if(instance == null){
synchronized(key){
if(instance == null){
instance = new ExcelManagerImpl();
}
}
}
return instance;
}
}
5.ExcelManagerImpl.java
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
public class ExcelManagerImpl implements ExcelManagerIf
{
/***************************************************************************************************************/
/**2008-04-18 11:36 姹囨誨鍑哄幓鎺夋棤鐢ㄧ殑絀烘牸*/
public boolean export1(HttpServletResponse response, List<ScoreRecordDetail> list,LinkedHashMap hashMap) {
Session session = null;
try {
// 鐢熸垚鐢?shù)瀛愯〃鏍兼枃錃g
HSSFWorkbook wb = new HSSFWorkbook();
// 閫氳繃鍏堝緩绔嬭〃鏍鹼紝鍚庡懡鍚嶅彲浠ュ畾涔変嬌鐢ㄧ殑緙栫爜錛屼粠鑰屼繚璇佹眽瀛楃殑姝e父鏄劇ず
HSSFSheet sheet1 = wb.createSheet();
/********************************************/
/**娣誨姞 2008-04-15瀵煎嚭鎵撳嵃榛樿鏂瑰紡,true錛氭í鍚?nbsp; false錛氱旱鍚?/
sheet1.getPrintSetup().setLandscape(true);
/********************************************/
sheet1.setColumnWidth((short) 0, (short) 5000);
sheet1.setColumnWidth((short) 1, (short) 5000);
sheet1.setColumnWidth((short) 2, (short) 5000);
sheet1.setColumnWidth((short) 3, (short) 5000);
// wb.setSheetName(0, "寮鍙戝晢璧勬枡",HSSFWorkbook.ENCODING_UTF_16);
// HSSFRow row;
// 鍏堝畾涔変竴涓瓧浣撳璞?br />
HSSFFont font = wb.createFont();
font.setFontName("瀹嬩綋");
font.setFontHeightInPoints((short) 16);// 瀛椾綋澶у皬
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 鍔犵矖
// 瀹氫箟琛ㄥご鍗曞厓鏍兼牸寮?br />
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(font);// 鍗曞厓鏍煎瓧浣?br />
cellStyle.setBorderBottom(cellStyle.BORDER_THIN);// 鍗曞厓鏍艱竟妗?br />
cellStyle.setBorderTop(cellStyle.BORDER_THIN);
cellStyle.setBorderRight(cellStyle.BORDER_THIN);
cellStyle.setBorderLeft(cellStyle.BORDER_THIN);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 姘村鉤瀵歸綈鏂瑰紡
// 瀹氫箟鏂扮殑璋冭壊鏉夸互渚垮畾涔夎嚜宸辯殑棰滆壊
HSSFPalette palette = wb.getCustomPalette();
// 鏇挎崲涓涓粯璁よ皟鑹叉澘涓凡緇忓畾涔夌殑棰滆壊
palette.setColorAtIndex(HSSFColor.BLUE_GREY.index, (byte) 230, (byte) 230, (byte) 255);
// 浣跨敤鍒氭墠瀹氫箟鐨勯鑹蹭負(fù)濉厖鑹?br />
cellStyle.setFillForegroundColor(HSSFColor.BLUE_GREY.index);
cellStyle.setFillPattern((short) HSSFCellStyle.SOLID_FOREGROUND);
// 鍐欏叆琛ㄥご
HSSFCell c;
// String[] headerName = (String[]) aHeaderName.get(type);
int[] maxLen = new int[4];
int maxCol = list.size() + 3;
ScoreRecordDetail first = list.get(0);
Long scoreId = first.getScoreRecord().getScoreid();
session = HibernateSessionFactory.getCurrentSession();
ScoreSet scoreSet = (ScoreSet) session.createQuery("from ScoreSet where scoreid = :scoreid").setLong("scoreid",
scoreId).uniqueResult();
String titleName = scoreSet.getScoreLot().getLotName() + "-" + scoreSet.getScoreModel().getScoremodelname();
//杈撳嚭鏂囦歡鍚嶈惤瀛欏北
String outFileName = scoreSet.getScoreModel().getScoremodelname();
// 鍦ㄨ〃鍗曚腑鏂板緩0琛?br />
HSSFRow row0 = sheet1.createRow((short) 0);
row0.setHeight((short) 600);
c = row0.createCell((short) 0);
HSSFCellStyle cellStyle0 = wb.createCellStyle();
cellStyle0.setFont(font);// 鍗曞厓鏍煎瓧浣?
cellStyle0.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 姘村鉤瀵歸綈鏂瑰紡
cellStyle0.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle0.setBorderBottom(cellStyle.BORDER_THIN);// 鍗曞厓鏍艱竟妗?br />
cellStyle0.setBorderTop(cellStyle.BORDER_THIN);
cellStyle0.setBorderRight(cellStyle.BORDER_THIN);
cellStyle0.setBorderLeft(cellStyle.BORDER_THIN);
c.setCellStyle(cellStyle0);// 鍗曞厓鏍肩殑鏍峰紡
c.setEncoding(HSSFCell.ENCODING_UTF_16);// 緙栫爜
c.setCellValue(titleName);// 鍗曞厓鏍煎?br />
sheet1.addMergedRegion(new Region(0, (short) 0, 0, (short) (maxCol-1)));
// 鍦ㄨ〃鍗曚腑鏂板緩1琛?br />
/**
HSSFRow row1 = sheet1.createRow((short) 1);
row1.setHeight((short) 700);
c = row1.createCell((short) 0);
HSSFCellStyle cellStyle1 = wb.createCellStyle();
cellStyle1.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 姘村鉤瀵歸綈鏂瑰紡
cellStyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle1.setBorderBottom(cellStyle.BORDER_THIN);// 鍗曞厓鏍艱竟妗?br />
cellStyle1.setBorderTop(cellStyle.BORDER_THIN);
cellStyle1.setBorderRight(cellStyle.BORDER_THIN);
cellStyle1.setBorderLeft(cellStyle.BORDER_THIN);
c.setCellStyle(cellStyle1);// 鍗曞厓鏍肩殑鏍峰紡
c.setEncoding(HSSFCell.ENCODING_UTF_16);// 緙栫爜
c.setCellValue("璇勬爣涓撳絳懼瓧錛?);// 鍗曞厓鏍煎?/span>
c = row1.createCell((short) 1);
c.setCellStyle(cellStyle1);// 鍗曞厓鏍肩殑鏍峰紡
c = row1.createCell((short) 2);
c.setCellStyle(cellStyle1);// 鍗曞厓鏍肩殑鏍峰紡
c.setEncoding(HSSFCell.ENCODING_UTF_16);// 緙栫爜
c.setCellValue("鏃ユ湡錛?);// 鍗曞厓鏍煎?/span>
c = row1.createCell((short) 3);
c.setCellStyle(cellStyle1);// 鍗曞厓鏍肩殑鏍峰紡
c.setEncoding(HSSFCell.ENCODING_UTF_16);// 緙栫爜
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
c.setCellValue(sdf.format(new Date()));// 鍗曞厓鏍煎?br />
*/
// 鍦ㄨ〃鍗曚腑鏂板緩2琛?br />
HSSFRow row2 = sheet1.createRow((short) 1);
row2.setHeight((short) 600);
HSSFCellStyle cellStyle2 = wb.createCellStyle();
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 姘村鉤瀵歸綈鏂瑰紡
cellStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle2.setFillBackgroundColor(HSSFColor.AQUA.index);
cellStyle2.setBorderBottom(cellStyle.BORDER_THIN);// 鍗曞厓鏍艱竟妗?br />
cellStyle2.setBorderTop(cellStyle.BORDER_THIN);
cellStyle2.setBorderRight(cellStyle.BORDER_THIN);
cellStyle2.setBorderLeft(cellStyle.BORDER_THIN);
c = row2.createCell((short) 0);
c.setCellStyle(cellStyle2);// 鍗曞厓鏍肩殑鏍峰紡
c.setEncoding(HSSFCell.ENCODING_UTF_16);// 緙栫爜
c.setCellValue("涓撳鍚嶇О");// 鍗曞厓鏍煎?br />
/*****************************************************************/
sheet1.addMergedRegion(new Region(1, (short) 0, 1, (short) 1));
//sheet1.addMergedRegion(new Region(2, (short) 0, 2, (short) 1));
/*****************************************************************/
row2.setHeight((short) 400);
c = row2.createCell((short) 1);
c.setCellStyle(cellStyle2);// 鍗曞厓鏍肩殑鏍峰紡
c.setEncoding(HSSFCell.ENCODING_UTF_16);// 緙栫爜
//c.setCellValue("欏圭洰");// 鍗曞厓鏍煎?/span>
row2.setHeight((short) 400);
c = row2.createCell((short) 2);
c.setCellStyle(cellStyle2);// 鍗曞厓鏍肩殑鏍峰紡
c.setEncoding(HSSFCell.ENCODING_UTF_16);// 緙栫爜
c.setCellValue("鍒嗗?);// 鍗曞厓鏍煎?/span>
//c = row2.createCell((short) 3);
//c.setCellStyle(cellStyle2);// 鍗曞厓鏍肩殑鏍峰紡
//閲嶅琛?br />
int rows=0;
if(hashMap!=null){
int j=0;
HSSFRow[] row_repeat= null;
for (Iterator iterator = hashMap.values().iterator(); iterator.hasNext();) {
Map map= (LinkedHashMap) iterator.next();
rows =map.size();
row_repeat=new HSSFRow[map.size()];
for(int i=0;i<rows;i++){
/****************************************************/
row_repeat[i] = sheet1.createRow((short) (2+i));
//row_repeat[i] = sheet1.createRow((short) (3+i));
/****************************************************/
row_repeat[i].setHeight((short) 400);
}
break;
}
for (Iterator iterator = hashMap.values().iterator(); iterator.hasNext();) {
Map map= (LinkedHashMap) iterator.next();
int i=0;
for (Iterator iter_key = map.keySet().iterator(); iter_key.hasNext();) {
SysUser sysUser = (SysUser) iter_key.next();
if(j==0){
c = row_repeat[i].createCell((short) 0);
c.setCellStyle(cellStyle2);// 鍗曞厓鏍肩殑鏍峰紡
c.setEncoding(HSSFCell.ENCODING_UTF_16);// 緙栫爜
c.setCellValue(sysUser.getUsername());// 鍗曞厓鏍煎?br />
/******************************************************************/
sheet1.addMergedRegion(new Region(2+i, (short) 0, 2+i, (short) 1));
//sheet1.addMergedRegion(new Region(3+i, (short) 0, 3+i, (short) 1));
/******************************************************************/
c = row_repeat[i].createCell((short) 1);
c.setCellStyle(cellStyle2);// 鍗曞厓鏍肩殑鏍峰紡
c = row_repeat[i].createCell((short) 2);
c.setCellStyle(cellStyle2);// 鍗曞厓鏍肩殑鏍峰紡
c.setEncoding(HSSFCell.ENCODING_UTF_16);// 緙栫爜
c.setCellValue("40");// 鍗曞厓鏍煎?br />
}
c = row_repeat[i].createCell((short) (3+j));
c.setCellStyle(cellStyle2);// 鍗曞厓鏍肩殑鏍峰紡
c.setCellType(HSSFCell.CELL_TYPE_NUMERIC);//鏁板瓧鍨?br />
c.setEncoding(HSSFCell.ENCODING_UTF_16);// 緙栫爜
c.setCellValue(Double.parseDouble(map.get(sysUser).toString()));// 鍗曞厓鏍煎?nbsp;
i++;
}
j++;
}
}
// 鍦ㄨ〃鍗曚腑鏂板緩3琛?br />
/************************************************/
HSSFRow row3 = sheet1.createRow((short) (2+rows));
//HSSFRow row3 = sheet1.createRow((short) (3+rows));
/************************************************/
row3.setHeight((short) 400);
c = row3.createCell((short) 0);
c.setCellStyle(cellStyle2);// 鍗曞厓鏍肩殑鏍峰紡
c.setEncoding(HSSFCell.ENCODING_UTF_16);// 緙栫爜
c.setCellValue(scoreSet.getScoreModel().getScoremodelname() + " 璇勫垎");// 鍗曞厓鏍煎?br />
/******************************************************************************/
//sheet1.addMergedRegion(new Region((3+rows), (short) 0,(3+rows), (short) 1));
sheet1.addMergedRegion(new Region((2+rows), (short) 0,(2+rows), (short) 1));
/******************************************************************************/
c = row3.createCell((short) 1);
c.setCellStyle(cellStyle2);// 鍗曞厓鏍肩殑鏍峰紡
c = row3.createCell((short) 2);
c.setCellStyle(cellStyle2);// 鍗曞厓鏍肩殑鏍峰紡
c.setEncoding(HSSFCell.ENCODING_UTF_16);// 緙栫爜
c.setCellValue("40");// 鍗曞厓鏍煎?/span>
for (int i = 0; i < list.size(); i++) {
sheet1.setColumnWidth((short) (i + 3), (short) 5000);
ScoreRecordDetail o = list.get(i);
String name = "";
if (o.getDevelopid() != null) {
BasDevelop basDevelop = (BasDevelop) session.createQuery("from BasDevelop where developid = :developid")
.setLong("developid", o.getDevelopid()).uniqueResult();
name = basDevelop.getDename();
} else {
ProductType productType = (ProductType) session.createQuery("from ProductType where productid = :productid")
.setLong("productid", o.getProductid()).uniqueResult();
name = productType.getProdname();
}
/****************************************************************************/
//c = row1.createCell((short) (i + 3));
//c.setCellStyle(cellStyle1);// 鍗曞厓鏍肩殑鏍峰紡
/****************************************************************************/
c = row2.createCell((short) (i + 3));
c.setCellStyle(cellStyle2);// 鍗曞厓鏍肩殑鏍峰紡
c.setEncoding(HSSFCell.ENCODING_UTF_16);// 緙栫爜
c.setCellValue(name);// 鍗曞厓鏍煎?/font>
c = row3.createCell((short) (i + 3));
c.setCellStyle(cellStyle2);// 鍗曞厓鏍肩殑鏍峰紡
c.setEncoding(HSSFCell.ENCODING_UTF_16);// 緙栫爜
c.setCellType(HSSFCell.CELL_TYPE_NUMERIC);//鏁板瓧鍨?br />
c.setCellValue(o.getScore());// 鍗曞厓鏍煎?br />
}
// 杈撳嚭Excel鏂囦歡
errorOrExportExcel(response, wb, 2 ,outFileName);
public class ExcelManagerFactory {
private static String key = "key";
private ExcelManagerFactory() {
}
private static ExcelManagerIf instance = null;
public static ExcelManagerIf getInstance(){
if(instance == null){
synchronized(key){
if(instance == null){
instance = new ExcelManagerImpl();
}
}
}
return instance;
}
}
2.澹版槑涓涓帴鍙?/span>
package com.factory;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.gpdi.orm.object.ScoreRecordDetail;
public interface ExcelManagerIf {
public boolean export(HttpServletResponse response, List<ScoreRecordDetail> list,LinkedHashMap hashMap);
/********************************************************************************************************/
public boolean export1(HttpServletResponse response,List list,LinkedHashMap hashMap);
/********************************************************************************************************/
}
3.瀹炵幇鎺ュ彛鐨勭被
package com.factory;
import java.io.IOException;
public class ExcelManagerImpl implements ExcelManagerIf
{
public boolean export1(HttpServletResponse response, List list,LinkedHashMap hashMap)
{
//瀹炵幇鍏蜂綋鍔熻兘鐨勪唬鐮佸疄鐜?br />
}
}
4.璋冪敤
HttpServletResponse response=null;
LinkedHashMap hm = new LinkedHashMap();
List list = Object.getList();
ExcelManagerIf eem = ExcelManagerFactory.getInstance();
/******************************************************************/
eem.export(response, list,hm);
1銆佹暟鎹簱緇撴瀯
category琛細(xì){ categoryId , categoryName }
item琛細(xì){ itemId , itemName }
category_item琛細(xì){ idCategory , idItem}
2銆丆ategory.hbm.xml
3銆両tem.hbm.xml
4銆丆ategory.java鍜孖tem.java
鏄犲皠鏂囦歡涓繕鍙互涓嶇敤set鑰岀敤bag錛屽彧瑕佹妸set鍑虹幇鐨勫湴鏂規(guī)敼鎴恇ag灝辮錛岃繖鏍風(fēng)浉搴旂殑category.java鍜孖tem.java
鏂囦歡褰撲腑灝卞彲浠ョ敤List銆?/span>
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<TABLE>
<TR>
<TD></TD>
</TR>
<TR>
<TD><img src="show.jsp"></TD>
</TR>
</TABLE>
</BODY>
</HTML>