下面是打印网页部分内容的代码,但是……,求解!
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Securities.aspx.cs" Inherits="Maticsoft.Web.Securities" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>查看审核密码</title>
<link rel="Stylesheet" href="css/StyleSheet1.css" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
不打印
<asp:Panel ID="Panel1" runat="server">
打印
</asp:Panel>
<asp:ImageButton ID="ImageButton1" runat="server" onclick="ImageButton1_Click" />
</form>
</body>
</html>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Maticsoft.Web
{
public partial class Securities : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
OutPutDocumnet("word");
}
public void OutPutDocumnet(string type)
{
//定义文档类型、字符编码
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
string filename = DateTime.Now.ToString() + "优惠劵.doc";
string fname = HttpUtility.UrlEncode(filename);
//attachment 参数表示作为附件下载,可以改成 online在线打开
//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm
if (type == "word")
Response.AppendHeader("Content-Disposition", "attachment;filename=" + fname);
else if (type == "excel")
Response.AppendHeader("Content-Disposition", "attachment;filename=" + fname);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
if (type == "word")
Response.ContentType = "application/ms-word";
else if (type == "excel")
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
// 定义一个输入流
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.Panel1.RenderControl(oHtmlTextWriter);
//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
Response.Write(oStringWriter.ToString());
Response.End();
}
}
}
XP系统下运行的结果:打印
win7系统下运行的结果:
<div id="Panel1">
打印
</div>
貌似不太兼容win7,如何使这段代码兼容win7?