数据到入到excel和打印功能
生活随笔
收集整理的這篇文章主要介紹了
数据到入到excel和打印功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
借花獻佛,一直來在用的東西,但是不知道那里找到的了
ExportToExcel.aspx文件:
?2?<HTML>
?3?????<HEAD>
?4?????????<title>ExportToExcel</title>?
?5?????????<!--?#include?file?=?"PrintFunction.inc"-->
?6?????</HEAD>
?7?????<body>
?8?????????<form?id="Form1"?method="post"?runat="server">
?9?????????????<!--需要導入到Excel的內容-->
10?????????????<asp:Table?ID="tabReport"?Runat="server">
11?????????????????<asp:TableRow>
12?????????????????????<asp:TableCell>
13?????????????????????????<!--需要打印的內容-->
14?????????????????????????<!--StartPrintPoint-->
15?????????????????????????<TABLE?id="Table1"?cellSpacing="1"?cellPadding="1"?width="300"?border="1">
16?????????????????????????????<TR>
17?????????????????????????????????<TD>姓名</TD>
18?????????????????????????????????<TD>性別</TD>
19?????????????????????????????????<TD>年齡</TD>
20?????????????????????????????</TR>
21?????????????????????????????<TR>
22?????????????????????????????????<TD>張三</TD>
23?????????????????????????????????<TD>男</TD>
24?????????????????????????????????<TD>20</TD>
25?????????????????????????????</TR>
26?????????????????????????????<TR>
27?????????????????????????????????<TD>李四</TD>
28?????????????????????????????????<TD>男</TD>
29?????????????????????????????????<TD>20</TD>
30?????????????????????????????</TR>
31?????????????????????????</TABLE>
32?????????????????????????<!--EndPrintPoint-->
33?????????????????????????<!--需要打印的內容結束-->
34?????????????????????</asp:TableCell>
35?????????????????</asp:TableRow>
36?????????????</asp:Table>
37?????????????<!--需要導入到Excel的內容結束-->
38?????????????<asp:Button?id="btnExport"?runat="server"?Text="導入到Excel"></asp:Button>
39?????????????<input?type="button"?value="打印"?onclick="preview(3);">
40?????????</form>
41?????</body>
42?</HTML>
ExportToExcel.aspx.cs:
?2?using?System.Collections;
?3?using?System.ComponentModel;
?4?using?System.Data;
?5?using?System.Drawing;
?6?using?System.Web;
?7?using?System.Web.SessionState;
?8?using?System.Web.UI;
?9?using?System.Web.UI.WebControls;
10?using?System.Web.UI.HtmlControls;
11?
12?namespace?CSharpProject.Excel
13?{
14?????public?class?ExportToExcel?:?System.Web.UI.Page
15?????{
16?????????protected?System.Web.UI.WebControls.Button?btnExport;
17?????????protected?System.Web.UI.WebControls.Table?tabReport;
18?????
19?????????private?void?Page_Load(object?sender,?System.EventArgs?e)
20?????????{
21?????????}
22?
23?????????#region?Web?窗體設計器生成的代碼
24?????????override?protected?void?OnInit(EventArgs?e)
25?????????{
26?????????????//
27?????????????//?CODEGEN:?該調用是?ASP.NET?Web?窗體設計器所必需的。
28?????????????//
29?????????????InitializeComponent();
30?????????????base.OnInit(e);
31?????????}
32?????????
33?????????///?<summary>
34?????????///?設計器支持所需的方法?-?不要使用代碼編輯器修改
35?????????///?此方法的內容。
36?????????///?</summary>
37?????????private?void?InitializeComponent()
38?????????{????
39?????????????this.btnExport.Click?+=?new?System.EventHandler(this.Button1_Click);
40?????????????this.Load?+=?new?System.EventHandler(this.Page_Load);
41?
42?????????}
43?????????#endregion
44?
45?????????private?void?Button1_Click(object?sender,?System.EventArgs?e)
46?????????{
47?????????????Response.Clear();?
48? ????????Response.Buffer=?true;?
49? ????????Response.Charset="utf-8"; ?
50? ????????//下面這行很重要,?attachment?參數表示作為附件下載,您可以改成?online在線打開?
51? ????????//filename=FileFlow.xls?指定輸出文件的名稱,注意其擴展名和指定文件類型相符,可以為:.doc? ?.xls? ?.txt? .htm ?????????
52? ????????Response.AppendHeader("Content-Disposition","attachment;filename=FileFlow.xls");?
53? ????????Response.ContentEncoding=System.Text.Encoding.GetEncoding("gb2312"); ?
54? ????????//Response.ContentType指定文件類型?可以為application/ms-excel? ?application/ms-word? ?application/ms-txt? ?application/ms-html? ?或其他瀏覽器可直接支持文檔 ?
55? ????????Response.ContentType?=?"application/ms-excel";?
56? ????????this.EnableViewState?=?false; ?
57? ????????//二、定義一個輸入流 ?
58? ????????System.IO.StringWriter?oStringWriter?=?new?System.IO.StringWriter();?
59? ????????System.Web.UI.HtmlTextWriter?oHtmlTextWriter?=?new?System.Web.UI.HtmlTextWriter(oStringWriter); ?
60? ????????//三、將目標數據綁定到輸入流輸出 ?
61? ????????//?this.RenderControl(oHtmlTextWriter);? ?
62?????????????//grdData.RenderControl(oHtmlTextWriter);
63?????????????tabReport.RenderControl(oHtmlTextWriter);
64? ????????//this?表示輸出本頁,你也可以綁定datagrid,或其他支持obj.RenderControl()屬性的控件 ?
65? ????????Response.Write(oStringWriter.ToString());?
66? ????????Response.End();
67?????????}
68?????}
69?}
70?
轉載于:https://www.cnblogs.com/Xuzq/archive/2005/10/11/ExportToExcel.html
總結
以上是生活随笔為你收集整理的数据到入到excel和打印功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常用的Net Command
- 下一篇: 在Asp.net页面中实现数据饼图