function MakeExcel(){
var i,j;
? ?try {
? ? ?var xls ? ?= new ActiveXObject ( "Excel.Application" );
? ? }
? ?catch(e) {
? ? ? ? alert( "要打印該表,您必須安裝Excel電子表格軟件,同時瀏覽器須使用“ActiveX 控件”,您的瀏覽器須允許執行控件。 請點擊【幫助】了解瀏覽器設置方法!");
? ? ? ? ? ? ?return "";
? ? }
? ?xls.visible =true; ?//設置excel為可見
? ?var xlBook = xls.Workbooks.Add;
? ?var xlsheet = xlBook.Worksheets(1);
? ?
? ? ?xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,7)).mergecells=true;
? ? ?xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,7)).value="發卡記錄";
? ? // ?xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,6)).Interior.ColorIndex=5;//設置底色為藍色
? ? ? ? ? ? ? ?// ? xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,6)).Font.ColorIndex=4;//設置字體色 ? ? ? ?
? // xlsheet.Rows(1). Interior .ColorIndex = 5 ;//設置底色為藍色 ?設置背景色 Rows(1).Font.ColorIndex=4 ?
? ?
? ?xlsheet.Rows(1).RowHeight = 25;
? ?
? ?xlsheet.Rows(1).Font.Size=14;
? ?
? ?xlsheet.Rows(1).Font.Name="黑體";
? ?
? ?xlsheet.Columns("A:D").ColumnWidth =18;
? ?
? ?xlsheet.Columns(2).NumberFormatLocal="@";
? ?xlsheet.Columns(7).NumberFormatLocal="@";
? ? //設置單元格內容自動換行 range.WrapText ?= ?true ?;
? ? //設置單元格內容水平對齊方式 range.HorizontalAlignment ?= ?Excel.XlHAlign.xlHAlignCenter;//設置單元格內容豎直堆砌方式
? ? ?//range.VerticalAlignment=Excel.XlVAlign.xlVAlignCenter
? ?//range.WrapText ?= ?true; ?xlsheet.Rows(3).WrapText=true ?自動換行
?
? ?//設置標題欄
? ? xlsheet.Cells(2,1).Value="卡號";
? ? xlsheet.Cells(2,2).Value="密碼";
? ? xlsheet.Cells(2,3).Value="計費方式";
? ? xlsheet.Cells(2,4).Value="有效天數";
? ? xlsheet.Cells(2,5).Value="金額";
? ? xlsheet.Cells(2,6).Value="所屬服務項目";
? ? ? xlsheet.Cells(2,7).Value="發卡時間";
? ? var oTable=document.all['fors:data'];
? ? var rowNum=oTable.rows.length;
? ? for(i=2;i<=rowNum;i++){
? ? for (j=1;j<=7;j++){
//html table類容寫到excel
? ? ? xlsheet.Cells(i+1,j).Value=oTable.rows(i-1).cells(j-1).innerHTML;
? ? ? ? ? ?}
? ?}
? ?
? ?// xlsheet.Range(xlsheet.Cells(i, 4), xlsheet.Cells(i-1, 6)).BorderAround , 4
? ? // for(mn=1,mn<=6;mn++) . ? ? xlsheet.Range(xlsheet.Cells(1, mn), xlsheet.Cells(i1, j)).Columns.AutoFit;
? ? ?xlsheet.Columns.AutoFit;
? ? ? ? ? ? ? ? xlsheet.Range( xlsheet.Cells(1,1),xlsheet.Cells(rowNum+1,7)).HorizontalAlignment =-4108;//居中
? ? ? ? ? ? ? ? ? xlsheet.Range( xlsheet.Cells(1,1),xlsheet.Cells(1,7)).VerticalAlignment =-4108;
? ? ? ? ? ? ? ? xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Font.Size=10;
? ? ?xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(3).Weight = 2; //設置左邊距
? ? ? xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(4).Weight = 2;//設置右邊距
? ? ? ? ? ? xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(1).Weight = 2;//設置頂邊距
? ? ? xlsheet.Range( xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(2).Weight = 2;//設置底邊距
xls.UserControl = true; ?//很重要,不能省略,不然會出問題 意思是excel交由用戶控制
? ? ? xls=null;
? ? ? xlBook=null;
? ? ? xlsheet=null;
}
response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
?response.setHeader("Pragma","no-cache"); //HTTP 1.0
?response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
我的數據庫結構是這樣的:

