asp.net(c#)将数据导出到word或excel
来源: 阅读:643 次 日期:2014-10-24 14:56:57
温馨提示: 小编为您整理了“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