Excel导入和HTML控件中的呈现 [英] Excel import and rendering in html control

查看:213
本文介绍了Excel导入和HTML控件中的呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击页面上的某些按钮后,我运行一些函数以提取dg中的记录,并将dg传递给以下函数和文件名.

After clicking on some button the page, i run some function to fetch the records in dg and pass dg to below function and filename.

private void ExportToExcel(string strFileName, DataGrid dg)
    {

HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;

        HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + strFileName + ".xls");
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
//dg is DataGrid records
        dg.RenderControl(htw);
        HttpContext.Current.Response.Write(sw.ToString());
        HttpContext.Current.Response.End();}


当它提示打开excel文件时,则显示以下消息


when it promts to open the excel file then it shows below message

"The file you trying to open, ''filename.xls'', is in a different format than specified by the file extension.Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"<br />
Yes | No | Help

推荐答案

下面的链接为您提供了工作示例:
http://forums.asp.net/t/1255489.aspx [ http://blog.weareon. net/export-datatable-to-excel-in-asp-net-without-using-excel-interop-part-ii [
Below link provides you the working example:
http://forums.asp.net/t/1255489.aspx[^]
http://blog.weareon.net/export-datatable-to-excel-in-asp-net-without-using-excel-interop-part-ii[^]


这篇关于Excel导入和HTML控件中的呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