本人做的一个单位项目中,要求客户端能将数据导出到excel格式保存,
本人将按钮点击事件中调用该函数
代码如下: public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
} protected void ImageButtonExcel_Click(object sender, ImageClickEventArgs e)
{
string strFileName = "在岗职工工资统计报表(" + RadioButtonListType.SelectedValue + ")"; ToExcel(GridViewZgzgtjbb, strFileName);
} private void ToExcel(GridView Gv, string strFileName)
{
Gv.AllowPaging = false;
Gv.AllowSorting = false;
Gv.DataBind(); HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(strFileName) + ".xls");
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
Gv.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
Gv.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End(); Gv.AllowPaging = true;
Gv.AllowSorting = true;
Gv.DataBind();
}这段代码一直用了很久都功能正常,
最近单位由于office版本升级,由excel2003改为了excel2007,于是要求下载的附件直接使用excel2007的文件格式保存(文件扩展名*.xlsx).
请高手指点如何能从gridview导出成excel2007的*.xlsx文件,并且能正常打开不报错.
本人将按钮点击事件中调用该函数
代码如下: public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
} protected void ImageButtonExcel_Click(object sender, ImageClickEventArgs e)
{
string strFileName = "在岗职工工资统计报表(" + RadioButtonListType.SelectedValue + ")"; ToExcel(GridViewZgzgtjbb, strFileName);
} private void ToExcel(GridView Gv, string strFileName)
{
Gv.AllowPaging = false;
Gv.AllowSorting = false;
Gv.DataBind(); HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(strFileName) + ".xls");
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
Gv.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
Gv.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End(); Gv.AllowPaging = true;
Gv.AllowSorting = true;
Gv.DataBind();
}这段代码一直用了很久都功能正常,
最近单位由于office版本升级,由excel2003改为了excel2007,于是要求下载的附件直接使用excel2007的文件格式保存(文件扩展名*.xlsx).
请高手指点如何能从gridview导出成excel2007的*.xlsx文件,并且能正常打开不报错.
解决方案 »
- 在MVS2005中调试正常,但是发布后执行老是说com控件注册失败
- 怎么样用C#不注册调用C++写的DLL控件
- 监视进程
- 求教高手!!
- 菜鸟在线等:如何快速的将一个对话框程序中3个文本框里面的字段,比如:姓名,年龄,性别等保存到本地ACCESS里面去?
- SQLDMO Restore 导入数据路径中有空格通不过??
- 几个简单的问题
- 关于将全局变量写到单独类文件的测试
- c# 调用delphi的dll的参数(枚举类型)传递问题
- winform 问题:如何将一个颜色值存入一个变量中,让其他控件调用
- SQLDMO 还原数据库问题!!!
- c#web编程SQLServerDAL和SQLProfileDAL两个分别有什么作用?还有它们里边都需要些什么?谢谢
引用Microsoft Excel 12.0 Object Library,需要安装excel 2007。
另一种可以使用 Office OPEN XML SDK 2.0 直接存取xlsx格式。
我已经找到并引用了Microsoft Excel 12.0 Object Library,请问要如何修改上面的代码才行?
{
Response.Clear();//清楚缓冲区流所有内容输出
Response.BufferOutput = true;
Response.Charset = "GB2312";//获取或设置输出流的HTTP字符集
//假定导出的文件名为FileName.xls
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");//将http头添加到输出流
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//设置导出的文件格式
Response.ContentType = "application/ms-excel";
EnableViewState = false;
System.Globalization.CultureInfo cultureinfo = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter stringWrite = new System.IO.StringWriter(cultureinfo);
System.Web.UI.HtmlTextWriter textWrite = new HtmlTextWriter(stringWrite);
Gv.RenderControl(textWrite);
//把html流写回到浏览器
Response.Write(stringWrite.ToString());
Response.End();
楼上给的这个方法只适应于 excel2003,我要求的是要成生成 excel2007的 *.xlsx文件。这段代码我试过了,不行,会报错。