62,046
社区成员
发帖
与我相关
我的任务
分享
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=zhuangdingshengchanjihuabiao.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.Write("<table border='1'>");
Response.Write("<tr>");
//使用Gridview的数据源datatable
//遍历datatable,重组新table
Response.Write("</tr>");
Response.Write("</table>");
Response.End();
private DataTable ReadGridView()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("序号", typeof(string)));
dt.Columns.Add(new DataColumn("姓名", typeof(string)));
dt.Columns.Add(new DataColumn("身 份 证", typeof(string)));
dt.Columns.Add(new DataColumn("参加工作时间(年、月)", typeof(string)));
dt.Columns.Add(new DataColumn("离退休时间(年、月)", typeof(string)));
dt.Columns.Add(new DataColumn("实有工龄", typeof(int)));
dt.Columns.Add(new DataColumn("现任职称", typeof(string)));
dt.Columns.Add(new DataColumn("存量补贴标准(元/年)", typeof(decimal)));
dt.Columns.Add(new DataColumn("存量补贴额(元)", typeof(decimal)));
dt.Columns.Add(new DataColumn("备注", typeof(string)));
CheckBox chk = new CheckBox();
List<string> obj = new List<string>();
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
chk = this.GridView1.Rows[i].FindControl("chkSel") as CheckBox;
if (chk.Checked)
{
string strWh = "";
strWh = "id=" + chk.Text;
obj.Add(strWh);
}
}
StringBuilder str =new StringBuilder();
for (int i = 0; i < obj.Count; i++)
{
if (i == obj.Count - 1)
{
str.Append(obj[i]);
}
else
{
str.Append(obj[i]+" or ");
}
}
whe = str.ToString();
List<house.Model.employee> list = getEmpList(GetCount("employee"), 1, whe);
for (int i = 0; i < list.Count; i++)
{
dr = dt.NewRow();
dr[0] = list[i].eid;
dr[1] = list[i].name;
dr[2] =list[i].cardId.ToString();
dr[3] = list[i].joinJoyTime;
dr[4] = list[i].toRetireTime;
dr[5] = list[i].WorkAge.ToString();
if (list[i].incumbentHeadship != "")
dr[6] = list[i].incumbentHeadship;
else
{
dr[6] = "无";
}
dr[7] = list[i].subsidyCriterion.ToString();
dr[8] = list[i].subsidyMoney.ToString();
if (list[i].remark!= "")
dr[9] = list[i].remark;
else {
dr[9] = "无";
}
dt.Rows.Add(dr);
}
return dt;
}
DataTable dt = ReadGridView();
GridView ds = new GridView();
ds.DataSource = dt;
ds.DataBind();
//生成将要存放结果的Excel文件的名称
string NewFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
//转换为物理路径
NewFileName = Server.MapPath("excel/" + NewFileName);
//根据模板正式生成该Excel文件
File.Copy(Server.MapPath("excel/mouldtest.xls"), NewFileName, true);
//建立指向该Excel文件的数据库连接
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + NewFileName + ";Extended Properties='Excel 8.0;'";
OleDbConnection Conn = new OleDbConnection(strConn);
//打开连接,为操作该文件做准备
Conn.Open();
OleDbCommand Cmd = new OleDbCommand("", Conn);
foreach (GridViewRow dr in ds.Rows)
{
string XSqlString = "insert into [Sheet1$]";
XSqlString += "([序号],[姓名],[身 份 证],[参加工作时间(年、月)],[离退休时间(年、月)],[实有工龄],[现任职称],[存量补贴标准(元/年)],[存量补贴额(元)],[备注]) values(";
XSqlString += "'" + dr.Cells[0].Text + "',";
XSqlString += "'" + dr.Cells[1].Text + "',";
XSqlString += "'" + dr.Cells[2].Text + "',";
XSqlString += "'" + dr.Cells[3].Text + "',";
XSqlString += "'" + dr.Cells[4].Text + "',";
XSqlString += "'" + dr.Cells[5].Text + "',";
XSqlString += "'" + dr.Cells[6].Text + "',";
XSqlString += "'" + dr.Cells[7].Text + "',";
XSqlString += "'" + dr.Cells[8].Text + "',";
XSqlString += "'" + dr.Cells[9].Text + "')";
Cmd.CommandText = XSqlString;
Cmd.ExecuteNonQuery();
}
//操作结束,关闭连接
Conn.Close();
//打开要下载的文件,并把该文件存放在FileStream中
System.IO.FileStream Reader = System.IO.File.OpenRead(NewFileName);
//文件传送的剩余字节数:初始值为文件的总大小
long Length = Reader.Length;
Response.Buffer = false;
Response.AddHeader("Connection", "Keep-Alive");
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode("存量.xls"));
Response.AddHeader("Content-Length", Length.ToString());
byte[] Buffer = new Byte[10000]; //存放欲发送数据的缓冲区
int ByteToRead; //每次实际读取的字节数
while (Length > 0)
{
//剩余字节数不为零,继续传送
if (Response.IsClientConnected)
{
//客户端浏览器还打开着,继续传送
ByteToRead = Reader.Read(Buffer, 0, 10000); //往缓冲区读入数据
Response.OutputStream.Write(Buffer, 0, ByteToRead); //把缓冲区的数据写入客户端浏览器
Response.Flush(); //立即写入客户端
Length -= ByteToRead; //剩余字节数减少
}
else
{
//客户端浏览器已经断开,阻止继续循环
Length = -1;
}
}
//关闭该文件
Reader.Close();
//删除该Excel文件
File.Delete(NewFileName);