有关ASP.NET 和 Excel 返回浏览器
我要在服务器端用代码新建Excel文档,然后直接Response回客户端
以前看到过一个帖子有代码,现在找不到了。。。
有谁知道如何做?
我已经建好了Excel对象,Sheet里面也填充好了内容,就是不知道如何返回给客户端?(不在服务器保存)
记得有Response.ClearHeaders什么的。。。
问题点数:100、回复次数:2Top
1 楼TheAres(班门斧)回复于 2002-11-23 00:27:55 得分 100
Response.AddHeader("Content-Disposition", strHeadValue);
Response.ContentType="Application/vnd.ms-excel";
Response.WriteFile(strFilePath);
参考:
http://support.microsoft.com/default.aspx?scid=KB;EN-US;q306654&ID=KB;EN-US;q306654&lex
Top
2 楼nerk(尘世中一个迷途小书僮)回复于 2002-11-25 15:30:49 得分 0
多謝,可能我說的不是很清楚.
我找到了解決方案:
17. ' Create a connection and open it.
18. Dim objConn As New System.Data.SqlClient.SqlConnection("User ID=sa;Initial Catalog=Northwind;Data Source=SQLServer;")
19. objConn.Open()
20.
21. Dim strSQL As String
22. Dim objDataset As New DataSet()
23. Dim objAdapter As New System.Data.SqlClient.SqlDataAdapter()
24.
25. ' Get all the customers from the USA.
26. strSQL = "Select * from customers where country='USA'"
27. objAdapter.SelectCommand = New System.Data.SqlClient.SqlCommand(strSQL, objConn)
28. ' Fill the dataset.
29. objAdapter.Fill(objDataset)
30. ' Create a new view.
31. Dim oView As New DataView(objDataset.Tables(0))
32. ' Set up the data grid and bind the data.
33. DataGrid1.DataSource = oView
34. DataGrid1.DataBind()
35.
36. ' Verify if the page is to be displayed in Excel.
37. If Request.QueryString("bExcel") = "1" Then
38. ' Set the content type to Excel.
39. Response.ContentType = "application/vnd.ms-excel"
40. ' Remove the charset from the Content-Type header.
41. Response.Charset = ""
42. ' Turn off the view state.
43. Me.EnableViewState = False
44.
45. Dim tw As New System.IO.StringWriter()
46. Dim hw As New System.Web.UI.HtmlTextWriter(tw)
47.
48. ' Get the HTML for the control.
49. DataGrid1.RenderControl(hw)
50. ' Write the HTML back to the browser.
51. Response.Write(tw.ToString())
52. ' End the response.
53. Response.End()
Top




