DataGrid中的資料匯入到Word和Excel

來源:互聯網
上載者:User
datagrid|excel|word|資料 using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient ;
using System.Text;

namespace DataGrid_import_WordExcel
{
/// <summary>
/// 馬亞紅製作 2004-12-12
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button BtnImportWord;
protected System.Web.UI.WebControls.Button Btn_Import_Excel;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置使用者代碼以初始化頁面
CreateDataSet();
}

#region Web Form設計器產生的程式碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.BtnImportWord.Click += new System.EventHandler(this.BtnImportWord_Click);
this.Btn_Import_Excel.Click += new System.EventHandler(this.Btn_Import_Excel_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void ExportDataGrid(string FileType, string FileName) //從DataGrid匯出
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

Response.AppendHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState =false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw =new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
private void Btn_Import_Excel_Click(object sender, System.EventArgs e)
{
ExportDataGrid("application/ms-excel", "指數列表.xls"); //導到Excel
}

private void CreateDataSet() //建立DataSet
{
DataSet myDataSet = new DataSet("aNewDataSet");
DataTable table1 = MakeTable("ID", "Name");
myDataSet.Tables.Add(table1);
DataGrid1.DataSource=myDataSet;
DataGrid1.DataBind();
}


private DataTable MakeTable(String c1Name,String c2Name) //建表
{
int i;
DataTable myTable=new DataTable();
DataColumn myColumn;
// Add two DataColumns
myColumn = new DataColumn(c1Name,typeof(Int32)); //增加欄位及設定類型
myTable.Columns.Add(myColumn);
myColumn = new DataColumn(c2Name,typeof(string));
myTable.Columns.Add(myColumn);
DataRow Dr;
for(i=1;i<11;i++) //表中增加資料
{
Dr=myTable.NewRow();
Dr[0]=i;
Dr[1]="Name" + i.ToString();
myTable.Rows.Add(Dr);
}
return myTable;
}

private void BtnImportWord_Click(object sender, System.EventArgs e) //導到Word
{
ExportDataGrid("application/ms-word", "指數列表.doc");
}
}
}





相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。