ASP.NET(C#)將數(shù)據(jù)導(dǎo)出到Word或Excel
來(lái)源:易賢網(wǎng) 閱讀:3633 次 日期:2015-01-14 16:23:32
溫馨提示:易賢網(wǎng)小編為您整理了“ASP.NET(C#)將數(shù)據(jù)導(dǎo)出到Word或Excel”,方便廣大網(wǎng)友查閱!

最簡(jiǎn)單的方法是把頁(yè)面上所有的東西都導(dǎo)出

在載入時(shí)調(diào)用,注意頁(yè)面里不能有其它控件,包括按鈕

void converttoexcel()

{

Response.Clear();

Response.Buffer = true;

Response.Charset = "GB2312";

Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls");

Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

Response.ContentType = "application/ms-excel";

this.Page.EnableViewState = false;

System.IO.StringWriter oStringWriter = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

this.Page.RenderControl(oHtmlTextWriter);

Response.Write(oStringWriter.ToString());

Response.End();

}

ASP.NET(C#)將數(shù)據(jù)導(dǎo)出到Word或Excel

命名空間:

using System.IO;

using System.Text;

將DataGrid的數(shù)據(jù)導(dǎo)出到Excel

string excelname="excel文件名";

HttpContext.Current.Response.Charset = "GB2312";

HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;

HttpContext.Current.Response.ContentType = "application/ms-excel";

HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + excelname + ".xls");

dr1.Page.EnableViewState = false;

StringWriter sw = new StringWriter();

HtmlTextWriter tw = new HtmlTextWriter(sw);

dr1.RenderControl(tw);

HttpContext.Current.Response.Write(sw.ToString());

HttpContext.Current.Response.End();

將DataGrid的數(shù)據(jù)導(dǎo)出到Word

string excelname="word文件名";

HttpContext.Current.Response.Charset = "GB2312";

HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;

HttpContext.Current.Response.ContentType = "application/ms-winword";

HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + excelname + ".doc");

dr1.Page.EnableViewState = false;

StringWriter sw = new StringWriter();

HtmlTextWriter tw = new HtmlTextWriter(sw);

dr1.RenderControl(tw);

HttpContext.Current.Response.Write(sw.ToString());

HttpContext.Current.Response.End();

ASP.NET 2.0,C#----利用GridView控件導(dǎo)出其他文件(導(dǎo)出Excel,導(dǎo)出Word文件)

// 注意,在Visual Studio2005平臺(tái)下,如果使用GridView導(dǎo)出文件,

//就必須重載VerifyRenderingInServerForm方法

public override void VerifyRenderingInServerForm(Control control)

{

}

///

/// 導(dǎo)出到文件的方法,

///

/// Model=1:導(dǎo)出為Execl,Model=2:導(dǎo)出為Word

private void toFiles(int Model)

{

string strFileName = DateTime.Now.ToString("yyyyMMdd-hhmmss");

System.Web.HttpContext HC = System.Web.HttpContext.Current;

HC.Response.Clear();

HC.Response.Buffer = true;

HC.Response.ContentEncoding = System.Text.Encoding.UTF8;//設(shè)置輸出流為簡(jiǎn)體中文

if (Model == 1)

{

//---導(dǎo)出為Excel文件

HC.Response.AddHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8) + ".xls");

HC.Response.ContentType = "application/ms-excel";//設(shè)置輸出文件類(lèi)型為excel文件。

}

else

{

//--- 導(dǎo)出為Word文件

HC.Response.AddHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8) + ".doc");

HC.Response.ContentType = "application/ms-word";//設(shè)置輸出文件類(lèi)型為Word文件。

}

System.IO.StringWriter sw = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

this.GridView1.RenderControl(htw);

HC.Response.Write(sw.ToString());

HC.Response.End();

}

//-導(dǎo)出為Excel文件

protected void ToExecl_Click(object sender, EventArgs e)

{

toFiles(1);

}

//-導(dǎo)出為Word文件

protected void Button1_Click(object sender, EventArgs e)

{

toFiles(2);

}

FROM:http://hi.baidu.com/jg_%B3%C2/blog/item/4f0edf188851c50135fa41ce.html

C#操作Word[轉(zhuǎn)]

導(dǎo)入COM庫(kù):Microsoft word 11.0 Object Library.

更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:ASP.NET(C#)將數(shù)據(jù)導(dǎo)出到Word或Excel
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類(lèi)型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話(huà):0871-65099533/13759567129 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
咨詢(xún)QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)