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,查看某个网页的源文件,用户就会发现打开的程序已经更改了.