using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Services;
using
System.Web.SessionState;
using
System.Data;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Text;
using
System.Data.Common;
using
Microsoft.Practices.EnterpriseLibrary.Common;
using
Microsoft.Practices.EnterpriseLibrary.Data;
using
Kimbanx.UCS.ForeignStudentAdmin.Model;
using
Kimbanx.UCS.ForeignStudentAdmin.Common;
using
Word
=
Microsoft.Office.Interop.Word;
using
System.IO;
using
Microsoft.SharePoint;
namespace
Kimbanx.UCS.ForeignStudentAdmin.UserControl._layouts
{
///
<summary>
///
$codebehindclassname$ 的摘要说明
///
</summary>
[WebService(Namespace
=
"
http://tempuri.org/
"
)]
[WebServiceBinding(ConformsTo
=
WsiProfiles.BasicProfile1_1)]
public
class
StudentDetailsTableWordHandler_V2 : IHttpHandler, IReadOnlySessionState
{
#region
Fields
private
DataTable _studentTable
=
null
;
private
string
selected;
private
int
pageIndex;
private
int
pageSize;
private
string
queryStringWhere;
private
string
studentType;
private
string
classCode;
private
string
fileName;
private
int
gvColCount;
private
Dictionary
<
int
,
string
>
fieldsSelectedDic;
private
List
<
int
>
fieldsSelected;
#endregion
#region
Methods
private
void
GetRequestQueryString()
{
selected
=
HttpUtility.UrlDecode ( HttpContext.Current.Request.QueryString[
"
selected
"
]);
pageIndex
=
int
.Parse(HttpContext.Current.Request.QueryString[
"
pageIndex
"
]);
pageSize
=
int
.Parse(HttpContext.Current.Request.QueryString[
"
pageSize
"
]);
queryStringWhere
=
HttpContext.Current.Request.QueryString[
"
queryStringWhere
"
];
studentType
=
HttpUtility.UrlDecode(HttpContext.Current.Request.QueryString[
"
studentType
"
]);
classCode
=
HttpContext.Current.Request.QueryString[
"
classCode
"
];
gvColCount
=
int
.Parse(HttpContext.Current.Request.QueryString[
"
gvColCount
"
]);
fileName
=
HttpUtility.UrlDecode(HttpContext.Current.Request.QueryString[
"
fileName
"
]);
GetFieldsSelected(selected);
}
private
void
GetFieldsSelected(
string
selected)
{
fieldsSelected
=
new
List
<
int
>
();
fieldsSelectedDic
=
new
Dictionary
<
int
,
string
>
();
string
[] strSplit
=
new
string
[] {
"
--
"
};
string
[] tokenGroup
=
selected.Split(strSplit, StringSplitOptions.None);
string
[] token
=
new
string
[
2
];
foreach
(
string
group
in
tokenGroup)
{
token
=
group.Split(
'
-
'
);
fieldsSelected.Add(
int
.Parse(token[
0
]));
fieldsSelectedDic.Add(
int
.Parse(token[
0
]), token[
1
]);
}
}
///
<summary>
///
获取分页之后的数据
///
</summary>
private
void
GetStudentTable(
int
pageSize,
int
pageIndex)
{
_studentTable
=
null
;
switch
(queryStringWhere)
{
case
"
jx
"
:
_studentTable
=
Helper.StudentPagingResult(
"
zpadress,xmqz,xmqy,xmjz,xmjy,jx,zw,gj,sjyqk,zj,csrq,rwrq,xzz,dhd,dhx,fcjp,xh
"
,
"
tx_xyzl
"
,
string
.Format(
"
dwbh='{0}' and jx='{1}'
"
, classCode, studentType),
"
id
"
,
pageSize,
pageIndex);
//
this.gvStudent.DataBind();
break
;
case
"
gj
"
:
_studentTable
=
Helper.StudentPagingResult(
"
zpadress,xmqz,xmqy,xmjz,xmjy,jx,zw,gj,sjyqk,zj,csrq,rwrq,xzz,dhd,dhx,fcjp,xh
"
,
"
tx_xyzl
"
,
string
.Format(
"
dwbh='{0}' and gj='{1}'
"
, classCode, studentType),
"
id
"
,
pageSize,
pageIndex);
//
this.gvStudent.DataBind();
break
;
case
"
allyear
"
:
_studentTable
=
Helper.StudentPagingResult(
"
s.zpadress,s.xmqz,s.xmqy,s.xmjz,s.xmjy,s.jx,s.zw,s.gj,s.sjyqk,s.zj,s.csrq,s.rwrq,s.xzz,s.dhd,s.dhx,s.fcjp,s.xh
"
,
"
tx_xyzl as s inner join tx_dwsz as dw on s.dwbh=dw.bh
"
,
string
.Format(
@"
s.dwbh='{0}' and (dw.kxsj>=convert(datetime,'{1}'+'-01-01',120) and
dw.kxsj<=convert(datetime,'{1}'+'-12-31',120) or dw.bysj>=convert(datetime,'{1}'+'-01-01',120) and
dw.bysj<=convert(datetime,'{1}'+'-12-31',120))
"
, classCode, studentType),
"
s.id
"
,
pageSize,
pageIndex);
//
this.gvStudent.DataBind();
break
;
case
"
new
"
:
_studentTable
=
Helper.StudentPagingResult(
"
s.zpadress,s.xmqz,s.xmqy,s.xmjz,s.xmjy,s.jx,s.zw,s.gj,s.sjyqk,s.zj,s.csrq,s.rwrq,s.xzz,s.dhd,s.dhx,s.fcjp,s.xh
"
,
"
tx_xyzl as s inner join tx_dwsz as dw on s.dwbh=dw.bh
"
,
string
.Format(
@"
s.dwbh='{0}' and (dw.kxsj>=convert(datetime,'{1}'+'-01-01',120) and
dw.kxsj<=convert(datetime,'{1}'+'-12-31',120))
"
, classCode, studentType),
"
s.id
"
,
pageSize,
pageIndex);
//
this.gvStudent.DataBind();
break
;
default
:
_studentTable
=
Helper.StudentPagingResult(
"
zpadress,xmqz,xmqy,xmjz,xmjy,jx,zw,gj,sjyqk,zj,csrq,rwrq,xzz,dhd,dhx,fcjp,xh
"
,
"
tx_xyzl
"
,
string
.Format(
"
dwbh='{0}'
"
, classCode),
"
id
"
,
pageSize,
pageIndex);
//
this.gvStudent.DataBind();
break
;
}
}
#endregion
public
void
ProcessRequest(HttpContext context)
{
SPSecurity.RunWithElevatedPrivileges(
delegate
()
{
GetRequestQueryString();
GetStudentTable(
this
.pageSize,
this
.pageIndex);
StudentDetailsTable_Word_V2 customWord
=
new
StudentDetailsTable_Word_V2();
customWord.CreateWordFile();
customWord.SetPageMargin();
customWord.InsertTitle(customWord.ODoc, fileName);
int
tableTitleRows
=
1
;
int
tableRows
=
_studentTable.Rows.Count
+
tableTitleRows;
int
tableCols
=
gvColCount;
customWord.InsertTableToWord(customWord.ODoc, tableRows, fieldsSelected.Count);
Word.Table table
=
customWord.ODoc.Tables[
1
];
customWord.InsertTableTitle(customWord.ODoc, table, fieldsSelected, fieldsSelectedDic);
table.Borders.InsideLineStyle
=
Word.WdLineStyle.wdLineStyleSingle;
table.Borders.OutsideLineStyle
=
Word.WdLineStyle.wdLineStyleSingle;
object
row
=
table.Rows[
1
];
customWord.InsertDataToWord(table, _studentTable, tableRows
-
tableTitleRows, tableCols, fieldsSelected);
#region
string
tempFileFullName
=
customWord.Save(Settings.ImageLocalhostTempSaveDirectory, fileName);
string
tempFileName
=
tempFileFullName.Substring(tempFileFullName.LastIndexOf(
"
\\
"
)
+
1
);
using
(FileStream stream
=
new
FileStream(tempFileFullName, FileMode.Open))
{
byte
[] fileBytes
=
new
byte
[stream.Length];
stream.Read(fileBytes,
0
, (
int
)stream.Length);
HttpContext.Current.Response.ContentType
=
"
application/msword
"
;
HttpContext.Current.Response.AddHeader(
"
content-disposition
"
,
"
attachment; filename=
"
+
HttpUtility.UrlEncode(tempFileFullName));
HttpContext.Current.Response.OutputStream.Write(fileBytes,
0
, (
int
)stream.Length);
} File.Delete(tempFileFullName);
#endregion
});
}
public
bool
IsReusable
{
get
{
return
false
;
}
}
}
}