首先一開始我可以獲得一個頻道的channelId,我根據這個channelId得到一個首頁區塊的List,我在hibernate中配置homepagearea的加載方式,這樣就可以通過homepage的到關聯的欄目column(多對一關系),然后我還是使用hibernate的自動加載,取到column關聯的專題subject(一對多關系)。
這時候問題出來了,由于column到subject的關聯沒有帶channel信息,所以,我取到的subject實際上是一個column下所有的subject,而我期望的是要得到,一個homepagearea下根據channelId和columnId取得的subject.
解決思路:
希望通過hibernate直接建立homepage和subject的1對多關聯關系
解決方法
首先我改變原來利用工具生成的hibernate配置文件和entitybean
先列出原來的homepagearea的配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
??? "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
??? "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class
??? name="com.easou.wapsearch.channel.entity.CsHomepageArea"
??? table="CS_HOMEPAGE_AREA"
??? schema="WAPUSER"
??? lazy="true"
>
??? <id
??????? name="id"
??????? type="long"
??????? column="ID"
??????? length="22"
??? >
??? ?<generator class="assigned">
??? ??
??? ?</generator>
??? </id>
??? <property
??????? name="createdBy"
??????? type="long"
??????? column="CREATED_BY"
??????? length="22"
??? />
??? <property
??????? name="createdDate"
??????? type="timestamp"
??????? column="CREATED_DATE"
??????? length="7"
??? />
??? <property
??????? name="isMore"
??????? type="long"
??????? column="IS_MORE"
??????? length="22"
??? />
??? <property
??????? name="name"
??????? type="string"
??????? column="NAME"
??????? length="50"
??? />
??? <property
??????? name="rowCount"
??????? type="long"
??????? column="ROW_COUNT"
??????? length="22"
??? />
??? <property
??????? name="showCount"
??????? type="long"
??????? column="SHOW_COUNT"
??????? length="22"
??? />
??? <property
??????? name="theOrder"
??????? type="long"
??????? column="THE_ORDER"
??????? length="22"
??? />
??? <property
??????? name="updateBy"
??????? type="long"
??????? column="UPDATE_BY"
??????? length="22"
??? />
??? <property
??????? name="updateDate"
??????? type="timestamp"
??????? column="UPDATE_DATE"
??????? length="7"
??? />
??? <!-- Associations -->
??? <!-- bi-directional many-to-one association to CsChannel -->
??? <many-to-one
??????? name="csChannel"???????
??? >
??????? <column name="CHANNEL_ID" length="22"/>
??? </many-to-one>
??? <!-- bi-directional many-to-one association to CsColumn -->
??? <many-to-one
??????? name="csColumn"???????
??? >
??????? <column name="COLUMN_ID" length="22"/>
??? </many-to-one>
</class>
</hibernate-mapping>
為了讓homepagearea和subject形成一種一對多的關系,我增加的一個
<set name="csSubjects" inverse="true" cascade="save-update"
???lazy="false" order-by="THE_ORDER" table="CS_SUBJECT"
???outer-join="true">
???<key>
????<column name="CHANNEL_ID" index="CHANNEL_ID" />
????<column name="COLUMN_ID" index="COLUMN_ID" />
???</key>
???<one-to-many
????class="com.easou.wapsearch.channel.entity.CsSubject" />
</set>
但是由于hibernate一對多映射的一端必須是主鍵,而且我這里需要關聯的還是2列信息,所以我還必須要修改配置文件的主鍵設置
<composite-id mapped="false" unsaved-value="none">
????? <key-many-to-one name="csChannel" column="CHANNEL_ID"></key-many-to-one>
???<key-many-to-one name="csColumn" column="COLUMN_ID"></key-many-to-one>
</composite-id>
并且遮蔽掉原有的channel和column多對一關聯關系,否則會報告重復錯誤
<!-- Associations
?? bi-directional many-to-one association to CsChannel
??<many-to-one name="csChannel" lazy="true">
???<column name="CHANNEL_ID" length="22" />
??</many-to-one>
?? bi-directional many-to-one association to CsColumn
??<many-to-one name="csColumn" lazy="true">
???<column name="COLUMN_ID" length="22" />
??</many-to-one>
??-->
這樣hibernate才會在加載homepagearea時自動把subject的關聯信息也加載進來了
最后的homepagearea配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
??? "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
??? "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
?<class name="com.easou.wapsearch.channel.entity.CsHomepageArea"
??table="CS_HOMEPAGE_AREA" schema="WAPUSER" lazy="true">
??<composite-id mapped="false" unsaved-value="none">
????? <key-many-to-one name="csChannel" column="CHANNEL_ID"></key-many-to-one>
???<key-many-to-one name="csColumn" column="COLUMN_ID"></key-many-to-one>
??</composite-id>
??<property name="createdBy" type="long" column="CREATED_BY"
???length="22" />
??<property name="createdDate" type="timestamp"
???column="CREATED_DATE" length="7" />
??<property name="isMore" type="long" column="IS_MORE"
???length="22" />
??<property name="name" type="string" column="NAME" length="50" />
??<property name="rowCount" type="long" column="ROW_COUNT"
???length="22" />
??<property name="showCount" type="long" column="SHOW_COUNT"
???length="22" />
??<property name="theOrder" type="long" column="THE_ORDER"
???length="22" />
??<property name="updateBy" type="long" column="UPDATE_BY"
???length="22" />
??<property name="updateDate" type="timestamp"
???column="UPDATE_DATE" length="7" />
??<!-- Associations
?? bi-directional many-to-one association to CsChannel
??<many-to-one name="csChannel" lazy="true">
???<column name="CHANNEL_ID" length="22" />
??</many-to-one>
?? bi-directional many-to-one association to CsColumn
??<many-to-one name="csColumn" lazy="true">
???<column name="COLUMN_ID" length="22" />
??</many-to-one>
??-->
??<set name="csSubjects" inverse="true" cascade="save-update"
???lazy="false" order-by="THE_ORDER" table="CS_SUBJECT"
???outer-join="true">
???<key>
????<column name="CHANNEL_ID" index="CHANNEL_ID" />
????<column name="COLUMN_ID" index="COLUMN_ID" />
???</key>
???<one-to-many
????class="com.easou.wapsearch.channel.entity.CsSubject" />
??</set>
?</class>
</hibernate-mapping>
經驗總結:
1、本來使用hibernate的加載策略就是為了把一些業務邏輯直接融合在數據庫關系當中,但是由于自己邏輯沒有考慮清楚造成了加載時信息的丟失(而且我覺得我的表結構有問題,不知有有沒有dba給我指點一下問題)。
2、hibernate的一對多關聯關系多端是1端是針對主鍵的,所以不論你是關聯的是1列2列還是3列,它們都應該是你的1端的主鍵或者聯合主鍵(其實一對一,多對一,多對多的原理也是相似的)。
參考資料:hibernate_reference(3.2)
用戶在瀏覽網頁時,如果在網頁中單擊鼠標右鍵并選擇菜單中的“查看源文件”選項后,系統就會調用記事本打開該網頁的HTML源文件,用戶可以通過修改注冊表來更改默認的打開程序,然后如下:
一:打開注冊表。
“開始”菜單->“運行”->輸入 regedit 然后點確定。
二:打開HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\INTERNET EXPLORER,在其下新建一個主鍵"View Source Editor",在其下再新一個主鍵"Editor Name",又擊右側窗口中的"默認"將數值設為更換程序的路徑及文件名.
三:重新啟動INTERNET EXPLORER,查看某個網頁的源文件,用戶就會發現打開的程序已經更改了.