日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据到入到excel和打印功能

發布時間:2025/3/21 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据到入到excel和打印功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

借花獻佛,一直來在用的東西,但是不知道那里找到的了
ExportToExcel.aspx文件:

?1?<%@?Page?language="c#"?Codebehind="ExportToExcel.aspx.cs"?AutoEventWireup="false"?Inherits="CSharpProject.Excel.ExportToExcel"?%>
?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:

?1?using?System;
?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和打印功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。