protected void Button2_Click(object sender, EventArgs e) { System.IO.StringWriter SW = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter HTW = new System.Web.UI.HtmlTextWriter(SW); Page.EnableViewState = false; Page.Form.RenderControl(HTW); //Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以 string pageHtml = SW.ToString(); string pageHtml = ReplaceText(filecontent, getmodel()); int startIndex = pageHtml.IndexOf("<div id=\"PrintA\">"); int endIndex = pageHtml.LastIndexOf("</div>"); int lenth = endIndex - startIndex; pageHtml = pageHtml.Substring(startIndex, lenth); pageHtml = pageHtml.Replace("../images/1.jpg", Server.MapPath("../images/1.jpg")); Response.Buffer = true; Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/ms-word"; //Response.ContentType是输出流的 HTTP MIME 类型 //Response.ContentType --- word文件 //application/vnd.ms-excel --- excel文件 Response.Charset = "utf-8"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); Response.AddHeader("Content-Disposition", "attachment;filename=" + xxxx + ".doc"); // Response.AddHeader("content-length", file.Length.ToString()); //attachment --- 作为附件下载 //inline --- 在线打开 //filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) //进行进行编码,以解决文件名乱码的问题 Response.Write(pageHtml.ToString()); Response.Flush(); Response.Close(); }
用Uri MyUrl = Request.UrlReferrer;获取到客户端看到的地址和端口等解决图片问题