close

001 using System;
002 using System.Data;
003 using System.Configuration;
004 using System.Collections;
005 using System.Web;
006 using System.Web.Security;
007 using System.Web.UI;
008 using System.Web.UI.WebControls;
009 using System.Web.UI.WebControls.WebParts;
010 using System.Web.UI.HtmlControls;

011
012 public partial class excel : System.Web.UI.Page
013 {
014     protected void Page_Load(object sender, EventArgs e)
015     {
016
017     }

018     protected void ExportExcel_Click(object sender, EventArgs e)
019     {
020         //匯出excel檔
021         Response.Clear();
022         Response.AddHeader("content-disposition", "attachment;filename=test.xls");//excel檔名
023         Response.ContentType = "application/vnd.ms-excel";
024         Response.Charset = "";
025         System.IO.StringWriter sw = new System.IO.StringWriter();
026         System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
027         DataGrid dg = new DataGrid();
028         dg.DataSource = this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
029         dg.DataBind();
030         dg.RenderControl(htw);
031         Response.Write(sw.ToString());
032         Response.End();
033
034         //儲存excel檔
035         //System.IO.StringWriter sw = new System.IO.StringWriter();
036         //System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
037         //DataGrid dg = new DataGrid();
038         //dg.DataSource = this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
039         //dg.DataBind();
040         //dg.RenderControl(htw);
041         //System.IO.File.WriteAllText(@"c:\test.xls", sw.ToString());
042     }

043     protected void ExportWord_Click(object sender, EventArgs e)
044     {
045         //匯出word檔
046         Response.Clear();
047         Response.AddHeader("content-disposition", "attachment;filename=test.doc");//word檔名
048         Response.ContentType = "application/vnd.ms-word";
049         Response.Charset = "";
050         System.IO.StringWriter sw = new System.IO.StringWriter();
051         System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
052         DataGrid dg = new DataGrid();
053         dg.DataSource = this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
054         dg.DataBind();
055         dg.RenderControl(htw);
056         Response.Write(sw.ToString());
057         Response.End();
058
059         //儲存word檔
060         //System.IO.StringWriter sw = new System.IO.StringWriter();
061         //System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
062         //DataGrid dg = new DataGrid();
063         //dg.DataSource = this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
064         //dg.DataBind();
065         //dg.RenderControl(htw);
066         //System.IO.File.WriteAllText(@"c:\test.doc", sw.ToString());
067     }

068     protected void ExportText_Click(object sender, EventArgs e)
069     {
070         //匯出txt檔
071         Response.Clear();
072         Response.AddHeader("content-disposition", "attachment;filename=test.txt");//txt檔名
073         Response.ContentType = "application/vnd.ms-word";
074         Response.Charset = "";
075         System.Text.StringBuilder sb = new System.Text.StringBuilder();
076         DataTable dt = new DataTable();
077         dt = ((DataView)this.SqlDataSource1.Select(DataSourceSelectArguments.Empty)).ToTable();
078         //欄位名
079         for (int i = 0; i < dt.Columns.Count; i++)
080         {
081             sb.Append(dt.Columns[i].ColumnName);
082             sb.Append(",");
083         }

084         sb.Append(Environment.NewLine);
085         //資料
086         for (int i = 0; i < dt.Rows.Count; i++)
087         {
088             for (int j = 0; j < dt.Columns.Count; j++)
089             {
090                 sb.Append(dt.Rows[i][j].ToString());
091                 sb.Append(",");
092             }

093             sb.Append(Environment.NewLine);
094         }

095         Response.Write(sb.ToString());
096         Response.End();
097
098         //儲存txt檔
099         //System.Text.StringBuilder sb = new System.Text.StringBuilder();
100         //DataTable dt = new DataTable();
101         //dt = ((DataView)this.SqlDataSource1.Select(DataSourceSelectArguments.Empty)).ToTable();
102         ////欄位名
103         //for (int i = 0; i < dt.Columns.Count; i++)
104         //{
105         // sb.Append(dt.Columns[i].ColumnName);
106         // sb.Append(",");
107         //}

108         //sb.Append(Environment.NewLine);
109         ////資料
110         //for (int i = 0; i < dt.Rows.Count; i++)
111         //{
112         // for (int j = 0; j < dt.Columns.Count; j++)
113         // {
114         // sb.Append(dt.Rows[i][j].ToString());
115         // sb.Append(",");
116         // }

117         // sb.Append(Environment.NewLine);
118         //}
119         //System.IO.File.WriteAllText(@"c:\test.txt", sb.ToString());
120     }
121 }

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 winnercow 的頭像
    winnercow

    大智若愚,努力中

    winnercow 發表在 痞客邦 留言(0) 人氣()