您的位置:首页 > 大数据 > 云计算

EMC范承工:“五大支柱”为云计算保驾护航

2009-12-14 15:02 253 查看
下面代码是C# .net输出word和excel文件的公告函数,使用方法先建立gridview填充数据,然后用时copy如下函数到cs页面,调用方法 OutExcel(GridviewName, "File name.xls"); OutWord(GridviewName, "File name.doc");
指定gridview的 名字和文件名即可!
//输出到excel的函数,可直接copy到 cs页面
private void OutExcel(GridView dg, string name)
{
dg.Visible = true;
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
name = "attachment;filename=" + name;
Response.AppendHeader("Content-Disposition", name);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
dg.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
dg.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
//输出到word的函数,可直接copy到 cs页面
private void OutWord(GridView dg, string name)
{
//string name="lessons.xls";
dg.Visible = true;
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
name = "attachment;filename=" + name;
Response.AppendHeader("Content-Disposition", name);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-word";
dg.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
dg.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
//重载VerifyRenderingInServerForm方法,调用页面必须加入否则会提示错误
public override void VerifyRenderingInServerForm(Control control)
{
}
//调用方法 OutWord(Student, "File name.doc");
protected void Button1_Click(object sender, EventArgs e)
{
OutWord(Student, "File name.doc");
}
//调用方法 OutExcel(Student, "File name.xls");
protected void Button2_Click(object sender, EventArgs e)
{
OutExcel(Student, "File name.xls");
}
通过以上方式输出的word、Excel是没有格式的
本文出自 “www.faako.com” 博客,请务必保留此出处http://leyton.blog.51cto.com/892183/370299
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: