請兄弟們幫我解決一下,如果不行還有其它辦法嗎?只要能將報表內容輸出到EXCEL就行,但不生成任何新的文件。
真是不好意思,小弟實在沒分了,下次一定補上,拜托了,真的很急。
小弟還在試用期內,再搞不定就要打包了!
private void render()
{
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter tw=new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw=new System.Web.UI.HtmlTextWriter(tw);
CrystalReportViewer1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
24 个解决方案
#1
up
#2
你是说不输出成EXCEL文件保存吗?
#3
直接使用OFFICE编程吧!
#4
老大tw?????
肯定为空了!!!!
肯定为空了!!!!
#5
to hans_marke(森林) :我不是说不输出成EXCEL文件保存,而是不要生成新的文件,我上面那種方法就可以不生成新的文件而將報表數據以excel格式打開,可惜數據都不見了,有沒有辦法?
to jiaojiaohappy(小小) :tw是StringWriter對象,這樣不行嗎,那應該怎麼做?
to jiaojiaohappy(小小) :tw是StringWriter對象,這樣不行嗎,那應該怎麼做?
#6
http://expert.csdn.net/Expert/topic/1883/1883370.xml?temp=.3086054
#7
to greystar(greystar):上不去!
#8
各位兄弟請幫幫忙呀
#9
Response.BinaryWrite()
#10
還是不行啊
#11
用CrystalReport设计好表样,在ASP.NET中的REPORTDOCUMENT连接CrystalReport,
把REPORTDOCUMENT保存成PDF、WOED、EXCEL等
用WINDOW.OPEN打开
http://expert.csdn.net/Expert/topic/2000/2000702.xml?temp=.3789331
如需具体细节,请给我发邮件:hlf566@sohu.com
把REPORTDOCUMENT保存成PDF、WOED、EXCEL等
用WINDOW.OPEN打开
http://expert.csdn.net/Expert/topic/2000/2000702.xml?temp=.3789331
如需具体细节,请给我发邮件:hlf566@sohu.com
#12
hlf566: 請幫我也看看這個問題
http://expert.csdn.net/Expert/topic/2003/2003839.xml?temp=.2209131
http://expert.csdn.net/Expert/topic/2003/2003839.xml?temp=.2209131
#13
to hlf566(hlf566):你那種方法違背了我的意思,那樣會生成一個新的文件,而我不想生成任何文件。還有其它辦法嗎,幫幫忙!
#14
up
#15
100分都沒人能解決,誰幫我搞定這個問題再給100分!說話算數!
#16
是不是你调用方法有问题啊?
这个程序是在客户端生成的,你的客户端安装office200了吗?
参考:
http://xml.sz.luohuedu.net/xml/Content.asp
这个程序是在客户端生成的,你的客户端安装office200了吗?
参考:
http://xml.sz.luohuedu.net/xml/Content.asp
#17
我成功的例子:另外看数据是否得到了
Top.htm
============
<html>
<script language="vbscript">
Sub Button1_onclick
Select Case Select1.selectedIndex
Case 0
Dim sHTML
sHTML = window.parent.frames("bottom").document.forms(0).children("DataGrid1").outerhtml
Dim oXL, oBook
Set oXL = CreateObject("Excel.Application")
Set oBook = oXL.Workbooks.Add
oBook.HTMLProject.HTMLProjectItems("Sheet1").Text = sHTML
oBook.HTMLProject.RefreshDocument
oXL.Visible = true
oXL.UserControl = true
Case 1
window.open("ExcelExport.aspx?bExcel=1")
Case 2
window.parent.frames("bottom").navigate "ExcelExport.aspx?bExcel=1"
Case 3
window.parent.frames("bottom").location.href="ExcelExport.aspx"
End Select
End Sub
</script>
<body>
输出数据到Excel:
<SELECT id="Select1" size="1" name="Select1">
<OPTION value="0" selected>Automation自动化输出</OPTION>
<OPTION value="1">MIME Type (在新窗口输出)</OPTION>
<OPTION value="2">MIME Type (在幀中输出)</OPTION>
<OPTION value="3">恢复原状</OPTION>
</SELECT>
<INPUT type="button" id="Button1" value="开始输出数据到Excel">
</body>
</html>
ExcelExport.aspx
=================
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ExcelExport.aspx.vb" Inherits="aspxWeb.ExcelExport"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ExcelExport</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server" CellPadding="4" BackColor="White" BorderColor="#CC9966" BorderWidth="1px" BorderStyle="None" Width="100%" Height="100%" Font-Size="9pt" Font-Names="宋体">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#FFCC99"></AlternatingItemStyle>
<ItemStyle BorderWidth="2px" ForeColor="#330099" BorderStyle="Solid" BorderColor="Black" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" BorderWidth="2px" ForeColor="#FFFFCC" BorderStyle="Solid" BorderColor="Black" BackColor="#990000"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
</asp:datagrid>
</form>
</body>
</HTML>
ExcelExport.aspx.vb
===================
Public Class ExcelExport
Inherits System.Web.UI.Page
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' 定义是否是 SQL Server 数据库,这里为False
Dim blnIsSQLServer As System.Boolean = False
Dim strSQL As String
Dim objDataset As New DataSet()
Dim objConn As Object
If blnIsSQLServer Then
objConn = New System.Data.SqlClient.SqlConnection("User ID=sa;Initial Catalog=Northwind;Data Source=.\NetSDK;")
objConn.Open()
Dim objAdapter As New System.Data.SqlClient.SqlDataAdapter()
strSQL = "Select * from customers where country='USA'"
objAdapter.SelectCommand = New System.Data.SqlClient.SqlCommand(strSQL, objConn)
objAdapter.Fill(objDataset)
Else
objConn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("aspxWeb.mdb"))
objConn.Open()
Dim objAdapter As New System.Data.OleDb.OleDbDataAdapter()
strSQL = "Select Top 10 Title From Document"
objAdapter.SelectCommand = New System.Data.OleDb.OleDbCommand(strSQL, objConn)
objAdapter.Fill(objDataset)
End If
Dim oView As New DataView(objDataset.Tables(0))
DataGrid1.DataSource = oView
DataGrid1.DataBind()
objConn.Close()
objConn.Dispose()
objConn = Nothing
If Request.QueryString("bExcel") = "1" Then
Response.ContentType = "application/vnd.ms-excel"
' 从Content-Type header中去除charset设置
Response.Charset = ""
' 关闭 ViewState
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
' 获取control的HTML
DataGrid1.RenderControl(hw)
' 把HTML写回浏览器
Response.Write(tw.ToString())
Response.End()
End If
End Sub
End Class
Top.htm
============
<html>
<script language="vbscript">
Sub Button1_onclick
Select Case Select1.selectedIndex
Case 0
Dim sHTML
sHTML = window.parent.frames("bottom").document.forms(0).children("DataGrid1").outerhtml
Dim oXL, oBook
Set oXL = CreateObject("Excel.Application")
Set oBook = oXL.Workbooks.Add
oBook.HTMLProject.HTMLProjectItems("Sheet1").Text = sHTML
oBook.HTMLProject.RefreshDocument
oXL.Visible = true
oXL.UserControl = true
Case 1
window.open("ExcelExport.aspx?bExcel=1")
Case 2
window.parent.frames("bottom").navigate "ExcelExport.aspx?bExcel=1"
Case 3
window.parent.frames("bottom").location.href="ExcelExport.aspx"
End Select
End Sub
</script>
<body>
输出数据到Excel:
<SELECT id="Select1" size="1" name="Select1">
<OPTION value="0" selected>Automation自动化输出</OPTION>
<OPTION value="1">MIME Type (在新窗口输出)</OPTION>
<OPTION value="2">MIME Type (在幀中输出)</OPTION>
<OPTION value="3">恢复原状</OPTION>
</SELECT>
<INPUT type="button" id="Button1" value="开始输出数据到Excel">
</body>
</html>
ExcelExport.aspx
=================
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ExcelExport.aspx.vb" Inherits="aspxWeb.ExcelExport"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ExcelExport</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server" CellPadding="4" BackColor="White" BorderColor="#CC9966" BorderWidth="1px" BorderStyle="None" Width="100%" Height="100%" Font-Size="9pt" Font-Names="宋体">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#FFCC99"></AlternatingItemStyle>
<ItemStyle BorderWidth="2px" ForeColor="#330099" BorderStyle="Solid" BorderColor="Black" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" BorderWidth="2px" ForeColor="#FFFFCC" BorderStyle="Solid" BorderColor="Black" BackColor="#990000"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
</asp:datagrid>
</form>
</body>
</HTML>
ExcelExport.aspx.vb
===================
Public Class ExcelExport
Inherits System.Web.UI.Page
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' 定义是否是 SQL Server 数据库,这里为False
Dim blnIsSQLServer As System.Boolean = False
Dim strSQL As String
Dim objDataset As New DataSet()
Dim objConn As Object
If blnIsSQLServer Then
objConn = New System.Data.SqlClient.SqlConnection("User ID=sa;Initial Catalog=Northwind;Data Source=.\NetSDK;")
objConn.Open()
Dim objAdapter As New System.Data.SqlClient.SqlDataAdapter()
strSQL = "Select * from customers where country='USA'"
objAdapter.SelectCommand = New System.Data.SqlClient.SqlCommand(strSQL, objConn)
objAdapter.Fill(objDataset)
Else
objConn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("aspxWeb.mdb"))
objConn.Open()
Dim objAdapter As New System.Data.OleDb.OleDbDataAdapter()
strSQL = "Select Top 10 Title From Document"
objAdapter.SelectCommand = New System.Data.OleDb.OleDbCommand(strSQL, objConn)
objAdapter.Fill(objDataset)
End If
Dim oView As New DataView(objDataset.Tables(0))
DataGrid1.DataSource = oView
DataGrid1.DataBind()
objConn.Close()
objConn.Dispose()
objConn = Nothing
If Request.QueryString("bExcel") = "1" Then
Response.ContentType = "application/vnd.ms-excel"
' 从Content-Type header中去除charset设置
Response.Charset = ""
' 关闭 ViewState
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
' 获取control的HTML
DataGrid1.RenderControl(hw)
' 把HTML写回浏览器
Response.Write(tw.ToString())
Response.End()
End If
End Sub
End Class
#18
謝 net_lover(孟子E章) ,有沒有C#的代碼?
#19
to net_lover(孟子E章) :這種方法不是在服務器端運行的嗎?我是在服務器端調試的,在服務器上也裝了OFFICE2000,可是運行後網頁顯示的是一個空的EXCEL表格,但很怪的是原來頁面中的一些字符卻能顯示在EXCEL表格中,而DATAGRID中的內容沒法顯示。為什麼?幫幫忙!
#20
我現在在datagrid中的數據可以正常輸出EXCEL了,原因是datagrid所在頁面中放了一些其它控件,我把那些控件移除後就可以正常顯示;但問題是我想把水晶報表的數據用這種方法導出為EXCEL格式(請看下面代碼),但還是出現空的數據,為什麼DATAGRID可以而水晶報表不行?請幫幫忙,搞定後馬上給分!
private void render()
{
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter tw=new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw=new System.Web.UI.HtmlTextWriter(tw);
CrystalReportViewer1.RenderControl(hw); //DataGrid-->CrystalReportViewer1
Response.Write(tw.ToString());
Response.End();
}
private void render()
{
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter tw=new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw=new System.Web.UI.HtmlTextWriter(tw);
CrystalReportViewer1.RenderControl(hw); //DataGrid-->CrystalReportViewer1
Response.Write(tw.ToString());
Response.End();
}
#21
自已頂一下
#22
up
#23
導出數據時時用到的地方要object.tostring().trim() 一下,否則是沒有數據的.
#24
更正一下,在excel中是顯示不出來的.
#1
up
#2
你是说不输出成EXCEL文件保存吗?
#3
直接使用OFFICE编程吧!
#4
老大tw?????
肯定为空了!!!!
肯定为空了!!!!
#5
to hans_marke(森林) :我不是说不输出成EXCEL文件保存,而是不要生成新的文件,我上面那種方法就可以不生成新的文件而將報表數據以excel格式打開,可惜數據都不見了,有沒有辦法?
to jiaojiaohappy(小小) :tw是StringWriter對象,這樣不行嗎,那應該怎麼做?
to jiaojiaohappy(小小) :tw是StringWriter對象,這樣不行嗎,那應該怎麼做?
#6
http://expert.csdn.net/Expert/topic/1883/1883370.xml?temp=.3086054
#7
to greystar(greystar):上不去!
#8
各位兄弟請幫幫忙呀
#9
Response.BinaryWrite()
#10
還是不行啊
#11
用CrystalReport设计好表样,在ASP.NET中的REPORTDOCUMENT连接CrystalReport,
把REPORTDOCUMENT保存成PDF、WOED、EXCEL等
用WINDOW.OPEN打开
http://expert.csdn.net/Expert/topic/2000/2000702.xml?temp=.3789331
如需具体细节,请给我发邮件:hlf566@sohu.com
把REPORTDOCUMENT保存成PDF、WOED、EXCEL等
用WINDOW.OPEN打开
http://expert.csdn.net/Expert/topic/2000/2000702.xml?temp=.3789331
如需具体细节,请给我发邮件:hlf566@sohu.com
#12
hlf566: 請幫我也看看這個問題
http://expert.csdn.net/Expert/topic/2003/2003839.xml?temp=.2209131
http://expert.csdn.net/Expert/topic/2003/2003839.xml?temp=.2209131
#13
to hlf566(hlf566):你那種方法違背了我的意思,那樣會生成一個新的文件,而我不想生成任何文件。還有其它辦法嗎,幫幫忙!
#14
up
#15
100分都沒人能解決,誰幫我搞定這個問題再給100分!說話算數!
#16
是不是你调用方法有问题啊?
这个程序是在客户端生成的,你的客户端安装office200了吗?
参考:
http://xml.sz.luohuedu.net/xml/Content.asp
这个程序是在客户端生成的,你的客户端安装office200了吗?
参考:
http://xml.sz.luohuedu.net/xml/Content.asp
#17
我成功的例子:另外看数据是否得到了
Top.htm
============
<html>
<script language="vbscript">
Sub Button1_onclick
Select Case Select1.selectedIndex
Case 0
Dim sHTML
sHTML = window.parent.frames("bottom").document.forms(0).children("DataGrid1").outerhtml
Dim oXL, oBook
Set oXL = CreateObject("Excel.Application")
Set oBook = oXL.Workbooks.Add
oBook.HTMLProject.HTMLProjectItems("Sheet1").Text = sHTML
oBook.HTMLProject.RefreshDocument
oXL.Visible = true
oXL.UserControl = true
Case 1
window.open("ExcelExport.aspx?bExcel=1")
Case 2
window.parent.frames("bottom").navigate "ExcelExport.aspx?bExcel=1"
Case 3
window.parent.frames("bottom").location.href="ExcelExport.aspx"
End Select
End Sub
</script>
<body>
输出数据到Excel:
<SELECT id="Select1" size="1" name="Select1">
<OPTION value="0" selected>Automation自动化输出</OPTION>
<OPTION value="1">MIME Type (在新窗口输出)</OPTION>
<OPTION value="2">MIME Type (在幀中输出)</OPTION>
<OPTION value="3">恢复原状</OPTION>
</SELECT>
<INPUT type="button" id="Button1" value="开始输出数据到Excel">
</body>
</html>
ExcelExport.aspx
=================
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ExcelExport.aspx.vb" Inherits="aspxWeb.ExcelExport"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ExcelExport</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server" CellPadding="4" BackColor="White" BorderColor="#CC9966" BorderWidth="1px" BorderStyle="None" Width="100%" Height="100%" Font-Size="9pt" Font-Names="宋体">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#FFCC99"></AlternatingItemStyle>
<ItemStyle BorderWidth="2px" ForeColor="#330099" BorderStyle="Solid" BorderColor="Black" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" BorderWidth="2px" ForeColor="#FFFFCC" BorderStyle="Solid" BorderColor="Black" BackColor="#990000"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
</asp:datagrid>
</form>
</body>
</HTML>
ExcelExport.aspx.vb
===================
Public Class ExcelExport
Inherits System.Web.UI.Page
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' 定义是否是 SQL Server 数据库,这里为False
Dim blnIsSQLServer As System.Boolean = False
Dim strSQL As String
Dim objDataset As New DataSet()
Dim objConn As Object
If blnIsSQLServer Then
objConn = New System.Data.SqlClient.SqlConnection("User ID=sa;Initial Catalog=Northwind;Data Source=.\NetSDK;")
objConn.Open()
Dim objAdapter As New System.Data.SqlClient.SqlDataAdapter()
strSQL = "Select * from customers where country='USA'"
objAdapter.SelectCommand = New System.Data.SqlClient.SqlCommand(strSQL, objConn)
objAdapter.Fill(objDataset)
Else
objConn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("aspxWeb.mdb"))
objConn.Open()
Dim objAdapter As New System.Data.OleDb.OleDbDataAdapter()
strSQL = "Select Top 10 Title From Document"
objAdapter.SelectCommand = New System.Data.OleDb.OleDbCommand(strSQL, objConn)
objAdapter.Fill(objDataset)
End If
Dim oView As New DataView(objDataset.Tables(0))
DataGrid1.DataSource = oView
DataGrid1.DataBind()
objConn.Close()
objConn.Dispose()
objConn = Nothing
If Request.QueryString("bExcel") = "1" Then
Response.ContentType = "application/vnd.ms-excel"
' 从Content-Type header中去除charset设置
Response.Charset = ""
' 关闭 ViewState
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
' 获取control的HTML
DataGrid1.RenderControl(hw)
' 把HTML写回浏览器
Response.Write(tw.ToString())
Response.End()
End If
End Sub
End Class
Top.htm
============
<html>
<script language="vbscript">
Sub Button1_onclick
Select Case Select1.selectedIndex
Case 0
Dim sHTML
sHTML = window.parent.frames("bottom").document.forms(0).children("DataGrid1").outerhtml
Dim oXL, oBook
Set oXL = CreateObject("Excel.Application")
Set oBook = oXL.Workbooks.Add
oBook.HTMLProject.HTMLProjectItems("Sheet1").Text = sHTML
oBook.HTMLProject.RefreshDocument
oXL.Visible = true
oXL.UserControl = true
Case 1
window.open("ExcelExport.aspx?bExcel=1")
Case 2
window.parent.frames("bottom").navigate "ExcelExport.aspx?bExcel=1"
Case 3
window.parent.frames("bottom").location.href="ExcelExport.aspx"
End Select
End Sub
</script>
<body>
输出数据到Excel:
<SELECT id="Select1" size="1" name="Select1">
<OPTION value="0" selected>Automation自动化输出</OPTION>
<OPTION value="1">MIME Type (在新窗口输出)</OPTION>
<OPTION value="2">MIME Type (在幀中输出)</OPTION>
<OPTION value="3">恢复原状</OPTION>
</SELECT>
<INPUT type="button" id="Button1" value="开始输出数据到Excel">
</body>
</html>
ExcelExport.aspx
=================
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ExcelExport.aspx.vb" Inherits="aspxWeb.ExcelExport"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ExcelExport</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server" CellPadding="4" BackColor="White" BorderColor="#CC9966" BorderWidth="1px" BorderStyle="None" Width="100%" Height="100%" Font-Size="9pt" Font-Names="宋体">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#FFCC99"></AlternatingItemStyle>
<ItemStyle BorderWidth="2px" ForeColor="#330099" BorderStyle="Solid" BorderColor="Black" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" BorderWidth="2px" ForeColor="#FFFFCC" BorderStyle="Solid" BorderColor="Black" BackColor="#990000"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
</asp:datagrid>
</form>
</body>
</HTML>
ExcelExport.aspx.vb
===================
Public Class ExcelExport
Inherits System.Web.UI.Page
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' 定义是否是 SQL Server 数据库,这里为False
Dim blnIsSQLServer As System.Boolean = False
Dim strSQL As String
Dim objDataset As New DataSet()
Dim objConn As Object
If blnIsSQLServer Then
objConn = New System.Data.SqlClient.SqlConnection("User ID=sa;Initial Catalog=Northwind;Data Source=.\NetSDK;")
objConn.Open()
Dim objAdapter As New System.Data.SqlClient.SqlDataAdapter()
strSQL = "Select * from customers where country='USA'"
objAdapter.SelectCommand = New System.Data.SqlClient.SqlCommand(strSQL, objConn)
objAdapter.Fill(objDataset)
Else
objConn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("aspxWeb.mdb"))
objConn.Open()
Dim objAdapter As New System.Data.OleDb.OleDbDataAdapter()
strSQL = "Select Top 10 Title From Document"
objAdapter.SelectCommand = New System.Data.OleDb.OleDbCommand(strSQL, objConn)
objAdapter.Fill(objDataset)
End If
Dim oView As New DataView(objDataset.Tables(0))
DataGrid1.DataSource = oView
DataGrid1.DataBind()
objConn.Close()
objConn.Dispose()
objConn = Nothing
If Request.QueryString("bExcel") = "1" Then
Response.ContentType = "application/vnd.ms-excel"
' 从Content-Type header中去除charset设置
Response.Charset = ""
' 关闭 ViewState
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
' 获取control的HTML
DataGrid1.RenderControl(hw)
' 把HTML写回浏览器
Response.Write(tw.ToString())
Response.End()
End If
End Sub
End Class
#18
謝 net_lover(孟子E章) ,有沒有C#的代碼?
#19
to net_lover(孟子E章) :這種方法不是在服務器端運行的嗎?我是在服務器端調試的,在服務器上也裝了OFFICE2000,可是運行後網頁顯示的是一個空的EXCEL表格,但很怪的是原來頁面中的一些字符卻能顯示在EXCEL表格中,而DATAGRID中的內容沒法顯示。為什麼?幫幫忙!
#20
我現在在datagrid中的數據可以正常輸出EXCEL了,原因是datagrid所在頁面中放了一些其它控件,我把那些控件移除後就可以正常顯示;但問題是我想把水晶報表的數據用這種方法導出為EXCEL格式(請看下面代碼),但還是出現空的數據,為什麼DATAGRID可以而水晶報表不行?請幫幫忙,搞定後馬上給分!
private void render()
{
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter tw=new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw=new System.Web.UI.HtmlTextWriter(tw);
CrystalReportViewer1.RenderControl(hw); //DataGrid-->CrystalReportViewer1
Response.Write(tw.ToString());
Response.End();
}
private void render()
{
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter tw=new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw=new System.Web.UI.HtmlTextWriter(tw);
CrystalReportViewer1.RenderControl(hw); //DataGrid-->CrystalReportViewer1
Response.Write(tw.ToString());
Response.End();
}
#21
自已頂一下
#22
up
#23
導出數據時時用到的地方要object.tostring().trim() 一下,否則是沒有數據的.
#24
更正一下,在excel中是顯示不出來的.