Printaddresslist.aspx:
<%@ page language= "C #" autoeventwireup= "true" codefile= "PrintAddressList.aspx.cs" inherits= "Test_ Pringaddresslist "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<script language= "javascript" type= "Text/javascript" >
function Btnexport_onclick () {
HideIframe.location.href = "/web/handler/exportxlshandler.ashx?temp=" + (new Date ()). GetTime ();
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<input id= "btnexport" type= "button" value= "Export Contacts" onclick= "return Btnexport_onclick ()"/><br/>
<asp:gridview id= "GridView1" runat= "Server" allowpaging= "True"
autogeneratecolumns= "False" backcolor= "white" bordercolor= "#3366CC"
Borderstyle= "None" borderwidth= "1px" cellpadding= "4" datasourceid= "Odsuserinfo"
Width= "572px" >
<rowstyle backcolor= "White" forecolor= "#003399"/>
<Columns>
<asp:boundfield datafield= "id" headertext= "id" sortexpression= "id"/>
<asp:boundfield datafield= "Dutyinfono" headertext= "Dutyinfono"
sortexpression= "Dutyinfono"/>
<asp:boundfield datafield= "UserName" headertext= "UserName"
sortexpression= "UserName"/>
<asp:boundfield datafield= "mobile" headertext= "mobile"
sortexpression= "Mobile"/>
<asp:boundfield datafield= "QQ" headertext= "QQ" sortexpression= "QQ"/>
<asp:boundfield datafield= "Email" headertext= "email" sortexpression= "email"/>
<asp:boundfield datafield= "Deptinfono" headertext= "Deptinfono"
sortexpression= "Deptinfono"/>
</Columns>
<footerstyle backcolor= "#99CCCC" forecolor= "#003399"/>
<pagerstyle backcolor= "#99CCCC" forecolor= "#003399" horizontalalign= "left"/>
<selectedrowstyle backcolor= "#009999" font-bold= "True" forecolor= "#CCFF99"/>
</asp:GridView>
<asp:objectdatasource id= "Odsuserinfo" runat= "Server"
Selectmethod= "GetUserInfo" typename= "ExtOA.Biz.UserInfoBiz" >
</asp:ObjectDataSource>
</div>
<iframe name= "Hideiframe" id= "Hideiframe" src= "" width= "0" height= "0" scrolling= "no" frameborder= "0" style= " Display:none "></iframe>
</form>
</body>
EXPORTXLSHANDLER.ASHX:
<%@ WebHandler language= "C #" class= "Exportxlshandler"%>
Using System;
Using System.Data.SqlClient;
Using System.Data.OleDb;
Using System.IO;
Using System.Text;
Using System.Web;
Using System.Web.SessionState; To use the Session, you must join this namespace
Using System.Web.Configuration;
Using System.Collections.Generic;
Using Extoa.biz;
Using Extoa.ent;
public class Exportxlshandler: IHttpHandler, irequiressessionstate
{
String mErr = "", Tp_title = "National News education Employee Address Book", Tfile = "", Sfile = "", Wpath = "";
String BranchName1 = (new Branchinfobiz ()). Getbranchinfobyid (1). Branchname;
String BranchName2 = (new Branchinfobiz ()). Getbranchinfobyid (2). Branchname;
<summary>
Save data (add or modify data)
</summary>
<param name= "Oledb_conn" >conn</param>
<param name= "SheetName" > Sheet name </param>
<param name= "Type" >insert|update</param>
private void SaveData (OleDbConnection oledb_conn, String sheetname, String type)
{
using (OleDbCommand Oledb_command = new OleDbCommand ())
{
String SqlString = "";
Oledb_command. Connection = Oledb_conn;
#region Update ' Address Book name '
SqlString = "Update [" + SheetName + "$] Set remark = @remark Where remark = ' name ';";
Oledb_command.commandtext = SqlString;
Oledb_command. Parameters.clear ();
Oledb_command. Parameters.addwithvalue ("Remark", String. Format ("National Education Directory ({0})", SheetName));
Oledb_command. ExecuteNonQuery ();
#endregion
#region Update ' tab date '
SqlString = "Update [" + SheetName + "$] Set remark = @remark Where remark = ' tabulation date ';";
Oledb_command.commandtext = SqlString;
Oledb_command. Parameters.clear ();
Oledb_command. Parameters.addwithvalue ("Remark", "date of tabulation:" + DateTime.Now.ToString ("Yyyy/mm/dd"));
Oledb_command. ExecuteNonQuery ();
#endregion
Ilist<userinfo> Userinfos = (new Userinfobiz ()). Getuserinfobydeptinfoid (1);//Get a list of all employees of the agency by agency ID
int index = 0;
foreach (UserInfo UserInfo in Userinfos)
{
index++;
if (type = = "Insert")
SqlString = "INSERT INTO [" + SheetName + "$] (remark,depart,username,dutyname,mobile,qq,email) VALUES (@remark, @depart, @ Username, @dutyname, @mobile, @qq, @email) ";
Else
SqlString = "Update [" + BranchName2 + "$] set remark = @remark, [email protected],[email protected],[email Protected],[ema Il protected],[email protected],[email protected] where remark= ' "+ Index +" ' ";
Oledb_command.commandtext = SqlString;
Oledb_command. Parameters.clear ();
Oledb_command. Parameters.addwithvalue ("Remark", "" + Index. ToString () + "");
String deptname = (new Departinfobiz ()). Getdepartinfobyid (Userinfo.deptinfono). Departname;
Oledb_command. Parameters.addwithvalue ("Depart", Deptname);
Oledb_command. Parameters.addwithvalue ("username", userinfo.username);
String dutyname = (new Dutyinfobiz ()). Getdutyinfobyid (Userinfo.dutyinfono). Dutyname.split ('-') [1];
Oledb_command. Parameters.addwithvalue ("Dutyname", dutyname);
Oledb_command. Parameters.addwithvalue ("mobile", userinfo.mobile);
Oledb_command. Parameters.addwithvalue ("QQ", USERINFO.QQ);
Oledb_command. Parameters.addwithvalue ("email", userinfo.email);
Oledb_command. ExecuteNonQuery ();
}
}
}
public void ProcessRequest (HttpContext context)
{
#region copying Excel files to resolve common issues
Wpath = context. Server.MapPath ("~/xls/default/");
Sfile = Wpath + "Addresstemplate.xls";
Wpath = context. Server.MapPath ("~/xls/excel/");
Tfile = Wpath + string. Format ("National news Education staff Address Book ({0}{1}). xls", DateTime.Now.Year.ToString ("D2"), DateTime.Now.Month.ToString ("D2"));
File.delete (Tfile);
if (file.exists (tfile))
MERR = "File cannot be created!\\n";
Else
File.Copy (Sfile, Tfile, true);
#endregion
#region Open a new Excel file
if (MERR = = "")
{
First column of Excel field name
String xls_conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data source="
+ Tfile + "; Extended properties=\ "Excel 8.0; Hdr=yes\ "";
The first column of Excel does not have a field name
String xls_conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Tfile + "; Extended properties=\ "Excel 8.0; Hdr=no\ "";
using (OleDbConnection oledb_conn = new OleDbConnection (Xls_conn))
{
Oledb_conn. Open ();
SaveData (oledb_conn,branchname1, "Insert");//operation of the Shanghai campus
SaveData (oledb_conn,branchname2, "Update"); Operation of the Beijing campus
Oledb_conn. Close ();
string filename = string. Format (".. /xls/excel/Education Staff Address Book ({0}{1}). xls ", DateTime.Now.Year.ToString (" D2 "), DateTime.Now.Month.ToString (" D2 "));
Context. Response.Redirect (filename + "? xtemp=" + DateTime.Now.ToString ("HHMMSS"));
}
#endregion
if (MERR! = "")
{
Setting the output format
Context. Response.ContentType = "text/html";
Context. Response.Write ("<script type=\" text/javascript\ ">alert (\" "+ MERR +" \ ");/script>");
Context. Response.End ();
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
Export of. NET Excel documents