我用Response.ContentType = "application/vnd.ms-excel"生成的EXCEL打开后EXCEL中怎么全都是网页源代码啊,大家帮忙看看怎么解决,源代码如下:
printBook.vbImports System.Data.SqlClient
Public Class printBook
Inherits System.Web.UI.Page
Dim conn As SqlConnection
Private connectionstring As String
Dim strSql As String
Dim sda As SqlDataAdapter
Protected WithEvents btnprint As System.Web.UI.HtmlControls.HtmlInputButton
Protected WithEvents test As System.Web.UI.WebControls.Panel
Dim ds As DataSet#Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub
Protected WithEvents lblTemp As System.Web.UI.WebControls.Label
Protected WithEvents ltoolbar As System.Web.UI.WebControls.Label
Protected WithEvents file As System.Web.UI.WebControls.Label '注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
connectionstring = System.Configuration.ConfigurationSettings.AppSettings("ConnString").ToString.Trim
conn = New SqlConnection(connectionstring)
'"<a onclick=window.print() style='cursor:hand'>打印该页</a>"
file.Text = "<a href='javascript:printbook()'>生成并下载Excel文件</a>"
End Sub
Public Sub toprint()
strSql = Session("urlSql")
If strSql = "" Then
Response.Write("<script>alert('出错了,没有传入任何参数!');window.close();</script>")
Exit Sub
End If
Try
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.Open()
sda = New SqlDataAdapter(strSql, conn)
ds = New DataSet
sda.Fill(ds, "book")
Dim i As Integer
If ds.Tables("book").Rows.Count = 0 Then
Response.Write("<tr><td width='100%'>今天没有录入任何数据!</p></td></tr>")
Else
For i = 0 To ds.Tables("book").Rows.Count - 1
Response.Write("<tr>")
Response.Write("<td width='70%'>" & ds.Tables("book").Rows(i)(1) & "</td>")
Response.Write("<td width='30%' rowspan='2'>" & ds.Tables("book").Rows(i)(0) & "<td>")
Response.Write("</tr>")
Response.Write("<tr>")
Response.Write("<td>" & ds.Tables("book").Rows(i)(2) & "<td>")
Next
End If
Catch ex As Exception
Server.Transfer("../../inc/err.aspx?msg=" & ex.Message)
Finally
strSql = ""
sda.Dispose()
ds.Clear()
ds.Dispose()
conn.Close()
End Try
End Sub
Private Sub btnprint_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprint.ServerClick
Me.lblTemp.Visible = False
ltoolbar.Text = ""
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("Content-Disposition", "attachment;filename=" & HttpUtility.UrlEncode("今日录入图书" & ".xls", System.Text.Encoding.UTF8))
Me.EnableViewState = True
Dim oStringWriter As System.IO.StringWriter = New System.IO.StringWriter
Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)
Me.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString())
Response.End()
End Sub
End ClassprintBook.aspx<%@ Page Language="vb" AutoEventWireup="false" Codebehind="printBook.aspx.vb" Inherits="hrms_system.printBook"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>打印今天录入</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<!-- #include file="../../css/Style.css" -->
<script language="javascript">
function printbook(){
//document.location.href = "printBook.aspx?file=T";
document.getElementById("btnprint").click();
}
function toclose(){
window.close();
}
</script>
</HEAD>
<body style="MARGIN: 0px" vLink="#ffffff" aLink="#ffffff" link="#ffffff" bgColor="#f7fbff">
<form id="Form1" method="post" runat="server">
<TABLE style="WIDTH: 100%" cellSpacing="0" cellPadding="0" background="../../image/alert_bg.gif"
runat="server" ID="Table1">
<TR style="HEIGHT: 12px">
<TD style="WIDTH: 33%; HEIGHT: 3px"><FONT face="宋体" color="white" size="2">
<asp:Label id="lblTemp" runat="server">工具箱:</asp:Label>
<asp:label id="ltoolbar" runat="server"></asp:label>
<asp:Label id="file" runat="server"></asp:Label><INPUT id="btnprint" style="DISPLAY: none; WIDTH: 24px; HEIGHT: 16px" type="button" value="Button"
name="btnprint" runat="server"></FONT></TD>
</TR>
</TABLE>
<TABLE id="Table2" cellSpacing="0" cellPadding="0" width="100%" border="1">
<%toprint()%>
</TABLE>
</form>
</body>
</HTML>
printBook.vbImports System.Data.SqlClient
Public Class printBook
Inherits System.Web.UI.Page
Dim conn As SqlConnection
Private connectionstring As String
Dim strSql As String
Dim sda As SqlDataAdapter
Protected WithEvents btnprint As System.Web.UI.HtmlControls.HtmlInputButton
Protected WithEvents test As System.Web.UI.WebControls.Panel
Dim ds As DataSet#Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub
Protected WithEvents lblTemp As System.Web.UI.WebControls.Label
Protected WithEvents ltoolbar As System.Web.UI.WebControls.Label
Protected WithEvents file As System.Web.UI.WebControls.Label '注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
connectionstring = System.Configuration.ConfigurationSettings.AppSettings("ConnString").ToString.Trim
conn = New SqlConnection(connectionstring)
'"<a onclick=window.print() style='cursor:hand'>打印该页</a>"
file.Text = "<a href='javascript:printbook()'>生成并下载Excel文件</a>"
End Sub
Public Sub toprint()
strSql = Session("urlSql")
If strSql = "" Then
Response.Write("<script>alert('出错了,没有传入任何参数!');window.close();</script>")
Exit Sub
End If
Try
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.Open()
sda = New SqlDataAdapter(strSql, conn)
ds = New DataSet
sda.Fill(ds, "book")
Dim i As Integer
If ds.Tables("book").Rows.Count = 0 Then
Response.Write("<tr><td width='100%'>今天没有录入任何数据!</p></td></tr>")
Else
For i = 0 To ds.Tables("book").Rows.Count - 1
Response.Write("<tr>")
Response.Write("<td width='70%'>" & ds.Tables("book").Rows(i)(1) & "</td>")
Response.Write("<td width='30%' rowspan='2'>" & ds.Tables("book").Rows(i)(0) & "<td>")
Response.Write("</tr>")
Response.Write("<tr>")
Response.Write("<td>" & ds.Tables("book").Rows(i)(2) & "<td>")
Next
End If
Catch ex As Exception
Server.Transfer("../../inc/err.aspx?msg=" & ex.Message)
Finally
strSql = ""
sda.Dispose()
ds.Clear()
ds.Dispose()
conn.Close()
End Try
End Sub
Private Sub btnprint_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprint.ServerClick
Me.lblTemp.Visible = False
ltoolbar.Text = ""
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("Content-Disposition", "attachment;filename=" & HttpUtility.UrlEncode("今日录入图书" & ".xls", System.Text.Encoding.UTF8))
Me.EnableViewState = True
Dim oStringWriter As System.IO.StringWriter = New System.IO.StringWriter
Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)
Me.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString())
Response.End()
End Sub
End ClassprintBook.aspx<%@ Page Language="vb" AutoEventWireup="false" Codebehind="printBook.aspx.vb" Inherits="hrms_system.printBook"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>打印今天录入</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<!-- #include file="../../css/Style.css" -->
<script language="javascript">
function printbook(){
//document.location.href = "printBook.aspx?file=T";
document.getElementById("btnprint").click();
}
function toclose(){
window.close();
}
</script>
</HEAD>
<body style="MARGIN: 0px" vLink="#ffffff" aLink="#ffffff" link="#ffffff" bgColor="#f7fbff">
<form id="Form1" method="post" runat="server">
<TABLE style="WIDTH: 100%" cellSpacing="0" cellPadding="0" background="../../image/alert_bg.gif"
runat="server" ID="Table1">
<TR style="HEIGHT: 12px">
<TD style="WIDTH: 33%; HEIGHT: 3px"><FONT face="宋体" color="white" size="2">
<asp:Label id="lblTemp" runat="server">工具箱:</asp:Label>
<asp:label id="ltoolbar" runat="server"></asp:label>
<asp:Label id="file" runat="server"></asp:Label><INPUT id="btnprint" style="DISPLAY: none; WIDTH: 24px; HEIGHT: 16px" type="button" value="Button"
name="btnprint" runat="server"></FONT></TD>
</TR>
</TABLE>
<TABLE id="Table2" cellSpacing="0" cellPadding="0" width="100%" border="1">
<%toprint()%>
</TABLE>
</form>
</body>
</HTML>
{
this.DataList1.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
this.DataList1.RenderControl(hw);
string HtmlInfo = tw.ToString().Trim();
string DocFileName = "销售报表.xls";
string FilePathName = Request.PhysicalPath;
FilePathName = FilePathName.Substring(0, FilePathName.LastIndexOf(""));
//得到Excel文件的物理地址
FilePathName = FilePathName + "" + DocFileName;
System.IO.File.Delete(FilePathName);
FileStream Fs = new FileStream(FilePathName, FileMode.Create);
BinaryWriter BWriter = new BinaryWriter(Fs, System.Text.Encoding.GetEncoding("GB18030"));
//将DataGrid的信息写入Excel文件 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FilePathName, System.Text.Encoding.UTF8));
BWriter.Write(HtmlInfo);
BWriter.Close();
Fs.Close();
}
供参考。。
必须使toprint在pageload中执行,方法,使用一label标签,将生成的表格字段存在label标签中执行,OK,大功告成,帖子不删了,供大家参考,谢谢大家的参与