ASP.NET(C#)将数据导出到Word或Excel
来源: 阅读:882 次 日期:2014-10-13 10:34:39
温馨提示: 小编为您整理了“ASP.NET(C#)将数据导出到Word或Excel”,方便广大网友查阅!

最简单的方法是把页面上所有的东西都导出

在载入时调用,注意页面里不能有其它控件,包括按钮

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#)将数据导出到Word或Excel

命名空间:

using System.IO;

using System.Text;

将DataGrid的数据导出到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的数据导出到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控件导出其他文件(导出Excel,导出Word文件)

// 注意,在Visual Studio2005平台下,如果使用GridView导出文件,

//就必须重载VerifyRenderingInServerForm方法

public override void VerifyRenderingInServerForm(Control control)

{

}

///

/// 导出到文件的方法,

///

/// Model=1:导出为Execl,Model=2:导出为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;//设置输出流为简体中文

if (Model == 1)

{

//---导出为Excel文件

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

HC.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。

}

else

{

//--- 导出为Word文件

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

HC.Response.ContentType = "application/ms-word";//设置输出文件类型为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();

}

//-导出为Excel文件

protected void ToExecl_Click(object sender, EventArgs e)

{

toFiles(1);

}

//-导出为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[转]

导入COM库:Microsoft word 11.0 Object Library.

更多信息请查看IT技术专栏

更多信息请查看网络编程
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!

2025国考·省考课程试听报名

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:
咨询QQ:526150442(9:00—18:00)版权所有:
云南网警报警专用图标
Baidu
map