手动导出Excel方法

时间:2024-12-14 13:34:38

private void dgJiBingZhenDuanBind()

{

    Response.Clear();

   Response.Charset = "GB2312";

Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。

Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("柘荣县医院住院病历" + DateTime.Now.ToShortDateString() + ".xls", Encoding.UTF8));         this.EnableViewState = false;

StringBuilder sbHtml = new StringBuilder();

sbHtml.Append("<table border='1'>");

sbHtml.Append("<tr>");

sbHtml.Append("<td>住院号</td>");

sbHtml.Append("<td>姓名</td>");

sbHtml.Append("<td>性别</td>");

//  sbHtml.Append("<td>床位号</td>");

sbHtml.Append("<td>出生日期</td>");

//sbHtml.Append("<td>职业</td>");

// sbHtml.Append("<td>地址</td>");

sbHtml.Append("<td>入院时间</td>");

sbHtml.Append("<td>入院诊断</td>");

sbHtml.Append("<td>出院时间</td>");

sbHtml.Append("<td>出院诊断</td>");

sbHtml.Append("<td>医生</td>");

sbHtml.Append("</tr>");

DataTable dt = Session["BingLiChaXun"] as DataTable;

if (CanShuPanDuan.ShiFouKong(dt, 1))

{

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "msg", "<script language='javascript'>yanshitishi('系统提示:打印暂无数据,请先进行查询!','2')</script>");

return;

}

//逐行处理数据

foreach (DataRow row in dt.Rows)

{

sbHtml.Append("<tr>");

sbHtml.Append("<td>" + row["ZHUYUANHAO"].ToString() + "</td>");

sbHtml.Append("<td>" + row["XINGMING"].ToString() + "</td>");

sbHtml.Append("<td>" + row["XINGBIE"].ToString() + "</td>");

//sbHtml.Append("<td>" + row["chuanghao"].ToString() + "</td>");

sbHtml.Append("<td>" + row["newnianling"].ToString() + "</td>"); //  杨磊

//sbHtml.Append("<td>" + row["zhiye"].ToString() + "</td>");

//sbHtml.Append("<td>" + row["hukoudizhi"].ToString() + "</td>");

sbHtml.Append("<td>" + row["RUYUANSHIJIAN"].ToString() + "</td>");

sbHtml.Append("<td>" + row["ruyuanzhenduan1"].ToString() + "</td>");

sbHtml.Append("<td>" + row["CHUYUANSHIJIAN"].ToString() + " </td>");

sbHtml.Append("<td>" + row["chuyuanzhenduan1"].ToString() + "</td>");

sbHtml.Append("<td>" + row["yisheng"].ToString() + "</td>");

sbHtml.Append("</tr>");

}

sbHtml.Append("</table>");

Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");

Response.Write(sbHtml.ToString());

Response.End();

}