C#導(dǎo)出GridView數(shù)據(jù)到Excel文件類(lèi)實(shí)例
更新時(shí)間:2015年03月25日 15:07:53 作者:lele
這篇文章主要介紹了C#導(dǎo)出GridView數(shù)據(jù)到Excel文件類(lèi),實(shí)例分析了C#使用GridView及Excel的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#導(dǎo)出GridView數(shù)據(jù)到Excel文件類(lèi)。分享給大家供大家參考。具體如下:
這段C#代碼自定義了一個(gè)封裝類(lèi),用于將GridView數(shù)據(jù)導(dǎo)出到Excel文件
using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
namespace DotNet.Utilities
{
public class ExportExcel
{
protected void ExportData(string strContent, string FileName)
{
FileName = FileName + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "gb2312";
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
//this.Page.EnableViewState = false;
// 添加頭信息,為"文件下載/另存為"對(duì)話(huà)框指定默認(rèn)文件名
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls");
// 把文件流發(fā)送到客戶(hù)端
HttpContext.Current.Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
HttpContext.Current.Response.Write(strContent);
HttpContext.Current.Response.Write("</body></html>");
// 停止頁(yè)面的執(zhí)行
//Response.End();
}
/// <summary>
/// 導(dǎo)出Excel
/// </summary>
/// <param name="obj"></param>
public void ExportData(GridView obj)
{
try
{
string style = "";
if (obj.Rows.Count > 0)
{
style = @"<style> .text { mso-number-format:\@; } </script> ";
}
else
{
style = "no data.";
}
HttpContext.Current.Response.ClearContent();
DateTime dt = DateTime.Now;
string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=ExportData" + filename + ".xls");
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
obj.RenderControl(htw);
HttpContext.Current.Response.Write(style);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
catch
{
}
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- C#導(dǎo)入導(dǎo)出EXCEL文件的代碼實(shí)例
- C#數(shù)據(jù)導(dǎo)入/導(dǎo)出Excel文件及winForm導(dǎo)出Execl總結(jié)
- C#導(dǎo)出數(shù)據(jù)到Excel文件的方法
- C#導(dǎo)出數(shù)據(jù)到CSV文件的通用類(lèi)實(shí)例
- C#實(shí)現(xiàn)pdf導(dǎo)出 .Net導(dǎo)出pdf文件
- C#導(dǎo)出生成excel文件的方法小結(jié)(xml,html方式)
- C#實(shí)現(xiàn)導(dǎo)出List數(shù)據(jù)到xml文件的方法【附demo源碼下載】
- C#如何使用SHBrowseForFolder導(dǎo)出中文文件夾詳解
相關(guān)文章
Avalonia封裝實(shí)現(xiàn)指定組件允許拖動(dòng)的工具類(lèi)
這篇文章主要為大家詳細(xì)介紹了Avalonia如何封裝實(shí)現(xiàn)指定組件允許拖動(dòng)的工具類(lèi),文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧2023-03-03
使用checked語(yǔ)句防止數(shù)據(jù)溢出的解決方法
本篇文章是對(duì)用checked語(yǔ)句防止數(shù)據(jù)溢出的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
winform簡(jiǎn)單緩存類(lèi)實(shí)例
這篇文章主要介紹了winform簡(jiǎn)單緩存類(lèi),涉及C#緩存使用技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-09-09

